• 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 some people just won't return condition

 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my short career as a programmer I saw this so many times that it made me wonder. Why some peopole just don't want to return condition?

Don't they know they can do that? That might be true for beginning beginners.
But today I saw return condition ? true : false; written by Senior Developer...
(you can tell that he indeed is senior because he was smart enough to simplify this by replacing if / else with ?: )

Another variation of this phenomenon is a tendency to compare boolean expressions to true or false.
So we have:
if (condition == true)
if (condition == false)
if (condition != true)
if (!condition == true)
if (!(condition == true)) yeah... operator precedence
if (condition != false) ... ???
if (!condition != false) ... Do I really need to continue giving examples ?

Why they just can not write if(condition) or if(!condition)? They make my eyes bleed

And as a dessert a piece of code written by fellow intern. Really good combo

What is your experience with this?
 
Ranch Hand
Posts: 35
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a beginner I do in fact understand and use the simplified if statement, but I don't think that makes me a Senior Developer .

These are probably programmers that learned things one way and never changed. There is a lot to know and I don't blame anybody for doing this.
Maybe if you ask the developer if they knew of the other way, you could understand why they do it.


maybe it is just lazy programming and it doesn't take as much thought, they are just coding away and not trying to make it elegant, just make it work?


Anyway that's my input coming from a beginner.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pawel Pawlowicz wrote:In my short career as a programmer I saw this so many times that it made me wonder. Why some peopole just don't want to return condition?



I am not defending this practice...

ok..well, maybe I am, a little, but not intentionally. When I write code, I tend to write a LOT to debug and test as I go, knowing full well that someday I will take it out again. I love System.out.println(). I can see someone doing this:



now, sure, you could put something like this in the method that is calling this code and actually getting it's returned value, but I can totally see me doing this here, too.

Then, when I clean it up, it's easier to delete two lines than re-write it as you suggest.

Just a thought...
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pawel, this kind of thing happens all the time for one reason or another. Sometimes it's just like Fred says and he's in the middle of something and just taking the scenic route, other times it's just down to lack of care and attention.

Whatever the reason, you have a couple of options:

1) If it's old code written ages ago then just clean it up as you see fit.
2) If it's recent code written by a current team member then go and talk to them, like physically go over to them if you can. Perhaps there's a decent enough reason why it's that way, perhaps the less verbose version just doesn't read well. Who knows. Whatever way that conversation goes, you'll probably both agree to change it or leave it.

You shouldn't get annoyed by stuff like this or you'll spend your whole career annoyed which will be no fun. Look at them as little refactoring challenges, get your covering tests in place and then make it better. You might even enjoy it!
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I notice that I actually make this mistake now and then.

It usually happens when I name my variables improperly. condition is a bad name for a variable, that's why I'm tempted to write if (condition == true); it's closer to natural language. What I should have done instead was if (somethingHappened).
 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I once misunderstood a piece of code thanks to one innocuous line that looked like this:

The ! and the l together - combined with the IDE font I was using and a mediocre monitor resolution - made the ! pretty inconspicuous while reading.
Ever since, I use "if (condition == false)" or its equivalent, no matter the language.
Better to make code less taxing to read for others, than save a few characters for no good reason.
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karthik Shiraly wrote:I once misunderstood a piece of code thanks to one innocuous line that looked like this:

The ! and the l together - combined with the IDE font I was using and a mediocre monitor resolution - made the ! pretty inconspicuous while reading.
Ever since, I use "if (condition == false)" or its equivalent, no matter the language.
Better to make code less taxing to read for others, than save a few characters for no good reason.




You can avoid that by avoiding if(!condition) and rearranging the code to say if(condition) instead. I find it easier to read conditions that do not contain ! and you can almost always avoid using ! in an if statement.


On a side note, I also prefer



rather than



 
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why some peopole just don't want to return condition?



I would like to know what kind of condition you are expecting to return. I am not very clear.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karthik Shiraly wrote:Ever since, I use "if (condition == false)" or its equivalent, no matter the language.
Better to make code less taxing to read for others, than save a few characters for no good reason.


But there is a good reason. It's very easy to accidentally write if (condition = false) which is valid Java code but is the opposite of what you intended.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:

Why some peopole just don't want to return condition?



I would like to know what kind of condition you are expecting to return. I am not very clear.


A condition is the result of a boolean expression i.e. anything that evaluates to true or false.
e.g.
is a boolean expression, which means you can write either

or you can simplify it to
 
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just recently finished some coding practice at CodingBat and was surprised by how much I did this. I'm gonna make a small defense of it:

A lot of the time when we're coding our thought process goes like this:

1. Check condition
2. Do something

So we have two separate steps making things nice and organized for our brain.

But when we place the return keyword first, its almost like this step process gets reversed or at the very least intertwined. In our minds we're starting to "do something" (return a value) before or at the same as we're checking a condition. Our brains are handling two steps at once, which isn't necessarily a big deal, but I think sometimes its easier to just type a little more and take things one at a time.
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tyson Lindner wrote:I just recently finished some coding practice at CodingBat and was surprised by how much I did this. I'm gonna make a small defense of it:

A lot of the time when we're coding our thought process goes like this:

1. Check condition
2. Do something

So we have two separate steps making things nice and organized for our brain.

But when we place the return keyword first, its almost like this step process gets reversed or at the very least intertwined. In our minds we're starting to "do something" (return a value) before or at the same as we're checking a condition. Our brains are handling two steps at once, which isn't necessarily a big deal, but I think sometimes its easier to just type a little more and take things one at a time.


You almost convinced me . But the case here is: you don't need to check the condition and then return something. Why not just return whatever the condition is? That's the whole point.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But sometimes what you're returning isn't a boolean. Its almost like the result of the condition being returnable is just a coincidence that allows you to shortcut your code.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tyson Lindner wrote:But sometimes what you're returning isn't a boolean.


In that case the shortcut won't work. It only works when you're using a condition to decide whether to return true or false.

can be shortcutted to

but

can't be shortcutted.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Except by using the ternary operator:
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember that ternary operator has a low precedence. It should be:
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
&& already has higher precedence so these parentheses aren't necessary.
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:&& already has higher precedence so these parentheses aren't necessary.


Shame on me! You are right!
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
CodingBat seems interesting link to practice and sharpen coding skill. thanks for pointing it. I wonder what other good sites are available similar to CodingBat.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:Except by using the ternary operator:


Well obviously, but I was talking about the short cut of removing the if/else (which is what this thread is about) rather than the short cut of writing the if/else in a different way.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:

Tyson Lindner wrote:But sometimes what you're returning isn't a boolean.


In that case the shortcut won't work. It only works when you're using a condition to decide whether to return true or false.



That's my point. If sometimes shortcutting works and sometimes it doesn't, it might be easier for you to just not shortcut at all then deal with the hassle of figuring out whether it will work or not.

But then again maybe that hassle is only a nano-second of thought to well experienced programmers.
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tyson Lindner wrote:But then again maybe that hassle is only a nano-second of thought to well experienced programmers.


When I seethen I already know what the intent of an autor of the code is.

When I see it forces me to think about the code more. I need to focus and check what the logic behind the code is.
Of course it happens ultra fast but still.... It happens.

One more think I want to add.
I totally disagree with the opinion that writing return condition; should be called a shortcut. No.
Writing this redundant if / else is taking a long way around.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tyson Lindner wrote:But then again maybe that hassle is only a nano-second of thought to well experienced programmers.


Well-experienced programmers know that good code is all about clarity, and not assuming that someone else will be clever enough to figure things out.

Pawel is correct, returning the condition is not "a shortcut"; writing a lot of unnecessary code is never correct.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I prefer the "Microsoft Windows" approach...

Sorry, couldn't resist!
 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




Between these two snippets I would quicker write the latter
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:


