To me this is a coding horror, and to you? [modified]
-
First, let me say that the code excerpt below is not an egregious violation (and maybe not a violation at all), but it is a coding horror for my programming style and I am curious as to what the others here think about it. So, in reviewing a coworker's code I come across the following line:
m\_boolVar = (intVar == 0 ? false : true) ;
Yes, parenthesization and spacing exactly as shown above. Were it my code, it would have been written as:
m\_boolVar = intVar != 0; // (corrected from == 0 by GDavy -- I typed too fast!
...or if I was feeling in a bit more perverse mood:
m\_boolVar = !!intVar;
There were much bigger fish to fry in this code, but there are times when I just can't let things like this go by. These things are like misspelled words that shout out at me from among the surrounding text.
modified on Tuesday, September 16, 2008 3:02 AM
-
First, let me say that the code excerpt below is not an egregious violation (and maybe not a violation at all), but it is a coding horror for my programming style and I am curious as to what the others here think about it. So, in reviewing a coworker's code I come across the following line:
m\_boolVar = (intVar == 0 ? false : true) ;
Yes, parenthesization and spacing exactly as shown above. Were it my code, it would have been written as:
m\_boolVar = intVar != 0; // (corrected from == 0 by GDavy -- I typed too fast!
...or if I was feeling in a bit more perverse mood:
m\_boolVar = !!intVar;
There were much bigger fish to fry in this code, but there are times when I just can't let things like this go by. These things are like misspelled words that shout out at me from among the surrounding text.
modified on Tuesday, September 16, 2008 3:02 AM
-
Uhm I think your code would have a bug.... You better write it this way:
m_boolVar = intVar != 0;
-
First, let me say that the code excerpt below is not an egregious violation (and maybe not a violation at all), but it is a coding horror for my programming style and I am curious as to what the others here think about it. So, in reviewing a coworker's code I come across the following line:
m\_boolVar = (intVar == 0 ? false : true) ;
Yes, parenthesization and spacing exactly as shown above. Were it my code, it would have been written as:
m\_boolVar = intVar != 0; // (corrected from == 0 by GDavy -- I typed too fast!
...or if I was feeling in a bit more perverse mood:
m\_boolVar = !!intVar;
There were much bigger fish to fry in this code, but there are times when I just can't let things like this go by. These things are like misspelled words that shout out at me from among the surrounding text.
modified on Tuesday, September 16, 2008 3:02 AM
Your code is shorter, but the other one is easier to read. Your code is still pretty easy to read but I think readability is more important than conciseness. When you look at someone's or even your own code after a while (to find a bug or whatever), it's best if you can easily read and immediately grasp the function of the code. If everything is as concise as possibly, you often have hard to read code, which takes much longer to comprehend. So, writing easy to read code will save you time later.
-
First, let me say that the code excerpt below is not an egregious violation (and maybe not a violation at all), but it is a coding horror for my programming style and I am curious as to what the others here think about it. So, in reviewing a coworker's code I come across the following line:
m\_boolVar = (intVar == 0 ? false : true) ;
Yes, parenthesization and spacing exactly as shown above. Were it my code, it would have been written as:
m\_boolVar = intVar != 0; // (corrected from == 0 by GDavy -- I typed too fast!
...or if I was feeling in a bit more perverse mood:
m\_boolVar = !!intVar;
There were much bigger fish to fry in this code, but there are times when I just can't let things like this go by. These things are like misspelled words that shout out at me from among the surrounding text.
modified on Tuesday, September 16, 2008 3:02 AM
geoffs wrote:
m_boolVar == !!intVar;
And this one
m_boolVar = !intVar;
Although ! is shorter, I prefer to use the != way because it's clearer.
Eslam Afifi
-
Your code is shorter, but the other one is easier to read. Your code is still pretty easy to read but I think readability is more important than conciseness. When you look at someone's or even your own code after a while (to find a bug or whatever), it's best if you can easily read and immediately grasp the function of the code. If everything is as concise as possibly, you often have hard to read code, which takes much longer to comprehend. So, writing easy to read code will save you time later.
That's what people are always saying and perhaps rightfully so. However, coming from a background of programming stemming from the mid-70's when I had 4KB of memory to program with and compilers that weren't as optimizing as today's, conciseness was a virtue. After so many years it has become a habit but hopefully not to the point where I am so concise that I generate obfuscated code. For me, verbose code is actually painful to read so maybe it works both ways. And maybe there is no absolute wrong or right in this case either...
-
geoffs wrote:
m_boolVar == !!intVar;
And this one
m_boolVar = !intVar;
Although ! is shorter, I prefer to use the != way because it's clearer.
Eslam Afifi
The second example was only if I was feeling in a perverse mood. :-D However, like GDavy, you caught me in another instance of fingers not doing what my mind was thinking. I meant to type '=' and not '=='. I obviously posted this way too late in the evening (almost midnight my time). I corrected this in the original. Also, you cannot use just one logical negation because we want the receiving bool variable to receive true if the int variable is non-zero. One negation reverses the true/false sense which, for this example, means that m_boolVar becomes true if intVar is 0 and false otherwise. That is not what was desired. Time to go to sleep... :zzz:
-
The second example was only if I was feeling in a perverse mood. :-D However, like GDavy, you caught me in another instance of fingers not doing what my mind was thinking. I meant to type '=' and not '=='. I obviously posted this way too late in the evening (almost midnight my time). I corrected this in the original. Also, you cannot use just one logical negation because we want the receiving bool variable to receive true if the int variable is non-zero. One negation reverses the true/false sense which, for this example, means that m_boolVar becomes true if intVar is 0 and false otherwise. That is not what was desired. Time to go to sleep... :zzz:
Oops :doh:, you're right. Sorry. It's 9:12 am here and I'm still awake. I think I should sleep :zzz:.
Eslam Afifi
-
First, let me say that the code excerpt below is not an egregious violation (and maybe not a violation at all), but it is a coding horror for my programming style and I am curious as to what the others here think about it. So, in reviewing a coworker's code I come across the following line:
m\_boolVar = (intVar == 0 ? false : true) ;
Yes, parenthesization and spacing exactly as shown above. Were it my code, it would have been written as:
m\_boolVar = intVar != 0; // (corrected from == 0 by GDavy -- I typed too fast!
...or if I was feeling in a bit more perverse mood:
m\_boolVar = !!intVar;
There were much bigger fish to fry in this code, but there are times when I just can't let things like this go by. These things are like misspelled words that shout out at me from among the surrounding text.
modified on Tuesday, September 16, 2008 3:02 AM
For me
geoffs wrote:
m_boolVar = intVar != 0;
is the bestest style. Generally
!=
is a better option then==
.
Panic, Chaos, Destruction. My work here is done.
-
First, let me say that the code excerpt below is not an egregious violation (and maybe not a violation at all), but it is a coding horror for my programming style and I am curious as to what the others here think about it. So, in reviewing a coworker's code I come across the following line:
m\_boolVar = (intVar == 0 ? false : true) ;
Yes, parenthesization and spacing exactly as shown above. Were it my code, it would have been written as:
m\_boolVar = intVar != 0; // (corrected from == 0 by GDavy -- I typed too fast!
...or if I was feeling in a bit more perverse mood:
m\_boolVar = !!intVar;
There were much bigger fish to fry in this code, but there are times when I just can't let things like this go by. These things are like misspelled words that shout out at me from among the surrounding text.
modified on Tuesday, September 16, 2008 3:02 AM
It's just different styles of programming rather than a coding horror.
-
First, let me say that the code excerpt below is not an egregious violation (and maybe not a violation at all), but it is a coding horror for my programming style and I am curious as to what the others here think about it. So, in reviewing a coworker's code I come across the following line:
m\_boolVar = (intVar == 0 ? false : true) ;
Yes, parenthesization and spacing exactly as shown above. Were it my code, it would have been written as:
m\_boolVar = intVar != 0; // (corrected from == 0 by GDavy -- I typed too fast!
...or if I was feeling in a bit more perverse mood:
m\_boolVar = !!intVar;
There were much bigger fish to fry in this code, but there are times when I just can't let things like this go by. These things are like misspelled words that shout out at me from among the surrounding text.
modified on Tuesday, September 16, 2008 3:02 AM
Not a horror, in my opinon. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
First, let me say that the code excerpt below is not an egregious violation (and maybe not a violation at all), but it is a coding horror for my programming style and I am curious as to what the others here think about it. So, in reviewing a coworker's code I come across the following line:
m\_boolVar = (intVar == 0 ? false : true) ;
Yes, parenthesization and spacing exactly as shown above. Were it my code, it would have been written as:
m\_boolVar = intVar != 0; // (corrected from == 0 by GDavy -- I typed too fast!
...or if I was feeling in a bit more perverse mood:
m\_boolVar = !!intVar;
There were much bigger fish to fry in this code, but there are times when I just can't let things like this go by. These things are like misspelled words that shout out at me from among the surrounding text.
modified on Tuesday, September 16, 2008 3:02 AM
-
Your code is shorter, but the other one is easier to read. Your code is still pretty easy to read but I think readability is more important than conciseness. When you look at someone's or even your own code after a while (to find a bug or whatever), it's best if you can easily read and immediately grasp the function of the code. If everything is as concise as possibly, you often have hard to read code, which takes much longer to comprehend. So, writing easy to read code will save you time later.
Megidolaon wrote:
Your code is shorter, but the other one is easier to read.
I disagree, The simple != is more immediately clear than the trinary expression. The latter takes longer to read, and the unnecessary verbosity triggers a doublecheck to make sure I didn't misread something making it even worse.
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall
-
First, let me say that the code excerpt below is not an egregious violation (and maybe not a violation at all), but it is a coding horror for my programming style and I am curious as to what the others here think about it. So, in reviewing a coworker's code I come across the following line:
m\_boolVar = (intVar == 0 ? false : true) ;
Yes, parenthesization and spacing exactly as shown above. Were it my code, it would have been written as:
m\_boolVar = intVar != 0; // (corrected from == 0 by GDavy -- I typed too fast!
...or if I was feeling in a bit more perverse mood:
m\_boolVar = !!intVar;
There were much bigger fish to fry in this code, but there are times when I just can't let things like this go by. These things are like misspelled words that shout out at me from among the surrounding text.
modified on Tuesday, September 16, 2008 3:02 AM
Totally agree - it *is* a coding horror. Anyone who uses a ternary of type boolean is a twit.
-
Your code is shorter, but the other one is easier to read. Your code is still pretty easy to read but I think readability is more important than conciseness. When you look at someone's or even your own code after a while (to find a bug or whatever), it's best if you can easily read and immediately grasp the function of the code. If everything is as concise as possibly, you often have hard to read code, which takes much longer to comprehend. So, writing easy to read code will save you time later.
Megidolaon wrote:
Your code is shorter, but the other one is easier to read.
How on earth can it possibly be easier to read??? His code is a subset of the code you claim is "easier to read". In order to understand the ternary you need to understand the first expression (the condition), as well as the ternary itself, as well as the assignment operator. His code makes for less to read, and is clearer (one less step in deducing what value the variable ends up having under various conditions). Anyone who uses a ternary of type boolean is a twit!
-
Megidolaon wrote:
Your code is shorter, but the other one is easier to read.
I disagree, The simple != is more immediately clear than the trinary expression. The latter takes longer to read, and the unnecessary verbosity triggers a doublecheck to make sure I didn't misread something making it even worse.
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall
-
Megidolaon wrote:
Your code is shorter, but the other one is easier to read.
How on earth can it possibly be easier to read??? His code is a subset of the code you claim is "easier to read". In order to understand the ternary you need to understand the first expression (the condition), as well as the ternary itself, as well as the assignment operator. His code makes for less to read, and is clearer (one less step in deducing what value the variable ends up having under various conditions). Anyone who uses a ternary of type boolean is a twit!
-
For me
geoffs wrote:
m_boolVar = intVar != 0;
is the bestest style. Generally
!=
is a better option then==
.
Panic, Chaos, Destruction. My work here is done.
-
It's just different styles of programming rather than a coding horror.
-
Yeah, thanks, I typed too fast. Sometimes (as you get older) what you are thinking doesn't quite translate into what you are typing! :sigh:
geoffs wrote:
as you get older
Don't worry about it, I can have that too, and I'm not that old yet (little over 30). And luckily not everything I think is being translated into what I'm typing ;) As to your original post, I completely agree with you. I have the unlucky position to have to tackle code that has multiple gems like these. Is the logical expression that difficult to read for some that they have to literally assign a 'true' or 'false'? Or perhaps the coder fears that the logical expression returns a different kind of bool than true or false? I myself find it bad code-practice.