Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

    if (status != 0) return -1; // return on error
I always get angry when I find comments like this in our codebase.


This isn't as bad as it could get because it's using magic numbers. If it read:

  if (status != SUCCESS) return ERROR; // return on error
then you'd have a problem.


To play devil's advocate (and be really nitpicky), it doesn't say "return an error" but "return on error" which does contain some useful information: That this method doesn't have any kind of sophisticated error handling but simply aborts on error.


I don't think that's very useful - you see that it doesn't have any kind of sophisticated error handling simply by looking at the method's source code (which you need to do in order to see the comment).


This is ideal, yes. Would also settle for a previous comment explaining what each int represented, if the language in question doesn't have enums.


0 and -1 as status numbers aren't that unusual to call magic.


Why would a comment like that make you angry?


Because it re-states what the code is saying, which makes it two things you wind up reading to absorb one fact. It also create two update points if that code needs be changed.

I prefer that authors spend time to make sure the code is clear about what is happening, and use the comments to tell me why it's happening, and that too only when it's not obvious, or counterintuitive, or there for a special requirement.

Just wanted to point out that comments are not to be confused with documentation as comments, like Javadoc, Godoc strings. These need to be complete and verbose, especially if they code is a library or public.


It doesn't.

-1 could mean you're returning a success. I mean that would literally be true in Forth.


You're showing another problem with these comments: they're often ambiguous.

Specifically, the message says "return on error", which to me doesn't mean it returns an error, but to you it did.


If the function returns magic numbers then that should be stated in the documentation comment.


It just seem quite petty thing to get angry about. Reading those two things takes less then additional half second and on update you can delete the comment if you don't like it. I have seen project behind schedule and it was never because of tiny things like this.

Also, you are likely to find out that comments like this are often result of people just auto-writing them at the moment e.g. writing it was faster then reflect on it. Which is not necessary good thing, but it is saving time in expense of little bit less effective code.


The comment is more a symptom than a cause of problems. If useless comments are added, they’re often an excuse for lazy code, in that it allows a way to say I can’t be bothered to write this better, I’ll just add a comment instead.

Regarding time saving, I seriously doubt that code quality is proportional to time spent typing. Instead of typing two things I would prefer the author spent more time thinking and typing just one thing.


I had a coworker who would write comments like that, it wouldn’t exactly anger me, but it was a bit strange. Why would you go through the trouble of writting a comment like that, but not leave any hints as to what the devil the 50 lines of spagetti code above does? Because that would always be the case: some obvious comment, and nothing for the horribel or complicated stuff.


(they didn't understand what their 50 lines of spaghetti did...otherwise they would've written a pithy comment explaining it.)


Indeed. "// This was copy and pasted from Stack Overflow. I have no idea how it works." doesn't come across as very professional.


I've on rare occasion written a comment along the lines of:

// based on stackoverflow.com/questions/201323/how-to-validate-an-email-address-using-a-regular-expression

because I'm not going to pretend to have written this monster myself:

(?:[a-z0-9!#$%&'+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'+/=?^_`{|}~-]+)|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])")@(?:(?:[a-z0-9](?:[a-z0-9-][a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-][a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

I've only ever done it with stackoverflow because:

1. I think they'll be around for a long time, and 2. they have that policy of explaining answers on the page rather than just referring to external links (yes - irony not lost on me).

Any future dev looking at my 3-line validateEmailAddress() function who wants to learn about it can go to the site and read about it, and maybe find there's a newer status quo way of validating email addresses.

I suppose you could argue that I should copy the explanation of what the regexp does into the comment but really it's just asking for it to become redundant and if someone really wants to get into it they'll probably benefit from reading the entire up-to-date discussion.

Admittedly this is a very specific kind of situation, but this doesn't feel unreasonable or unprofessional to me (bearing in mind that I'm not generally a copy-paster, I prefer to write my own code but in the case of things like email validation I think it's more professional to find an established algorithm than it is to roll your own, unless you have a specific reason for doing so).


If I cut and paste some code, even if not from Stack Overflow I'll document it. Partly because I want to attribute the code correctly, but also so that someone later can go back to the original source. The original source may have additional documentation, explanations, assumptions, etc. that people in the future may want to know. Plus it helps to know that this block of code is an external cut and paste and that modifying it should be done more carefully than other code.

This tends to only be for tricky or time-consuming algorithms. RGB to HSL/HSV, is point in a polygon, hash function, etc. Those point-in-polygon functions in particular can be densely optimized to the point that it is not obvious a) how it works, b) what the original algorithm was.


I believe that comment is required by the licence terms of code cribbed from Stack Overflow, though.


The same person that would be able to write readable understandable comment about that horrible and complicated stuff would be also able to refactor it and make it clearer.

That is why. The person left comment on literally one thing in that code he is able to explain.


Because it adds no value


Because comments like that accompany the most trivial bits of the codebase, while the thousand line monkey-patched crap has zero comments.


I sigh often when I read (bad) code. I don't get angry. That, to me, seems to be a bit much. I'm not trying to be judgemental, I just truly don't understand why anger.


Perhaps someone could write a program that writes trivial comments like that. It would allow me to tell my boss that my code is commented :)


Error handling is one of the areas where ime more comments would frequently be in order, though not in your trivial snippet.


    i++; // Increment by 1




Consider applying for YC's Winter 2027 batch! Applications are open till November 2.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: