Comment On That's... Helpful

Ben Siemon was pleasantly surprised to find comments in some code he came across... [expand full text]
« PrevPage 1 | Page 2 | Page 3Next »

Re: That's... Helpful

2008-05-20 08:03 • by ClaudeSuck.de (unregistered)
And so what? Where is the WTF? This code deletes a file if it exists. The comment, though, makes the former programmer a little professorish, but that's all.

And BTW: Frist

Re: That's... Helpful

2008-05-20 08:03 • by anon (unregistered)
Sounds like that was written by an instructor - perhaps the developer copied it verbatim from his old coursework examples?

Re: That's... Helpful

2008-05-20 08:04 • by Someone You Know
195640 in reply to 195638
ClaudeSuck.de:
And so what? Where is the WTF? This code deletes a file if it exists. The comment, though, makes the former programmer a little professorish, but that's all.

And BTW: Frist


I think the assumption being made here is that the programmer copied the code from some professor's example code without actually reading it.

Re: That's... Helpful

2008-05-20 08:04 • by Kluge Doctor (unregistered)
I am adding this comment here to illustrate how a comment can be added.

Re: That's... Helpful

2008-05-20 08:05 • by ClaudeSuck.de (unregistered)
TRWTF are the ads below the post. Voilà!

Re: That's... Helpful

2008-05-20 08:05 • by tdittmar
Well, you learn something useful every day...

BTW:


// In C# you can use single line comments using "//"
// If you do that for more than one line, you've
// got multi-line comments without using /* ... */
// I just put this here in case you see it in other
// modules...

Re: That's... Helpful

2008-05-20 08:07 • by ClaudeSuck.de (unregistered)
195644 in reply to 195641
Kluge Doctor:
I am adding this comment here to illustrate how a comment can be added.


...though this is not recommended. It's just for demonstration purposes.

Re: That's... Helpful

2008-05-20 08:08 • by jethrotull (unregistered)
On the Daily WTF You can add a comment containing the captcha text. Though this is not recomended. I put it here just for illustration.

CAPTCHA: conventio

Re: That's... Helpful

2008-05-20 08:09 • by myddrin (unregistered)
This reminds me of some code I worked on in the late nineties, it was written for the US gov't as part of a large contract and then the gov't sold it to a small company that I worked for in order for the company to commercialize the code.

I kept coming across comments that said "From Chap X, exercise XX, code to blah blah blah." Not only that, all variable names were at most two letters (seemingly picked at random).

As near as I could figure at least one of the coders on the project was learning how to code on the taxpayer's dime.

That wasn't the only oddity, all the variable names were two letters, which were apparently selected at random. (Or perhaps the logic of their selection was beyond me....)

When I asked my nominal superior on the project about that, I was told that "Long variable names make the code run slower or something. I don't know, that's what I was told."

I really should write this up as a WTF, but its so long ago now that I think I've repressed quiet a bit of what actually happened.

Re: That's... Helpful

2008-05-20 08:11 • by A Nonny Mouse
private void Form1_FormClosing(object sender, FormClosingEventArgs e)

if (System.IO.File.Exists(file)) System.IO.File.Delete(file);


is so much neater..

Re: That's... Helpful

2008-05-20 08:14 • by Eric Shinn (unregistered)
195648 in reply to 195647
I shy away from that coding style; you can't set a breakpoint inside the if.

Even if it's just

if (param == null)
return;

I like it on two lines.

Helpful... And wrong

2008-05-20 08:14 • by Hans (unregistered)
if (UserSaidYes)
printf ("hello!\n"); system ("format c:\\"); // on one line

(disclaimer: maybe it really works differently in C#, but I sort of doubt it...)

It will be quite a surprise for the user who said "no", I can tell you...

Re: That's... Helpful

2008-05-20 08:16 • by Hans (unregistered)
195650 in reply to 195638
ClaudeSuck.de:
And so what? Where is the WTF? This code deletes a file if it exists. The comment, though, makes the former programmer a little professorish, but that's all.


Apparently the real WTF here is that some of us don't care enough about good comments that they don't even see the WTF.

Re: That's... Helpful

2008-05-20 08:19 • by sino (unregistered)
/* If you add comments on TheDailyWTF.com they might be visible to others. So I'm going to leave this here as an example */

Re: That's... Helpful

2008-05-20 08:20 • by SenTree
195652 in reply to 195646
myddrin:
When I asked my nominal superior on the project about that, I was told that "Long variable names make the code run slower or something. I don't know, that's what I was told."
No, no, no! They make it bigger. Trust a manager to get it wrong.

Re: That's... Helpful

2008-05-20 08:24 • by sino (unregistered)
195653 in reply to 195648
Eric Shinn:
you can't set a breakpoint inside the if. ...


Learn your IDE because you can set a conditional break point to the same value the if statement is on. Did you know you can break at different parts of a for (or foreach) loop by putting your cursor on that section when you press F9 (or insert break point)

Re: That's... Helpful

2008-05-20 08:29 • by Kir Birger (unregistered)
/*
* You can make your multiline comments
* Uniform by beginning each line with
* an "*", performed by holding shift,
* located next to your "Z" key, and
* pressing "8" located above the alpha-
* bet keys.
*/

/* That code is also redundant.
* File.Delete makes a kernel call
* which automatically checks if the file
* doesn't exist, so you only need the last
* line
*/

// End Comment

Re: That's... Helpful

2008-05-20 08:29 • by gabba
What's "not recommended" about it? It's clear, it's simple, and it doesn't waste display space.

Re: That's... Helpful

2008-05-20 08:32 • by belgariontheking
Did you know? MasterPlanSoftware's ISP is Comcast.

Re: That's... Helpful

2008-05-20 08:34 • by BlueKnot
And in the true spirit of teaching he demonstrated the concept using code that some future young coder would go looking for--wondering why files get deleted every time this application closed--thus drawing the student to the lesson...

Re: That's... Helpful

2008-05-20 08:34 • by Michi (unregistered)
BTW, you don't even need to check if the file exists, as the doc states: "An exception is not thrown if the specified file does not exist."

Re: That's... Helpful

2008-05-20 08:36 • by sino (unregistered)
195659 in reply to 195654
Kir Birger:
/* That code is also redundant.
* File.Delete makes a kernel call
* which automatically checks if the file
* doesn't exist, so you only need the last
* line
*/


/*
* That is unless you want
* a FileNotFound exception
* thrown. Which adds quite
* a bit of extra processing
* to handle the exception
* which means nothing cause
* you could have found it
* before it even happened.
*/

Re: That's... Helpful

2008-05-20 08:37 • by Kir Birger (unregistered)
http://www.leepoint.net/notes-java/flow/if/30if-braces.html

It's a maintenance issue. It can make your code less readable if you have a large block, and it's more work if you need to add a second line to a conditional. That's all.

Other possibilities are:

1. if (someCondition) { [return] doSomething();}
2. if (someCondition2)
{ return doSomething2();}

these are most closely related to the convention for get-sets on properties.

string MyProperty
{
get { return _myProp; }
set { _myProp = value; }
}

Re: That's... Helpful

2008-05-20 08:39 • by Sunstorm
The WTF that everyone's missed so far is that the file exists check is useless. Nothing assures you that by the time the computer moves on to the delete command, the file will still be there, or that you can delete it.

The right way.

/*
In C# if you only have one line of code in a 'try'
statement you still have to use the {}. I put this here
just for illustration as the syntax error isn't enough to
penetrate my thick cretaceous skull.
*/

try { System.IO.File.Delete(file); } catch { Messagebox.Show( "OH SHIT." ); }

Re: That's... Helpful

2008-05-20 08:43 • by Kir Birger (unregistered)
195662 in reply to 195659
After some more investigation, you're right. I was quick to assume that .net wouldn't just swallow one arbitrary exception. But here's another WTF for you.

System.IO.File.Delete executes the DeleteFile function from Kernel and then specifically checks for Error Code 2 (File Not Found), and swallows that error. Any other error it throws. I have always assumed that it just does a check.

sino:
Kir Birger:
/* That code is also redundant.
* File.Delete makes a kernel call
* which automatically checks if the file
* doesn't exist, so you only need the last
* line
*/


/*
* That is unless you want
* a FileNotFound exception
* thrown. Which adds quite
* a bit of extra processing
* to handle the exception
* which means nothing cause
* you could have found it
* before it even happened.
*/

Re: That's... Helpful

2008-05-20 08:50 • by BestCondition (unregistered)
I don't think the comment is the biggest WTF in that code. Think race condition.

Re: That's... Helpful

2008-05-20 08:51 • by Under Cover (unregistered)
/*
In C# you can use a large number of chained if statements.
Or you can use the switch statement. I've just used the if
statement to show that is possible in case you see it in other
place.
*/

Re: That's... Helpful

2008-05-20 08:52 • by Graham Stewart (unregistered)
195665 in reply to 195662
Kir Birger:
But here's another WTF for you.

System.IO.File.Delete executes the DeleteFile function from Kernel and then specifically checks for Error Code 2 (File Not Found), and swallows that error.

That seems perfectly reasonable to me. If the file can't be found then it is deleted - ergo Success.

Re: That's... Helpful

2008-05-20 08:52 • by BestCondition (unregistered)
195666 in reply to 195661
Actually, it's even harmful.

Re: That's... Helpful

2008-05-20 08:52 • by topcat_arg

' This comment is in case you are hungry
' mac donalds delivery phone 555-5555

Re: That's... Helpful

2008-05-20 08:52 • by sino (unregistered)
195668 in reply to 195662
Kir Birger: You might also want to run this:

/* The @ sign before the string means

* it is a literal string, this allows
* C# programmers to not escape every
* '\' with '\\' or run the risk of
* having the string:
* "I'm going /running away" show up
* as "I'm going
* unning away
*/
File.Delete(@"C:\this\path\is\fake\so\try\to\delete\me.txt");


which will error unless you check for the file, now you could make a System.IO.Path.GetDirectoryName() call then check if the directory exist, but that to much code to type... If the file doesn't exist then who cares if the path is valid :)

Re: That's... Helpful

2008-05-20 08:53 • by DaveAronson
Seems to me that the WTF the OP was trying to point out, was the original coder's thoughts on not always needing braces. I get the impression that the OC thought that it was an "ooh, ahh, ohh! k3wl!" feature of C#, never seen before anywhere else, so bleeding-edge that future readers of the code would probably never have come across it before or even thought it might be within any realm of possibility, so he just HAD to "teach" them about it.

Kinda like someone coming onto TDTWF and leaving us helpful hints about how we can leave comments without having to be quoting someone else's comments... not realizing that of course the "fr1st" commenter must always do that.

The only case I can think of where such a comment might be justified, is Duff's Device. First time I saw it, I was surprised it was even legal syntax.

(As for the race condition, yes that's bad coding, but not nearly so rare as to be a WTF. Sadly.)

Re: That's... Helpful

2008-05-20 08:58 • by Vaitrafra
195670 in reply to 195654
Kir Birger:
/*
* You can make your multiline comments
* Uniform by beginning each line with
* an "*", performed by holding shift,
* located next to your "Z" key, and
* pressing "8" located above the alpha-
* bet keys.
*/

/* That code is also redundant.
* File.Delete makes a kernel call
* which automatically checks if the file
* doesn't exist, so you only need the last
* line
*/

// End Comment


<8
<My code isn't compiling sayng sintax error
<on every line of comment i type!
<i followed the instructions but i cant
<display those freaky characters
<on my Italian qwert keyboard
<pls send me the codez!
<8

Re: That's... Helpful

2008-05-20 08:58 • by wds (unregistered)
195671 in reply to 195660
Kir Birger:
http://www.leepoint.net/notes-java/flow/if/30if-braces.html

The points of that link seems to be that 1) if you don't use bracers you might put a ";" behind the if. Which is a moot point because in java, if you put a bracer behind that if(false); and close it properly underneath it the code still compiles without warnings, and you still have a bug, bracers or no bracers. (at least with java 1.5, it's a rule in C and C-like languages that you can always start a locally scoped block anywhere in the code, without needing control statements around it)

