• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Code Review: Should I be using getters in this class? Code is for a statistician class.

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not quite sure that I have a strong grasp of when to use getters and setters. Should I be using getters in this program? Class is for a statistician. Please let me know what you think of my code.

Questions:
Should I be using getters anywhere in this program?
Is there a better way to check that a number is in a range of doubles?
Should I be coding exception statements into my program?
Do I need to initialize my instance variables to a number or can i let the JVM take care of that?
Is there anything that may be considered poor design?

Any other suggestions are welcome. Thanks everyone!


 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Quatrani wrote:I'm not quite sure that I have a strong grasp of when to use getters and setters.


So, what is it that you don't understand? That's far easier than us ploughing through 200 lines of code.

But just as a quick tip:

You should ALWAYS use getters if something outside your class needs to "get" a value. And the reason for that is that ALL your member fields should be private. ALWAYS. No 'if''s, or 'and's, or "what about my tortuous situation"s. ALWAYS - at least until you're an expert.

Setters are something different: If nothing outside your class needs to "set" anything, then DON'T provide them.

As to your other questions, you have lots, and they seem to be linked with your code; so I'll answer when I've had a chance to look at it.

Winston
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quickie: your class name violates conventions.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Should I be using getters anywhere in this program?


Yes, you should encapsulate the state of your object and expose them only via getters. So in future if you would want to add some checks or some code while fetching the variable value you can always do it in the getter. Avoid providing setters unless there is a need to change the state. I would advise not to provide setters at all, so that if there is a need to change the value of some instance variable, a new object with the updated value can be constructed.

Should I be coding exception statements into my program?


Any exceptional conditions in your code need to be handled. You can do that by throwing relevant exceptions or taking care of the exceptional conditions gracefully.

Do I need to initialize my instance variables to a number or can i let the JVM take care of that?


JVM will take care of initializing the instance variables but that will be to their default values. So if you have a List instance variable it will be initialized to null i.e non primitive variables will be initialized to null and primitive variables initialized to their default values.

Is there anything that may be considered poor design?


I would need to go through the code much better to comment on that. May be I will take some time for that.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohamed Sanaulla wrote: . . . Avoid providing setters unless there is a need to change the state. . . .

…and if you do need to change the state, it is probably better to provide a method which represent an action rather than a setter.
Example: a BankAccount objet would not have a setBalance method. It would however have deposit and withdraw methods which alter the balance..
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Quatrani wrote: . . .
Do I need to initialize my instance variables to a number or can i let the JVM take care of that?
. . .

It used to say in the Java Tutorials that all fields should be explicitly initialised, even if to the same value as their default value, as a matter of good style. I can't remember which section that is in.
 
Everyone is a villain in someone else's story. Especially this devious tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic