• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why is splitting some lines of code neater?

 
Ranch Hand
Posts: 271
6
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,

I made a new thread for this since I think every new developer should understand this.

I would like to know why is splitting some lines of codes neater and more readable since honestly in some cases I find this


Way neater, easily readable and more organized than this:


I believe it was Cambell who told me that the smallest method should be at least 3 lines long, and even though I agree that sometimes I do go too far by having in some cases a method that should be 4 - 5 lines in 1, but in some cases I prefer grouping them up as shown in the former code.

Cheers
 
Greenhorn
Posts: 21
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Yossuf,

It’s not as much about how neat it looks but how readable it is to another programmer.

It’s advised to keep the method signature on one line and the body of the method indented.

As I am sure you have seen, Indentation and carriage returns help a lot to identify right away a specific block of code.

I understand it might not seem crucial on your example of just a return statement but when I see a tight portion of code like your first example, I think more of variables declaration rather than methods definitions.
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I happen to like the one line approach in this case, though I use it very sparingly. I normally add a space after the opening brace and before the closing brace. Most of us that have been doing this awhile have migrated to one of the IDEs which will do automatic formatting. Sometimes it's easiest to let the IDE do its thing, a benefit of which is consistency.

One of the lines that I did object to in your code was:

I would add some spaces and braces to get this:

And people get into religious wars over things like the placement of braces. This my preferred layout. I find it much easier to visually detect missing or misaligned braces. The down side being that less code is visible on a page at a time, but that is less of a factor in today's world of viewing (mostly) everything on large monitors.

YMMV
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The rule of thumb on my teams is to let the IDE worry about formatting, let the programmer worry about code clarity in terms of intention-revealing names and clean designs. I ask developers to autoformat their code before committing it to version control. We also have our formatting rules checked into our VCS. New team members don't get any choice as to their code formatting rules, they have to use the team's standard for shared code whether it conforms to their own personal preferences or not.
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yosuf Ibrahim wrote:I would like to know why is splitting some lines of codes neater and more readable


That is because of convention, which is being used probably by 99% of Java developers. If all of your team members prefer that style - probably that is fine as long as team doesn't change. But if all of your team members leave the company and then regular Java developer being hired, this is where problems start - there is a bigger chance that such developer got used to a conventional style.

Another thing. Now you have methods:

Let's assume you found out you need another method, getDescriptionByProductCode(), so you have:

Nice, so let's combine them:

Ups!

Let me guess what you do next, go back to all methods previously defined and sort out spacing:

Then one of senior developers said, you know, productCode parameter name isn't really clear enough, nor searcheable, can you please change to eanCode, you say, ok, good idea:

Damn!!!

What's next, yet again? Thanks, but no! So you don't do that kind of stuff as it complicates things. Such convention was created for a reason, and is used in many languages, even modern (more recent than Java), I trust it was developed through many years of research.

-----------------------------

Another reason:

Now imagine you got an exception, and once you need to identify where it happened, you go and look for stack trace and its specified line number, now since there are couple of places where one or another exception can happen, all of them would be specified as a same line - that complicates things from the debugging point of view. What you want to achieve by looking to stack trace, is that whenever you see a line number, you'd be pretty confident that once you there, you'd see the cause right away.

However, your second example isn't good enough too, because each successive method supposed to be separated by an empty line.

Slightly out off topic, I think you can improve the name of method getImage(), because it actually returns ImageIcon, while method suggests different, field name you have image too, what it is after all? icon or image or imageIcon? Such confusion shouldn't arise for a code reader.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Single‑line methods are only suitable for books, where it is necessary to squeeze the code into a limited space on paper. Since your code will probably never go onto paper, you have no need to write code so difficult to read.

Yosuf Ibrahim wrote:. . . Way neater, easily readable and more organized than this. . .

What you then posted is incorrectly indented; you need empty lines between successive methods. No wonder that isn't clear.

the smallest method should be at least 3 lines long . . .

See, three lines, and yes, I did say that. That method is now clearly indented, so everybody can see where it starts and finishes, how many statements it contains, etc. If you use Allman indentation, that becomes four lines.If you find Allman indentation easier to read, then make a policy of using it. If you are using an IDE, Junilu is right to say feed the options to your IDE and let it do all the indenting, and also that all members of a team must conform to local conventions.
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:If you use Allman indentation, that becomes four lines.


From what I've experienced many Java programmers do not use Allman indentation and many C# programmers do. I was not aware of the name Allman before this post.

With this code:

you may be fine at the start. However you may run into issues when you need to modify the code. It could then end up being something like

Now getClassName() on line 2, which has a simple if really stands out and the code does not look that tidy.
Yes a ternary operator could be used for this example to make it shorter however ternary operator are not that easy to read some times.
So now that the method getClassName() is longer does it get different indention? If so what is the rule as to what is on one line and what isn't?

Doing spacing like posted in the first example when one function is on one line can also be misleading to the next developer.
Some programming languages like COBOL, RPG IV. to name a couple, require you to have functions defined in certain columns.
So if a novice/new Java programmer picks up your code they may fell that the columns mean certain things can they may choose to continue on that way.
In doing so they may have a function/method which is all defined on one long single line.

I do agree with Campbell regarding spacing and that does appear to be the convention.

Side Note
COBOL Syntax explained:
https://www.tutorialspoint.com/cobol/cobol_basic_syntax.htm

RPG VI Sample program:
http://www.go4as400.com/data-structure-in-as400/data.aspx?cid=13
 
Yosuf Ibrahim
Ranch Hand
Posts: 271
6
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am so glad I asked this question. It bugged me every time someone told me to split my code. Now I see why you guys told me to split them up and stick to convention and now I will start bugging other people so they stick to the conventional coding styles 😁😁

Cheers
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make sure to give a good explanation for coding styles.
 
Yosuf Ibrahim
Ranch Hand
Posts: 271
6
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Make sure to give a good explanation for coding styles.



Yes boss
reply
    Bookmark Topic Watch Topic
  • New Topic