|
I see you are the one who write that code
|
|
|
|
|
How would you rewrite it? (I dont actually get into 'coding', I am far more an architectural engineer. )
|
|
|
|
|
Try this on for size:
bool myVar = (value < 0);
I have lived with several Zen masters - all of them were cats.
|
|
|
|
|
Is that any more concise? Dont know how many lines of assembler are in each version. The former is more explicit, it reads easier IMO (I like really readable code. The architecture is often so complex the code has to be very obvious in the kernel)
|
|
|
|
|
Define 'readable'. The problem is that 'readable' is a very subjective term. More concise it is, because an unneeded mapping bool -> bool is removed. Also, what if I just reverse true and false in at that point? It would take a while until someone spots this little treachery.
I have lived with several Zen masters - all of them were cats.
|
|
|
|
|
have you checked in in assembler to see if it is more concise?
CodeWraith wrote: what if I just reverse true and false in at that point
Why would you? You want the code to do what it should do.
|
|
|
|
|
Munchies_Matt wrote: Why would you? You want the code to do what it should do. |
Malice.
Or just beating the code into submission and later forgetting to clean up.
I have lived with several Zen masters - all of them were cats.
|
|
|
|
|
For me it's way, way better to do
value = condition
rather than
value = condition? true : false;
Because when reading you need to look at condition then you also need to check that true and false are in the correct order. Or should it be false and true?
I don't care what the assembler generates: I can about what the guy who comes after has to deal with when it's my code.
cheers
Chris Maunder
|
|
|
|
|
I guess its a personal preference thing.
|
|
|
|
|
Chris Maunder wrote: I can about what the guy who comes after has to deal with when it's my code.
Especially if that person is you in six months.
|
|
|
|
|
OK, here is why:
I prefer the 'if x, then y = a, if not y=c' readability to 'y = if x'.
It just isnt explicit when I read the code. A programer might think the second is better, because its terser, but thats the thing I have seen so often, programmers glorying in how terse, and arcane, they can make their code at the expense of good simple readability.
I prefer explicit readability. Probably because I have more than enough complexity to deal with in the architecture when working in the kernel! It is viciously complex at times.
|
|
|
|
|
var negative = value < 0;
This way, variable "negative" will be automatically initialized as Boolean, and the value will be true if value is smaller than zero, otherwise it will be false.
|
|
|
|
|
In VB only.
I prefer the 'if x, then y = a, if not y=c' readability to 'y = if x'.
It just isnt explicit when I read the code.
|
|
|
|
|
I don't see why "x = condition" is not explicit enough.
is it negative value? = is the value smaller than zero? is pretty explicit and self explanatory.
there is no need to add this redundant statement : "if yes then it's truly negative, or else it's false"; because the first expression "is the value smaller than zero?" has given us a true or false answer already.
|
|
|
|
|
I just prefer 'if x = y then c = a, else c = z'
|
|
|
|
|
|
Oh man. Fixed.
cheers
Chris Maunder
|
|
|
|
|
Well, that is not elegant (and possibly less efficient) but correct. I would say both milk and cereal in the fridge.
|
|
|
|
|
Whoever wrote that probably has a background in Javascript where you can never be sure if a comparison operator actually converts to a bool.
Latest Article - Code Review - What You Can Learn From a Single Line of Code
Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny
Artificial intelligence is the only remedy for natural stupidity. - CDP1802
|
|
|
|
|
JavaScript does make one a little twitchy.
cheers
Chris Maunder
|
|
|
|
|
I agree. Those parentheses are totally unnecessary.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
The one you posted has an extra semicolon, right?
bool myVar = (value < 0) ? : true : false;
Ternary operator only has one semicolon.
bool myVar = (value < 0) ? true : false;
But, yes, even the right syntax in this case is a waste, because the result of (value < 0) will result in true or false.
That's what you call your "Double-checking boolean code"
Here's the advanced code for superior programmers who want extreme overkill:
bool myVar = (value < 0)? true : (value > 0 ? false : true);
modified 5-Feb-18 8:28am.
|
|
|
|
|
raddevus wrote: The one you posted has an extra semicolon, right?
It did. Fixed.
raddevus wrote:
bool myVar = (value < 0)? true : (value > 0 ? false : true);
There we go! I knew someone could make the code better!
cheers
Chris Maunder
|
|
|
|
|
raddevus wrote: bool myVar = (value < 0) ? true : false;
raddevus wrote: bool myVar = (value < 0)? true : (value > 0 ? false : true);
That's the "advanced" version with the extra subtle bug that flips the result when value == 0 , right?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
"I meant to do that!"
|
|
|
|