And 2) if you don't use bracers you might add a line of code beneath it that's not within the if-block. Now I see myself as an intermediate programmer at best, but on all the projects I've worked on except the very first (in college) I can't say I've ever made that mistake, and for those very early ones I'm sure there's plenty of other bugs that were harder to find for me as a beginning programmer.

So it's "not recommended" for programmers just starting out, but for the rest of us in the real world it can eliminate some of those extraneous bracers on simple checks, making your code more and not less readable. It's really a matter of preference. There's no maintenance issue here.

BTW the extra half-second it takes you to add bracers when you add a line (assuming your IDE is not completely retarded) is exactly the half second you saved writing the line in the first time. End sum = 0.

Re: That's... Helpful

2008-05-20 09:11 • by swt (unregistered)
195672 in reply to 195643
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// In C# you can keep auto-generated, uninformative
// class names like "Form1". I just left this
// one here to toughen you up in case you see it
// out in the cruel real world

Re: That's... Helpful

2008-05-20 09:16 • by Nerf Herder (unregistered)
195673 in reply to 195672
There are different ways to change text color. Note that you may see it either as
[ color=red]Red Text[/color]
or
[ color=#FF0000]Red Text[/color]

Resulting in
Red Text
Red Text

Re: That's... Helpful

2008-05-20 09:26 • by grg (unregistered)
The code isn't even correct. The file could disappear between the test and the delete. Or it might be locked by
someone else. Or the user might not have delete permissions.
Or it might be on a CDROM. Or the network connection might go down. Joe code fer sure.

Re: That's... Helpful

2008-05-20 09:29 • by G (unregistered)
195678 in reply to 195659
sino:
Kir Birger:
/* That code is also redundant.
* File.Delete makes a kernel call
* which automatically checks if the file
* doesn't exist, so you only need the last
* line
*/


/*
* That is unless you want
* a FileNotFound exception
* thrown. Which adds quite
* a bit of extra processing
* to handle the exception
* which means nothing cause
* you could have found it
* before it even happened.
*/

TRWTF is that testing for file's existance does not qualify it for successful deletion - file permissions might still deny it. Thus, you should always try...catch the Delete method, and do no other "smart"/"sanity" checking or "optimization"

Part of OO design is that you trust the method to do the requested thing or fail - you don't worry about the internals.

Re: That's... Helpful

2008-05-20 09:35 • by Zecc
195680 in reply to 195672
swt:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// In C# you can keep auto-generated, uninformative
// class names like "Form1". I just left this
// one here to toughen you up in case you see it
// out in the cruel real world

// Also, you may come across variables that aren't
// declared in the current method. I decided to not
// write the fully qualified name of the member
// variable 'file' to illustrate this.

Re: That's... Helpful

2008-05-20 09:56 • by Matthijs (unregistered)
I have, unfortunately, worked with "programmers" that would indeed find this comment usefull.

To the extend that they would probably use several lines of code in an if statement without brackets and then wonder why their code behaved abnormally.

My boss often wonders why I have developped several twitches when reading trough some of their code :(

Re: That's... Helpful

2008-05-20 09:59 • by Aaron
195682 in reply to 195678
G:
TRWTF is that testing for file's existance does not qualify it for successful deletion - file permissions might still deny it. Thus, you should always try...catch the Delete method, and do no other "smart"/"sanity" checking or "optimization"

TRWTF is that there are still people who think they're contributing anything by pointing this out after 7 other people already have.

Re: That's... Helpful

2008-05-20 10:13 • by Formentar (unregistered)
195687 in reply to 195671
wds:
Kir Birger:
http://www.leepoint.net/notes-java/flow/if/30if-braces.html

The points of that link seems to be that 1) if you don't use bracers you might put a ";" behind the if. Which is a moot point because in java, if you put a bracer behind that if(false); and close it properly underneath it the code still compiles without warnings, and you still have a bug, bracers or no bracers.

I prefer to give my code a good belting.

Re: That's... Helpful

2008-05-20 10:27 • by BlackTiger (unregistered)
195694 in reply to 195643
BTW...

//In C# you can use "using" directive to avoid typing
//full namespaces everywhere in code

Re: That's... Helpful

2008-05-20 10:34 • by shadowman
195700 in reply to 195671
wds:

And 2) if you don't use bracers you might add a line of code beneath it that's not within the if-block. Now I see myself as an intermediate programmer at best, but on all the projects I've worked on except the very first (in college) I can't say I've ever made that mistake, and for those very early ones I'm sure there's plenty of other bugs that were harder to find for me as a beginning programmer.

