• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Java Best practices - Clean Code  RSS feed

 
Ranch Hand
Posts: 640
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
clean code book contains a statement :

The method should not invoke the methods on objects that are returned by any of allowed functions
e.g
because it calls getScratchDir() on return value of getOptions() and then getAbsolutePath() on return value of getScratchDir();



But modifying above as :
doesn't it make the code more complex. A single line code converted to multiple line.
Also, are not the JDK functions meant to be use anywhere as they are(such as getAbsolutePath), without calling them in separate functions.

Please clarify me if I misunderstood something.
 
Puspender Tanwar
Ranch Hand
Posts: 640
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok I understood it. The code should be like:

it is written and explained further in book. Doubts cleared
 
Greenhorn
Posts: 28
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know what that book focus on, but functional programming states the opposite. In particular, afaik, with the introduction of the stream (in Java >= 8), it is common practice to call method in "cascade" style.
 
Puspender Tanwar
Ranch Hand
Posts: 640
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stefano Carniel wrote:Don't know what that book focus on, but functional programming states the opposite. In particular, afaik, with the introduction of the stream (in Java >= 8), it is common practice to call method in "cascade" style.


your statement is making me confused
The book is about writing clean quality codes.
 
Stefano Carniel
Greenhorn
Posts: 28
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can, take a look at this book. It explains very well these concepts, not only, it teaches how to implement classes that support this programming style (methods cascade).
Honestly, what your book states is not in contrast whith what mine states. Functional programming can help to essentially produce safer and more concise code, but for sure this code is less readable (but not necessarily less clean).
 
Sheriff
Posts: 21689
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although I'm a huge fan of chained method calls, it makes your code less readable - unless you use some tricks that is. For instance, I always put each filter and map call on its own line. The above example would be just as readable if you just add some line breaks:

This has two advantages:
1) It's more readable.
2) NullPointerExceptions are reported on the line they are caused by. With a one-liner, you couldn't tell the difference if ctxt was null, or if ctxt.getOptions() returned null, or if ctxt.getOptions.getScratchDir() returned null - they would all originate from the same line. With my example there would be three different lines for the three different causes.


Note that I almost never put the first call on a separate line, unless if all calls are similar, like StringBuilder.append or the String.replace example from https://coderanch.com/t/685210/java/String-replace-working#3216365.
 
I can't take it! You are too smart for me! Here is the tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!