|
It's questionable. Depends on how much readability is necessary.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
I would have scoped it:
m_boolVar = (intVar != 0);
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
John Simmons / outlaw programmer wrote: I would have scoped it:
m_boolVar = (intVar != 0);
Yeah, I do this, too. This is VERY readable to someone with sufficient competency in C# to be trusted with the maintenance of production code. The scoops highlight the boolean expression and make it a one-glance read; without the scoops, it requires a bit more concentration, which can be exhausting if you have to spend that bit of concentration 300 times while reading a single source file.
|
|
|
|
|
I think it's too trivial to fuss over.
|
|
|
|
|
dighn wrote: I think it's too trivial to fuss over.
If someone is wasting their time in writing such foolish expressions, and wasting my time by making me read it, it isn't trivial, not when I have to edit 200-300 lines of code before lunch.
|
|
|
|
|
i would go with:
m_boolVar = intVar ? true : false;
|
|
|
|
|
That would be invalid in C# or Java. Even in C/C++ I would always query against a Boolean expression for conceptual clarity.
Kevin
|
|
|
|
|
Swinefeaster wrote: i would go with:
m_boolVar = intVar ? true : false;
That combines the worst of expression one with the worst of expression three.
|
|
|
|
|
I use the style of your second example though usually with brackets around the expression.
m_boolVar = (intVar != 0);
However, recently I've been moving towards dispensing with the brackets too, partly prompted by my refactoring tool.
Kevin
|
|
|
|
|
I prefer your coworkers style . In terms of efficiency i doubt if there is any significant difference unless this is getting called repeatedly , and it is much much easier to read and understand than m_boolVar = intVar != 0; as this requires you to think about precedence. Just becasue you understand it does not mean that the maintenance programmer will . But at the end of the day its a style question , and nothing divides programmers more than that .
|
|
|
|
|
Andrew Torrance wrote: it is much much easier to read and understand than m_boolVar = intVar != 0; as this requires you to think about precedence.
If you have to think about precedence, you need to work on your skill set. I guarantee that if you were a junior I was mentoring, you wouldn't have to think about it by the time I got through with you.
|
|
|
|
|
Now that I've gotten this off of my chest, I can admit to myself that it really was more of a style issue rather than a coding horror as modern compilers would probably generate similar, if not same, code for any of the alternatives. I probably should have posted it to Soap Box or Lounge, but it is what it is at this point.
Thanks for all of your inputs. Some people agreed, some did not, and yet others were bored by the whole thing (in which case why did they even bother to reply?).
Meanwhile, my style is best...
|
|
|
|
|
geoffs wrote: Now that I've gotten this off of my chest, I can admit to myself that it really was more of a style issue rather than a coding horror as modern compilers would probably generate similar, if not same, code for any of the alternatives. I probably should have posted it to Soap Box or Lounge, but it is what it is at this point.
Don't let the idiots beat on you. It's not style, it's a clear indicator of someone uncomfortable with the language. Perhaps this is a minor thing, but I despise working with people (a) who are uncomfortable working with their language, (b) and who don't work like heck to GET comfortable.
|
|
|
|
|
geoffs wrote: m_boolVar = !!intVar;
What is so perverse with that?
To those who understand, I extend my hand.
To the doubtful I demand: Take me as I am.
Not under your command, I know where I stand.
I won't change to fit yout plan. Take me as I am.
|
|
|
|
|
Ah, a man after my own heart!
It is not perverse to me. In fact, I am quite at home with that syntax and like it. It is perverse because so many of my fellow programmers hate it and my use of it makes them froth at the mouth or sit there with a dumbfounded look because they don't understand what it is doing.
|
|
|
|
|
geoffs wrote: because they don't understand what it is doing
I hate people who use C to program in Java.
To those who understand, I extend my hand.
To the doubtful I demand: Take me as I am.
Not under your command, I know where I stand.
I won't change to fit yout plan. Take me as I am.
|
|
|
|
|
leonej_dt wrote: geoffs wrote:
m_boolVar = !!intVar;
What is so perverse with that?
Dependence on zero values being interpreted as false and non-zero as true is a particular feature of C-style languages. It confabulates boolean and integer types, which was OK in C (which has no native boolean type) but is highly inappropriate for any language that does support boolean.
|
|
|
|
|
cpkilekofp wrote: It confabulates boolean and integer types, which (...) is highly inappropriate for any language that does support boolean.
What is a boolean when the computer understand data in 8 bit chunks?
|
|
|
|
|
leonej_dt wrote: What is a boolean when the computer understand data in 8 bit chunks?
I know this is marked as a joke....but what IS the joke??
|
|
|
|
|
cpkilekofp wrote: but what IS the joke??
What's the need for such an abstraction as a "boolean"?
|
|
|
|
|
leonej_dt wrote: What's the need for such an abstraction as a "boolean"?
I should think it's obvious. Booleans represent either true or false, yes or no. "Is the door open? yes." Using a boolean type, you're guaranteed that the value of an instance of that type will be one of two choices, true or false. Any other questions?
|
|
|
|
|
And what's the difference between an 8-bit boolean and an 8-bit char whose values are restricted to 0 and 1? I still don't get it.
|
|
|
|
|
leonej_dt wrote: And what's the difference between an 8-bit boolean and an 8-bit char whose values are restricted to 0 and 1? I still don't get it.
What does the restriction? Either the language provides facilities for doing it, or the programmer has to provide the facilities. If its in the language, it can be taught in a standard way and understood in a standard way. If the programmer provides it, the programmer has to explain it, and only those with access to the programmer or his documentation will understand it. Why else develop languages in the first place? If everyone in the world had the ability to program effectively in machine language, we'd all be doing that.
|
|
|
|
|
When I program (I don't know about other people), my priorities are, in decreasing order:
1. the program has to be as fast as possible
2. the compiled executable has to be as small as possible
3. the source code has to be as small as possible
Any sufficiently good C programmer knows what the double negation does. But, if you can't quite get it, you can define a macro
#define IS_NOT_ZERO(a) !!(a)
And the double negation won't bug your mind everywhere in the code.
|
|
|
|
|
leonej_dt wrote: When I program (I don't know about other people), my priorities are, in decreasing order:
1. the program has to be as fast as possible
2. the compiled executable has to be as small as possible
3. the source code has to be as small as possible
Nice, as long as no one will ever have to change your code again. Naturally, you forgot the fourth rule, which distinguishes the professional from the cowboy:
4. The code must be readable by someone with no more than six months of experience.
5. The code must be modifiable and extendable without a major rewrite.
You see, there's no guarantee that the next person to maintain your code is as sharp on all the nuances of code as you are; in fact, in any group setting, first cuts are often written by senior programmers, while modifications are often written by juniors. And, in fact, your own observation qualities fall short: this is a C# example, not a C example.
I was an expert in C programming for the first half of my career, to the point that I had the names of the standard headers and most of their standard declarations memorized. I used this trick and many others unique to C-like languages. It is, however, a cowboy trick, and if I found it in a review of your C# code, you'd be fixing it the next day. Tricks like this were often necessary in C because C didn't support booleans, but it's unnecessary in C#.
leonej_dt wrote: Any sufficiently good C programmer knows what the double negation does. But, if you can't quite get it, you can define a macro
#define IS_NOT_ZERO(a) !!(a)
Again, this is a C# example, and C# does not have macro capabilities, any more than it has pointers.
Now, in some environments the skill level required is so high, and will remain so high, that you can write everything in obscure C with no indentation and the next programmer will be able to read it at a glance. Making your employer depend on this level of skill when they don't have to is a disservice to your employer.
|
|
|
|