|
Oh, yes, I remember those days. Comments were out of the question since your source / the interpreter /the compiler had to fit into those 4 kb. And the same went for longer variable names, if the language supported them at all. But since then we literally got a million times as much memory at our disposal. Priorities have changed and now I also prefer clarity.
A while ago he asked me what he should have printed on my business cards. I said 'Wizard'.
I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.
|
|
|
|
|
Certainly true.
I think the real issue is whether or not the original line has any more clarity than the one I prefer coded up. IMO it does not. What part of "logical expression yielding a boolean result" is tough for any programmer to read and understand?
At some point, verbosity for clarity's sake can become ridiculous.
modified on Tuesday, September 16, 2008 11:02 AM
|
|
|
|
|
I have to agree.
m_boolVar = intVar != 0; is much clearer and easier to read, IMO.
Bill W
|
|
|
|
|
In fact I just had the very same situation today. A call to a SAP-Websewrvice yielded an integer as result. The function in which the web method was called was required by an interface to return a boolean value for success or failure.
In my case it came down to something like this:
...
ReturnValue = SAPWebserviceProxy.Z_XXXXXX(...);
ErrorFlag = IsError(ReturnValue);
if(ErrorFlag)
{
LogSAPError(ReturnValue);
}
else
{
LogSuccess(.....);
}
return ErrorFlag;
This is by no means great code, but at least everybody should be able to see what's going on. It's alwys a little awkward, no matter what you do.
A while ago he asked me what he should have printed on my business cards. I said 'Wizard'.
I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.
|
|
|
|
|
I would have no problems with how you coded that up.
|
|
|
|
|
Thanks
In any case, a simple method like
public static bool IsError(int ResultValue)
{
return ResultValue != 0;
}
will do the trick nicely. Such methods may appear a bit ridiculous as well, but actually the compiler should inline them when optimizing the code. And in cases when the return values represent not only success or failure, but also warnings, correctable errors, critical errors, fatal errors and who knows what else, a method to classify the result is much better than dealing with this redundantly in the code every time it's needed.
A while ago he asked me what he should have printed on my business cards. I said 'Wizard'.
I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.
|
|
|
|
|
geoffs wrote: For me, verbose code is actually painful to read
It's a question of striking the right balance. I often find that too many parentheses makes code harder to read in cases where removing those parentheses would cause no human ambiguity.
e.g., I prefer
if (a == b || c == d)
to
if ((a == b) || (c == d))
Similarly I prefer
return a == b;
to
return (a == b);
Kevin
|
|
|
|
|
heh
Actually, I blame people like for cryptic names of C++ functions...
I know a few decades ago consiness was important, but I don't like that in C++ programmers still do this, even though you can easily use self explaining names these days and aren't limited by memory size.
|
|
|
|
|
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
|
|
|
|
|
Exactly. If a programmer is conversant in c/c++, and where I work they wouldn't have gotten hired if they weren't, the != version is both intuitive and easier to read (IMO).
|
|
|
|
|
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!
|
|
|
|
|
Thank you... for explaining in better terms what I was feeling when I made the original post.
|
|
|
|
|
Amazing - CP is apparently not much different from Usenet. No matter how inoffensive a statement might be, you find someone who will take offense.
How in heck can what I have stated in the previous message have caused someone to vote it down?
|
|
|
|
|
Let me get this right, you're asking about how stupidity can exist on the internet. The only type of question more likely to get a yes answer than 'can there be anyone on the web that stupid/ignorant/obnoxious' is 'can there be anything on the web that gross/perverse/obscene'.
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
|
|
|
|
|
Not to stray too far off topic but what's the "scandinavian language roots" you are referring to..? I'm Norwegian and have no clue what it might be.
|
|
|
|
|
Corruption of the verb nigle.[^]
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
|
|
|
|
|
Thanks! Interesting, and potentially good South Park material. I've got to say though that "å nigle" isn't exactly everyday Norwegian in 2008. A quick google search for "nigle site:.no" returned 15 results, in most of those it was a name, some of them were Danish texts (though on .no sites), though there was ONE page that used it as a verb: Ka du står der å nigle ætter? (What are you standing there [å nigle] after?)
|
|
|
|
|
Not overly surprising, the US's major wave of immigration from Scandinavia was a century ago.
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.
Easy to read for someone who doesn't understand what booleans are, yes. I don't want someone coding for me who doesn't understand booleans - they probably don't understand much else, either.
The third expression the original poster gives is the kind of thing that C programmers do all the time, but I don't think it's really appropriate for an application that has to be maintained by programmers of indeterminate skill. However, the first expression simply exposes the one who wrote it as ignorant.
|
|
|
|
|
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.
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...
|
|
|
|
|
Oops , you're right. Sorry. It's 9:12 am here and I'm still awake. I think I should sleep .
Eslam Afifi
|
|
|
|
|
For me
<blockquote class="FQ"><div class="FQA">geoffs wrote:</div> m_boolVar = intVar != 0;</blockquote>
is the bestest style.
Generally != is a better option then == .
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
Thanks for the vote of confidence!
On another point, I was with you until that last statement. Wouldn't the choice of '==' versus '!=' be completely dependent on what comparison you were trying to make? Generally isn't a term I would apply here.
|
|
|
|