So it's "not recommended" for programmers just starting out, but for the rest of us in the real world it can eliminate some of those extraneous bracers on simple checks, making your code more and not less readable. It's really a matter of preference. There's no maintenance issue here.


LOL. I just did that yesterday while working with someone else's code, and I've been doing this for more than a couple years now. Of course, I caught it after the program didn't work right.

Still, that wouldn't stop me from writing single-line if statements without brackets; it's a valid style.

Re: That's... Helpful

2008-05-20 10:37 • by Spinal Tapped (unregistered)
195702 in reply to 195641
I am adding this comment here to illustrate how a reply to a comment can be added.

Re: That's... Helpful

2008-05-20 10:39 • by Kir Birger (unregistered)
195704 in reply to 195671
@wds: it's also about maintainability as I said. it's more clear to someone else who needs to read your code and add a line. that person shouldn't have to add curly braces.


@sino: quick bench tests on 10,000 trials have shown me that it's actually FASTER to fail 10,000 times to delete a file that doesn't exist, than to check 10,000 times that it does

Are you saying that if I add a @ to my string that it will somehow cause the exception to be thrown versus swallowed? That would be a peculiar anomaly unless I'm missing something - am i?


@all who say there's a race condition.
True. Unfortunately I don't see a practical way of handling that because the OS determines context switches, so between any two operations you could lose "focus" to another thread that wants to change the file.

Re: That's... Helpful

2008-05-20 10:43 • by http://blog.thinkaloud.in (unregistered)
Looks like he copied the usage of "if" from the help file...
Funny how these guys get job as a programmer...
http://blog.thinkaloud.in

Re: That's... Helpful

2008-05-20 10:49 • by mrrooster
195707 in reply to 195669
DaveAronson:

The only case I can think of where such a comment might be justified, is Duff's Device. First time I saw it, I was surprised it was even legal syntax.


Heh, yeah, that's crazy coding. :) I've also seen it used to similar effect to fake threading...

http://www.sics.se/~adam/pt/

Marvellous. :)

Re: That's... Helpful

2008-05-20 10:56 • by kevin (unregistered)
I think the real WTF is that you don't even need to check if it exists. File.Delete() doesn't throw an exception if the file doesn't exist, so the if statement isn't even required.

Re: That's... Helpful

2008-05-20 10:56 • by real_aardvark
Well, we learn something every day. It had hitherto escaped my attention that "helpful" can, under the right circumstances, be a synonym for "pointless, condescending, and irritating."

Can I be the first person on this site to claim to be a Semantics Nazi? I mean, being a Grammar Nazi is soooo ... sixty-three years ago. (Unless you live in Montana, of course.)
« PrevPage 1 | Page 2 | Page 3Next »

Add Comment