- 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
The only "real" reason why I have seen, and been told why not using braces on single-line if blocks is the maintenance issue.. I sometimes dont add braces to my if blocks because I find it more readable, why use 3 lines when you only need one? (e.g guard conditions at a start of a code block, you want to keep them small)
Now, for those that start to get worried about their delicate fingers when needing to add braces, I have come up with an incredible solution (since I worry about mine too, and have needed to add braces) its called SURROUND WITH snippets..
"Oh, I need add another line here, surround with, braces, type type type". Done.
Although to be quite honest, I am now regretting even writing this comment because I really do not give a crap.
Admin
Just out of curiosity, have you ever met someone else who refers to those symbols as "bracers" rather than "braces"? I've never heard such talk before.
Admin
I guess it avoids confusion with a dental brace?
"Did you put your braces in?" "Are you saying I have bad teeth?"
Ohhhhhh the trouble our terminology causes!
Admin
I think my very faviate comment of all time was what i found in some work my a member of my team as i was updating it.
//I have done it this way because i hate you!!
Admin
Anyone that uses a conditional statement or loop without brackets is a jerk, for example:
.
That's HORRIBLE code.
Admin
Oh, and my favorite code comment:
// Capture keys (they're fast f**kers)
Admin
BTW to the later poster, there's a difference between braceless-if with one statement beneath it and braceless-if with entire blocks beneath it. I was talking about the former.
Admin
This forum software offers the possibility to post completely useless comments. Although it is generally not recommended, I'll just post one here, so you can recognize them in the future in case you come across one in some other forum.
Admin
Yeah I know, one will compile, one wont :)
My coffee is broken, Ive been half asleep all day! :(
Admin
I've seen plenty of comments in programs that explain the programming language rather than the program. Like, I used to work on a system that was filled with examples like:
i.e. all the variable names were "n" followed by a number, and the comments were like the above.
So, uh, yeah. If I was really struggling with that obscure feature of C that "+" means "add", this might be helpful. But what I really need to know is: What in the world do those variables contain?
Admin
In some of the code that I come across, I'd be happy to find -any- comments, useless or not.
Admin
Sadly, it wasn't a manager... it was "the lead programmer". (At the time, I was the junior most developer on the project. Within 6 months, I was "the lead"...)
Admin
Once a co-worker of mine asked for help with a program that was giving incorrect results and she just couldn't figure it out. The code went something like:
I looked at it and didn't see the problem either. We ran it with a debugger and for some mysterious reason the loop just quit after one iteration no matter what the data. I spent a lot of time studying the getNextMessage() function to figure out why it would return false when there were clearly more messages in the queue.
Well, from the context of this thread and the fact that I left out all the complicated code, you probably see the dumb error -- the semicolon after the while statement. But we worked on it for hours before that finally registered on me.
The moral being that even smart people like myself and my friend can miss dumb mistakes. And that a substantial percentage of program bugs are probably that sort of dumb mistake rather than some fundamental flaw in the logic.
Admin
Comments were also often stripped out, since they (a) made the code run slower, since the interpreter would have to skip forward over the bytes and (b) since memory was at a premium, they took up too much space.
Smarter BASIC interpreters, of course, would do some optimization and caching but old habits sometimes die hard.
For instance, since I learned programming on an early variant of FORTRAN called GOTRAN, a so-called "load and go" compiler with extremely limited capabilities, I sometimes find myself overparenthesizing expressions, since, I believe, the operator precedence was strictly left to right and coding something like
would produce an unexpected result.Admin
I love how he wasted at least twice as many lines as the ones he "saved" by not using braces.
Admin
I've been known to comment every line of a program like that when learning an assembly langugage. However, that's just as a learning tool, and not intended for production code. (I don't write production code in assembly language anyway, so the chance of such comments getting in to such code is minimal.)
Admin
I wrote a comment like this once...for Javascript...mainly because most people seem to have never considered immediate execution of functions by wrapping their definition in parenthesis which makes the function the result of an expression...
Admin
I program in C
It is terse.
omments are appreciated.
Admin
Hmm, I thought 4 + 12 = 16, not 17.
Oh wait...
Admin
I definitely agree. It's also clearer.
Admin
Lol! I bet your project manager doesn't agree.
Here's an idea for extending C# though. We've already got:
// to comment a line or /// for documenting a class interface
how about a new one
//// for really patronizing comments? That way they can be automatically extracted from source files and used for... err... something.
Admin
I'm posting this comment to show how one might ask a dumb question.
So, did anyone mention that File.Delete doesn't throw an exception if the file doesn't exist?
Admin
So when I started working with javascript, I brought my VB laziness with me. It took me two hours of pulling my hair out to finally figure out why this wouldn't work:
Admin
Admin
Bet you that snippet (comment and all) was copied from a CS textbook or the like...
Admin
So it's someone from a Java background who hasn't seen this C-style idiom before. Big deal. If the developer sees something which seems odd or in need of explanation to her, she should comment it. Its better to have well-commented code with the occasional trivial comment than undercommented code with none.
Admin
The only WTF was the this programmer didn't get his "Enter" pinky amputated.
Any programmer too lazy to put braces around a single line "then" clause should be fired on the spot. It will save numerous bugs down the road.
Admin
n4 is hydrogen peroxide. n12 is Tang.
Admin
Umm...is file even defined in this function?? Does this compile???
Admin
The real WTF is the obvious race condition. By the time File.Delete is called, the file may already have been deleted by another process. So the call to File.Exists is not only redundant, but flat-out incorrect.
Admin
this beauty from our codebase.....
///
Admin
I have a co-worker that likes to copy example code and leave in the comments. We work in a custom language, so it isn't quite as bad as the example.
And he usually only does it for complicated things that most of the rest of our group don't touch. So a case could be made he left in the explanation of the rest of them.
It just leaves a bad taste in my mouth because it seems like he's just copying and pasting withouth know what he's doing, even through I know he does.
Admin
At the end of the day, I suppose it depends upon what sort of work environment you're aiming for, really.
Admin
Admin
The WTF is that between the test for existence, and the attempt to delete, something else may have happened to the file.
I don't know what C# does if you attempt to delete a file that doesn't exist - in some languages it returns an error code, others a boolean (success/failure), others throw an exception.
The correct thing to do is simply attempt to delete, and handle failure.
Admin
In assembly you can write code the controls your processor. I find this interesting.
Admin
// I // get // paid // by // the // line.
Admin
// Personally I don't like this style of comment because when // you edit your comments then you have // to go through and rearrange all the double-slashes or // asterisks or whatever you've used. This is made even worse if you // (like me) use a non-proportional font in your // editor.
In response to a couple of other comments here:
I don't think brackets of any kind are ever extraneous. At the cost of a few more presses of keys you get code that is less ambiguous and clearer to read (for both humans and compilers).
I do think anal-retentiveness is required for programming. Programming languages are picky beyond belief. Miss one semicolon and you're history. Miss a digit in a format statement and your reactor melts down.
Cheers Craig
Admin
TRWTF is surfing the Internet without blocking adverts, popups and Flash.
Admin
SOunds like projects that I've been on that counted progress in lines of code. Useless comments count as work!
Admin
LOL, recognize that only crappy coders take shortcuts and do not use proper verbose shortcuts, comment on it, then do it.
Dumbass...
Admin
One day we can only hope that Java will support such C-style intricacies as writing single line if statements without braces.
Actually, lets be realistic the chances of that ever happening are almost as slim as people one day being able to write short obfuscated programs in Perl or low level programs in Assembly.
Admin
/* If errors are found in this code, fire Claude, not me. */
Admin
this is much more neater, but you didn't mention that.
PS: don't forget to put in your name, you'll be waiting a while. And lets speed this server up a bit hey.
Admin
The moral of the story is to use a modern day compiler that will point out mistakes like this.
Admin
The real moral is to put th brace at the end of the while loop (if you want one). For example:
Admin
{ ; in C#, semicolons do not designate commas ; and if you try to use them instead of /* */ ; or // you will have errors ; I'm just putting this so you won't make that mistake. }
Admin
this is useful ... NOOOOT!
Admin
Danke Hans. If you need comments of this kind then I must doubt your programming capabilities. One of the rules for comments is that thou shalt not comment stuff which is obvious. It distracts and disturbs to have rubbish comments. Say why the piece of code is executed but don't give programming lessons in the code.
Admin
Remember the old DOS days with GW-BASIC and other flavors of it. It was quite common that variables had only two characters because that was the maximum recognicable length for the interpreter. More characters were allowed but they were not significant for the distinction of variables. Hence AB1 was the same variable as AB2 since only AB was used by the interpreter to define the name of the variable. Thus, I suppose the superior probably still had the number 2 in mind but didn't remember why only 2 had to be used. You know, after years of programming and then several years after this can actually happen.