• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static without a cause  RSS feed

 
Jan de Boer
Ranch Hand
Posts: 830
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading through some old code and the creator of the source is using static functions in a class everywhere he can. For example also private functions that are always called from a created object of the class. Is there some efficiency reason for doing this? Why should people prefer to make 'everything' static where they can?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan de Boer wrote:I am reading through some old code and the creator of the source is using static functions in a class everywhere he can. For example also private functions that are always called from a created object of the class. Is there some efficiency reason for doing this? Why should people prefer to make 'everything' static where they can?

Quite a bit there.

Let's deal with your questions:

1. Is there some efficiency reason for doing this?
Answer: theoretically yes, but in practise no; and even if there were, it's the sort of micro-optimization that Donald Knuth warned us all about a long, long time ago.

2. Why should people prefer to make 'everything' static where they can?
Answer: They shouldn't. And the only reason they would (and this is just my guess) is probably because they're beginners, and they discovered that you can't call an instance method from main(); which is where newbies usually start out doing all their work - a very bad habit, I might add.

So, congratulations: you're clearly developing a good sense of "code smell".

Winston
 
Jan de Boer
Ranch Hand
Posts: 830
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank for confirming this is just bad coding. I thought I missed something here. :-)
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Possibly because he had a book teaching procedural programming and could only translate it into Java by making things into functions.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!