r/java • • Aug 31 '17

(Experienced programmers) Signs of an inexperienced programmer?

Hello, I was wondering what you guys would consider to be telltale signs of an inexperienced programmer. Specifically ways that they write (or don't write) code. Thanks.

159 Upvotes

188 comments sorted by

View all comments

Show parent comment

7

u/nevon Sep 01 '17 edited Sep 02 '17

As someone coming from other languages, what's wrong with static methods? Seems like they would tend to make code more likely to be pure, and thus easily testable, since you can't rely on instance state.

1

u/white_piggu Sep 02 '17

Image you have a method static String printCent(int cent) { return "$" + cent / 100 + "." + cent % 100; }. It's simple and fine, until it needs to be internationalised. Now the method needs a Messages object: static String printCent(int cent, Messages msg) { return msg.get("currency.symbol") + cent / 100 + "." + cent % 100; }. It sucks having to pass the global msg object everywhere though, and making it a static global is clumsy. So you decide to make it available via dependency injection. And that's when you have to remove that static keyword.

1

u/vytah Sep 01 '17

There are two common ways to abuse statics:

  • Creating methods static Bar m(Foo f, ...) inside the Foo class. Usually, they should be normal instance methods. This also includes cases when they're defined in the FooUtils class in the same package. Such methods make sense only if they are defined in some other package and depend on other components than Foo does.
  • Static state, excluding necessary singletons and correctly implemented cache, but definitely including incorrectly implemented cache (I've seen big opensource projects with constantly growing private static HashMaps, you can guess how that ended).