This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Transfer Learning for Natural Language Processing (MEAP) and have Paul Azunre on-line!
See this thread for details.
Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • 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
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Code style — long return x or return y

 
Rancher
Posts: 151
21
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know well that methods which follow the pattern "if x, return a, else return b" should be written with a ternary, which is obviously very clean:All is well so far. I sometimes find myself with the same pattern, but with too much logic to fit into a ternary. In that case I often find myself writing something like this:
I tend to get the bad cases out of the way, then write the code as if they did not exist. The logic is actually still if-else, though, so I wondered if I should rather writeI favour the first; to me it  looks cleaner. I know that as a fairly green programmer my opinion doesn't count for much. What should I be doing?
 
Saloon Keeper
Posts: 11899
253
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, don't use ternary statements if you're going to return booleans from them:

I realize it's just an example though.

When using an "if - else if - else" ladder, you can omit the final else-clause if every other clause in the ladder completes abruptly. Abrupt completion means using keywords such as return, break, continue, throw and yield.

Whether you omit the final else-clause is a matter of taste. Sometimes I do it, sometimes I don't. It's a matter of how it looks in that specific case, and how you feel at the time. There is no good argument for or against it.

Here's a guideline though: Omit the final else-clause if the last statement you want to perform is the "normal" situation, and all the clauses before are exceptional situations. Include the final else-clause if it's (roughly) equally likely to occur as the clauses before.

The following alternative might force you to make your code self-documenting though: DO use the ternary operator, but simplify its condition and sub-expressions by providing more descriptive methods:
 
Jj Roberts
Rancher
Posts: 151
21
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

First of all, don't use ternary statements if you're going to return booleans from them...

Yes, that was a bad example  

Your alternative approach is interesting. Would you do this normally? I agree that is very self-documenting. It is clear what is happening without writing a specific method for validating the row number, though, isn't it? Also with the similar method for getting the column from the matrix, it becomes six methods instead of two (which may or may not be a bad thing).

Your other points are good, thank you.
 
Stephan van Hulst
Saloon Keeper
Posts: 11899
253
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jj Roberts wrote:Also with the similar method for getting the column from the matrix, it becomes six methods instead of two


I'm not aware of compilers that charge money by the method.
 
Stephan van Hulst
Saloon Keeper
Posts: 11899
253
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On most of my applications where I have to index into a non-linear data structure, I create a separate class to hold the coordinates.
 
Sheriff
Posts: 15525
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jj Roberts wrote:I favour the first; to me it  looks cleaner.


I do, too.

My first consideration is team standards, if I'm working on shared code. I might try to influence change to team standard to something I prefer but if the team prefers something else, I'll generally just go with the flow. I've had no problem with this approach since most teams usually go with the default formatting rules that their IDE uses.

On personal projects, I tend to use whatever gives me the shortest code that doesn't sacrifice clarity too much. Usually the two (short and clear) are mutually compatible.
 
They gave me pumpkin ice cream. It was not pumpkin pie ice cream. Wiping my tongue on this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic