Go Ahead: Next Generation Java Programming Style

Many companies and developers move away from Java to new languages: Ruby, Python, Groovy, Erlang, Scala. You might be trapped with Java.

Even if you’ve trapped, you can change your programming style and reap some of the benefits of those new languages. In the last 15 years Java programming style has changed significantly:

  1. Final is your new love: More and more Java developers in the teams I’ve worked with, started to use final. Making variables final prevents changing those values. Most often, if you reasign a value, it’s a bug or you should use a new variable. Final prevents bugs and makes code easier to read and understand. Make everything immutable.

    I’ve written more about this topic in “All variables in Java must be final”.

  2. No setters. Many Java developers automatically – sometimes with the evil help of an IDE – write setters for all fields in their classes. You should not use setters. Think about each setter you want to write, are they really necessary for your fields? Better create new copies of your objects if values change. And try to write code without getters either. Tell, don’t ask tells you more about the concept.
  3. Do not use loops for list operations. Learning from functional languages, looping isn’t the best way to work on collections. Suppose we want to filter a list of persons to those who can drink beer. The loop versions looks like:

    This can – even in Java – be rewritten to a more a functional programming style. For example using Google collections filter:

    As remarked by Dave Jarvis, I should have dropped the getter, and he’s right ;-)

    which would lead to better code down the road:

    The predicate version is slightly larger, but consists of two parts. Each one is easier to understand. While the loop version gets unreadable fast with new requirements, the functional version can easily be combined,

    More on this at the Metaphysical Developer.

  4. Use one liners: Write concise code. Java is a noisy language. But don’t make it noiser as it needs to be. Instead of

    write

    IDEA and possibly other IDEs can keep oneliners formatted as oneliners, if you tell them so.

  5. Use many, many objects with many interfaces. Domain driven design currently makes a big splash. A class should be splitted into many roles, implementing many interfaces. This way the class is reusable.

    Methods should be written to only work on roles, not classes. This way methods can work on more objects. Another benefit is lower coupling.

    I’ve written more about that in “Never, never, never use String in Java “.

  6. Use Erlang-Style Concurrency. The Java concurrency primitives like locks and synchronized have been proven to be too low level and often to hard to use. There are better ways to write concurrent code. Erlang Style concurrency is one of them – in Java there are many ways to achive this in Java – I’ve written about them here. Newer ones are Akka and Actorom. You can also use Join/Fork or the myriad of data structures in java.util.concurrent.
  7. Use Fluent Interfaces. Fluent interfaces can make your code much more readable and shorter. A very good example is the MapMaker API in Google Collections:

  8. Data Transfer Objects without setters and getters. If you have simple data holders or data transfer objects, don’t write boiler plate code with getters and setters. I know this is heresy in Java, but just use public fields. Instead of

    write

    Refactoring is easy, should you need it. If you do not control all the code and it’s usage, you might need to be more careful though.

This tips lead to better Java code. Try them in your next Java class. What do you think? I’d like to hear how your Java coding style has changed in the last 10 years.

Uodate: Some thoughts to Cedrics thoughts.

As the author of the post, your thoughts are appreciated, some of mine:

“Besides, it’s convenient to be able to mutate object if you want to use pools.”

No setters doesn’t mean you can’t mutate objects, it’s just that plain setters are not object oriented thinking. How about stop() vs. setStop(true);

“I think the first example is more readable than the second one that uses Predicates.”

I think your assumptions are wrong. Loops are procedural code, while Predicates are, encapsulated, reusable, understandable objects. Nothing to do with functional programming, plain OO – it’s only ursurped by functional programming, therefor I refer to it as FP.

258 thoughts on “Go Ahead: Next Generation Java Programming Style”

  1. Pingback: nike blazer paris
  2. Pingback: human hair
  3. Pingback: electric scooter
  4. Pingback: コピー時計
  5. Pingback: parajumpers salg
  6. Pingback: 財布コピー
  7. Pingback: roger vivier shoes
  8. Pingback: fifa 16 coins
  9. Pingback: 財布 風水 紺
  10. Pingback: fut coin buy
  11. Pingback: fut coin store
  12. Pingback: fifa 16 coins risk
  13. Pingback: fut coins for you
  14. Pingback: fifa 16 coins ps3
  15. Pingback: fut coins for you
  16. Pingback: fifa 14 coin store
  17. Pingback: fifa coins seller
  18. Pingback: nike outlet uk
  19. Pingback: air max
  20. Pingback: FENDI ?? ??????
  21. Pingback: nike tn pas cher
  22. Pingback: counter go
  23. Pingback: game counterstrike
  24. Pingback: cs go buy steam
  25. Pingback: awp skin
  26. Pingback: update csgo
  27. Pingback: trading cs go
  28. Pingback: cosmetics mac
  29. Pingback: cs go 2014
  30. Pingback: cs go skin
  31. Pingback: cs team
  32. Pingback: big wheel scooter
  33. Pingback: ak cartel csgo
  34. Pingback: best
  35. Pingback: fucoxanthin
  36. Pingback: geniux pills
  37. Pingback: intelligex
  38. Pingback: intellux
  39. Pingback: probrain
  40. Pingback: radian c
  41. Pingback: supreme antler
  42. Pingback: genius reviews
  43. Pingback: alpha f1
  44. Pingback: alpha fuel
  45. Pingback: alpha man pro
  46. Pingback: alpha peak
  47. Pingback: alpha xtrm
  48. Pingback: caralluma extract
  49. Pingback: anabolic rx24
  50. Pingback: cognifocus
  51. Pingback: garcinia melt
  52. Pingback: max synapse
  53. Pingback: nerventrax
  54. Pingback: nutra forskolin
  55. Pingback: premium cleanse
  56. Pingback: testorip
  57. Pingback: testoroar
  58. Pingback: pro forskolin
  59. Pingback: forskolin slim
  60. Pingback: garcinia xt
  61. Pingback: forskolin fuel
  62. Pingback: vitapulse
  63. Pingback: Vita pulse
  64. Pingback: vitapulse reviews
  65. Pingback: vitapulse review
  66. Pingback: pure colon detox
  67. Pingback: somnapure
  68. Pingback: perfect biotics
  69. Pingback: reviews vitapulse
  70. Pingback: vitapulse vitamins
  71. Pingback: vitapulse scam
  72. Pingback: vitapulse
  73. Pingback: vitapulse
  74. Pingback: vita pulse
  75. Pingback: taurus
  76. Pingback: Bikini Body Guide
  77. Pingback: Destroy Depression
  78. Pingback: How To Cure ED
  79. Pingback: Diabetes Free scam
  80. Pingback: Alexapure Pro
  81. Pingback: T90 Xplode
  82. Pingback: Tea tox
  83. Pingback: Tai Cheng
  84. Pingback: MegaDrox Reviews
  85. Pingback: Test X180 Reviews

Comments are closed.