• Post Reply Bookmark Topic Watch Topic
  • New Topic

Questions about improving my code  RSS feed

 
Lucian Whiteman
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) What is Single Responsibility principle ? "A class should have one and only one reason to change" - sounds extremely stupid to me. I for once change a class when I fix a bug, or when I refactor it. To claim that you need to write classes so that you will refactor them - that is nonsense.

2) What is "the law of Demeter" ?

3) What is "the principle of least Astonishment " ?

4) Some static methods should not be static ? I usually make each method static if I can, since they run faster - so why should I stop doing that ? What is the legitimate reason for a method to be static, and why should I make non static a method that may be static (only uses its parameters and no fields of its object) ? Perhaps you could provide me with examples.

5) How do I identify features ? So that I group code into features. What if a method in one of my helper classes can be called by a feature that checks the weather and by another feature that composes music. should I then duplicate it ? Should I consider the helper class with that method as a third feature ? Is it not a bit overdoing in grouping code by features ?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) That's not what Single Responsibility Principle means. It's about having only one responsibility. For example, a well designed class isn't responsible for both getting data from the database and printing a pretty report. That said, refactoring is a good thing.

2) Have you tried Googling it yet? What don't you understand about what you found?

3) This is often called the "principle of least surprise"

4) The performance difference between static and non-static is negligible. It's not something you should be thinking about on design. If we did that, everything would static including variables. A method should be an instance method when it uses data that goes with the object.

5) No, you shouldn't duplicate it. That's why it is in a helper class in the first place - so it can be called from multiple places.
 
Lucian Whiteman
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Jeanne. I was hoping someone could give me some examples, and some tips regarding these issues.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are asking for is like a book. Please be more specific about what you want to know.
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lucian Whiteman wrote: . . . I usually make each method static . . .
Search this forum for “Campbell Ritchie classification of methods”.
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,
If you meant this, then it is quite an interesting classification.
 
Lucian Whiteman
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Lucian Whiteman wrote: . . . I usually make each method static . . .
Search this forum for “Campbell Ritchie classification of methods”.


That classification is wrong. Not to sound condescending, you claim that:

1: Those which require information from the outside world to operate. You can recognise these because they have method parameters.
2: Those which do not require information from outside; they have empty () in their heading.


I do beg your pardon, by a method without parameters can require information from outside if it checks class fields or object fields or some static methods from a package imported by its class.
Having said that, and I am grateful for your contribution to this thread, I fail to see the relevance to my question.

I asked for some sort of rule of thumb about when to make or not make a method static.
 
Tim Harris
Ranch Hand
Posts: 57
3
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lucian Whiteman wrote:I asked for some sort of rule of thumb about when to make or not make a method static.


Understanding how static methods work will help you understand when they should be used (generally speaking).

Static methods are able to be accessed without instantiating the class they belong to. This does definitely have some benefits, especially when referring to utility classes such as Math - you don't need to create an object to add two numbers together, you just need inputs. Instantiating a class to do so would be using up resources that weren't necessary at the time. So many of these "utility" methods are static.

So, as this stackoverflow question points out, a good rule of thumb is to ask yourself: "Does it make sense to call this method without creating an associated object?" Or, conversely, will you need to use object-oriented approaches to complete your goal? If the answers are respectively yes and no, the method should be static.
 
Lucian Whiteman
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thank you for all your answers. But these are begginer answers.
The guys that are good have a standard when to make a method static, and corner cases and cutoff points.
For example:




When is it not a good idea to chose add1 instead of add, and why ?
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, very few of the methods I write are static. It all depends on what the purpose of the method is though. If it does not require an instance of the class then consider making it static. But then also consider why that method is in the class.

Another thing to consider is how testable your code is if that method is static. It becomes less easy to mock out dependencies if the dependency is a static method because you have hard coded the specific implementation that your class is using.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!