- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Unlike Java, in .NET there is no stack trace until it is thrown.
Admin
I think you're confusing readable and understandable. The original claims made here in favor of '==true' were based on being easier to read, and I think that (a) they ring true, and (b) who gets to be the judge of what others find more readable?
I know that these tired eyes have missed a '!' on more than one occasion, especially nested inside parens as mentioned here. In my 20+ years of programming, I've always tried to steer clear of the 'punctuation-style' operators when possible, as they DO slow down reading as opposed to more lengthy operators. Notice I again said "reading", not "understanding".
One of my "rules" developed over the years is to ALWAYS remember that code is usually "write once, read many". YMMV.
Admin
This site has gone downhill in the past couple months. I cant stand these watered down WTFs anymore.
captcha: stinky, like this site's recent content
Admin
When does rookie initiation take place?
Admin
Admin
Holy excrement... why not use this method instead?
Admin
Why ==false gives away an unskilled programmer:I was an assistant teacher in beginners and intermediate programming courses at the university some years ago, and overwhelmingly, the students who felt the urge to add "== false" to their boolean expressions were the ones that didn't quite understand boolean logic. Because they weren't comfortable with the boolean logic, they felt that they somehow needed to spoon feed the compiler with verbose instructions: "now, compiler, you need to take that a==b-thing, and check whether it was false, ok?". It wasn't a conscious choice. They were the same students that didn't quite understand how to use loops, and wrote code like
and generally would write code much like in today's WTF.Today I work with many skilled colleagues, and I rarely see such redundant "explanations" for the compiler. But quite recently I visited another project, with ==false redundancies sprinkled liberally around the code base. My alarm bells rang, and sure enough: The project had next to no automated test, loads of errors, loads of copy-paste-edited code, no documentation... Is it a coincidence? Naah!
So, basically, every experience I have ever had with coding has shown that ==false and WTFiness go hand in hand.
And just to finish off: Even if I cannot judge what others find more readable, I have a pretty good guess in this case (does the first statement even do what we want it to?):
Admin
Admin
Of course, in my opinion
is the right way. has one advantage above in C/C++, the compiler won't allow you to do but it will be quite content with I have seen that WAY too often. And so hard to spot at times.Admin
Damn right, mate. Unfortunately, following this perfectly sensible rule would result in doom for 90% of the idiots who post answers here. And they're probably in the top 10% of softare professionals anyway.
Happy, happy, happy.
(Captcha: gotcha. Well, no, I'm afraid you'll have to try a little harder...)
Admin
PS Alex: WTF is "onomatopoeia"? Hell, I've got an A-Level in Ancient Greek, and even I find this one difficult to type in ..
Admin
Getting references to the epic of Gilgamesh. Now that's old!
Admin
So are you so stupid or am i so smart that i think it's perfectly readable without that "== bool"? Maybe poeple who need this only think they know the language. There's more to it. There is no spoon...
Admin
Damn, this was a reply. The forum really is the biggest wtf.
Admin
PS Alex: WTF is "onomatopoeia"? Hell, I've got an A-Level in Ancient Greek, and even I find this one difficult to type in ..
http://en.wikipedia.org/wiki/Onomatopoeia
Admin
Yes, I have heard of readability.
...Oops. According to your version of "readability", that should be:
"Yes, it is true that I have heard of readability."
Sheesh.
Admin
There are no such "systems" only bad programmers if they make FALSE to mean non-zero and TRUE to mean zero, just like writing a class where read() means write and write() means read. It will compile but won't work the way the user expects it to and will therefore be wrong.
In C and C++, boolean expressions are true if non-zero and false if 0, so comparing to false is ok (albeit bad style but comparing to true is wrong because true could be 1, -1 or a bit-mask etc and it is not guaranteed that one true value is equal to another.
Comparing a pointer to NULL (or 0) or a return value to 0 is another matter. It is probably good style here. It is bad style in my opinion to compare strings in C with a statement such as
(note it won't compile if you put = 0 here as the return of strcmp is not an l-value)
Note also that an overloaded operator == or != in C++ is not guaranteed to be defined both ways so
is not guaranteed to be defined at all just because you can do
Now for the original post, something strange I noticed was that the coder was doing a foreach loop and didn't use the loop object (match) which seems rather strange to me.
Admin
In C#,
if(someValue)
and
if(!someValue)
looks better than
if(someValue == true)
and
if(someValue == false)
That's just me.
Admin
[quote user="Jackal von ÖRF"][quote user="EvanED"][quote user="htg"]However it is less CPU overhead than executing code in a try..catch statement (well, historically in Java, I don't know about now, or C#).[/quote]
That depends on the implementation, and how often exceptions are thrown. I don't know how Java or C# does it, but it's possible (and I think at least sometimes done) to compile exceptions in such a way that if an exception isn't thrown there is NO overhead...
The overhead of try-catch blocks, when not throwing exceptions, should be a problem only when the try-catch is inside a loop/hotspot. The overhead is in entering/exiting the try-catch block, and not in running code inside the block.
Quoted from http://www.mortench.net/blog/2006/08/08/dos-and-donts-for-exception-handling-or-what-every-developer-should-know-about-the-implementation-of-exception-handling/ "Specifically, the case of overhead of try-catch-finally constructions when no exceptions occur is difficult to get rid of by compiler & virtual-machine implementers. [--] Basically this is because something like a “linked list” has to be maintained internally by the compiler or VM each time the control flow enters or exits a try-catch-finally."[/quote]
I'm not sure if that information is correct. It's possible that some JITs for Java compile code in a way that requires maintaining some overhead data structure when entering and exiting try/catch blocks, but in interpreted code this would not be necessary since the method has a table relating sections of the bytecode to catch blocks. As long as the JITted code could map offsets in the native code back to the bytecode it was compiled from, it could use this table, and thus would not have to maintain any further data structure.
At least the Sun JVM on 32-bit Windows seems to do this. I haven't had a chance to try any other architecture yet. I created a class that adds up some numbers in a loop, parsing them all from Strings with Integer.parseInt() (but they're all good, so no exceptions will be thrown). It doesn't matter whether I have a try/catch block for NumberFormatException in the inner loop, the running time for one million iterations is the same.
It would be interesting to hear if this actually is a problem on other JVM's, or other architectures.
Admin
I am not that expert at it as some of you guys are but I can see why someone would want some functions to return an exception like that. Perhaps in situation of a network connection where you are remotely executing some function on the server and wanted to make sure that no exceptions were thrown.
Admin
pndeukab gnsz qseygox iugoprvt wkdalfoe ylni knfmjstyo
Admin
Hah I don't blame the guy, trying and catching it such a pain compared to just returning a success indicator of some kind.
Admin
Absolutely. And I also know that "if (...)" and "while (...)" expect boolean expressions. Hint: Comparing a boolean variable to 'true'/'false' doesn't make it more boolean.
I find "if (a && b)" quite readable. Quick: Tell me when which branch of the following conditional will be taken:
if (! (a != true && b == !false))
I wish the programmer had just written
if (a || b)
In everyday language, do you say "If it's true that I carry an umbrella, then it's not going to rain?" Just say what you mean: "If I carry an umbrella, it's not going to rain."
"...== false" is a hint that the programmer never understood that Boolean is an algebra, and that he can simply write things like
bool ok = ((0 < x) && (x < 10));
Now if he didn't get that, what else did he miss?
Of course, when dealing with numbers instead of boolean, I'd insist on a comparison. A number or a pointer value is not boolean by concept, so I want to see
if (u > 0) /* assuming unsigned u; */
if (p != NULL) /* assuming p is a pointer */
The C interpretation "zero is false, non-zero is true" is not obvious.
Admin
This is probably obvious, but what about Java's Throwable.getCause()?
Not that most people would be implementing that, of course...