Seconded
Was that a serious post Rico or are you just trying to wind everyone up ?
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thirded...
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lol... exactly the type of response I was expecting.

My point is, choices of how code can be constructed should be left for personal projects... Just as with most professional undertakings there would be some sort of plan consisting of rules and guidelines to follow, so that the team undertaking the task has an objective and common understanding... so to should a software project lay out before hand rules and guidelines (code conventions) for the project that everyone should agree upon, so that everyone undertaking the task will have a common understanding... In doing so, reading through any piece of code (analogous to instruction set) that any member of the team wrote (analogous to compiler) can be processed quickly without the need for question...

There will only be two possible states: acceptable or unacceptable (1 or 0)

This is my thought on the subject matter...
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pawel Pawlowicz wrote:In my short career as a programmer I saw this so many times that it made me wonder. Why some people just don't want to return condition?

(....)



I wonder what's your problem here. You find it illegible? Below (your?) standard? Well, the general form is:

Why would you want to make an exception to this rule, just for this very specific version:

Having said that, in the ongoing Scala course, I learnt to just write: A. (indeed, it's part of the struggle, getting used to
this form of syntax ) The problem is non existent there!

Greetz,
Piet
 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Well, the general form is:


General form of what? There are lots of different things you can do within an if statement, other than return things. And there are lots of ways to return things, other than putting if statements around the returns. Your "general form" is only a general form in the context of an if/else with two returns, for cases that actually need that. There's no reason to assume it applies to all returns, or to all if/else statements.

Piet Souris wrote:Why would you want to make an exception to this rule, just for this very specific version:


Um, no, the case under discussion is actually

which is obviously simpler as


Piet Souris wrote:Having said that, in the ongoing Scala course, I learnt to just write: A.


And that, indeed, is what almost everyone here would prefer - modulo the fact that Java requires the "return" keyword.

Piet Souris wrote:[(indeed, it's part of the struggle, getting used to
this form of syntax ) The problem is non existent there!


It's still possible for people to make needlessly verbose return statements in Scala, akin to what's posted in the first example above. I've seen them, in production code. But fortunately it's much less common there, due to a combination of language and culture.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pawel Baczynski wrote:I totally disagree with the opinion that writing return condition; should be called a shortcut.


Like Bear, I agree, and I tend to avoid it if I can; with the one exception that Fred brought up: debugging.

However, I also agree with Tim: if you find yourself annoyed by something as trivial as this, you're going to spend a lot of time being annoyed.

Winston
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If this sort of coding style annoys you, you may be in for a long, painful career

Although clarity in code is paramount, there is also a case for "If it ain't broke, don't fix it". I think it is also worth remembering that at some point, your coding style is likely to annoy another developer who hopefully will also do, as Tim suggested, kindly speak to you about it in person.
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Who told you I am annoyed? I am just wondering. That's all.
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pawel Baczynski wrote:They make my eyes bleed


This doesn't sound like the statement of someone who is happy...
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Boswell wrote:

Pawel Baczynski wrote:They make my eyes bleed


This doesn't sound like the statement of someone who is happy...


But I put a winky face after that statement. You should have noticed that I was trying to be sarcastic
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pawel Baczynski wrote:But I put a winky face after that statement


Safety Wink
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

Tyson Lindner wrote:But then again maybe that hassle is only a nano-second of thought to well experienced programmers.


Well-experienced programmers know that good code is all about clarity, and not assuming that someone else will be clever enough to figure things out.

Pawel is correct, returning the condition is not "a shortcut"; writing a lot of unnecessary code is never correct.



I'm not saying its correct to write redundant code, just sometimes that's a result of following a normal step by step process.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tyson Lindner wrote: just sometimes that's a result of following a normal step by step process.


Again my point is that experienced developers aren't just going to blindly follow some color-by-number process and just blat out code that's needlessly wordy and complex without thinking about it.
reply
    Bookmark Topic Watch Topic
  • New Topic