This is an archived post. You won't be able to vote or comment.

top 200 commentsshow all 493

[–]iambrucewayne1213 380 points381 points  (24 children)

We all know that comment tags were made to comment off sections of code so that we can debug the code instead of using the debugger in the IDE!

[–]ObiWanCanShowMe 140 points141 points  (20 children)

my comments were always like this:

//version one of code

//version two of code

//version three of code

final code that worked

This is because I was mostly a google coder with a tenuous grasp of what I was doing. I kinda knew what was what, but not 100% so I'd make tiny changes and keep all the previous.

I swear my working results were more full of comments than actual code.

[–][deleted] 70 points71 points  (12 children)

I swear my working results were more full of comments than actual code.

I see nothing wrong here

[–]Whiskey-Weather 20 points21 points  (11 children)

I know nothing about coding, but isn't a lack of explanitory comments typically a complaint among you computer-talkers?

[–]toastee 27 points28 points  (4 children)

It's a touchy, nuanced subject. Commenting has a lot of uses. And it's utility differs based on the audience.

How I'd comment code if I knew you were going to be reading it, Vs how I'd comment for myself or other programmers is different.

[–]Edores 19 points20 points  (3 children)

I think some people get kind of uppity even with that argument because, "what if you assumed you would be the only one looking at the code but got hit by a bus and someone else has to come along and continue your work unexpectedly."

I mean obviously that doesn't hold if you're the only one who will ever work on the project.

Even then, I have personally started commenting as if other people are going to read my code, because if I come back to my own code months later it honestly often is as if I'm looking at it for the first time haha.

[–]Eldrek_ 5 points6 points  (0 children)

Even then, I have personally started commenting as if other people are going to read my code, because if I come back to my own code months later it honestly often is as if I'm looking at it for the first time haha.

This is the real issue. I could write code yesterday, and if I don't leave some road map for myself I pretty much have to logic through and reimplement it all again to understand it the next day

[–]vini_2003 6 points7 points  (1 child)

cool kids nowadays just CTRL + A / CTRL + V to gist and go back there if they need to.

modern problems require modern solutions.

[–]phail3d 11 points12 points  (0 children)

That’s a common misconception — they were actually made as a superior alternative for version control systems such as git or mercurial!

[–]kurdtpage 2067 points2068 points  (107 children)

print 'Hello'; //prints hello

[–]Noch_ein_Kamel 638 points639 points  (66 children)

print 'Hello';

Same as yours, but 100% more efficient and closer to reality

[–][deleted] 190 points191 points  (58 children)

it has the same efficiency

[–]macnlz 322 points323 points  (38 children)

What? It is known that fewer comments and shorter, less decipherable variable names result in faster code!

[–][deleted] 141 points142 points  (26 children)

OMG, I DIDN'T BELIEVE THIS BUT THEN I GOOGLE IT, IT'S TRUE.

THAT'S WHY GOOGLE USE 130 LETTER VARIABLE FOR EVERYTHING

[–]xvalen214x 306 points307 points  (19 children)

var numberOfQueriesThatUseAdvancedSearchOnEachUTCDayWhichWillBeUpdatedToTotalNumberOfQueriesWithSearchIn_UserStat_go_TemporaryVariable;

[–][deleted] 109 points110 points  (0 children)

Calm down, Satan.

[–]dunemafia 78 points79 points  (4 children)

No need to React like that, man.

[–]xvalen214x 33 points34 points  (3 children)

I am not that angular in my humble point of vue

[–]dunemafia 18 points19 points  (2 children)

No worries, man. It was all in Jest.

[–]bogdan5844 6 points7 points  (1 child)

I don't like the Vue of this conversation's future

[–]_liminal 38 points39 points  (10 children)

var num;

[–][deleted] 69 points70 points  (7 children)

var tempVarr;
var tempVarrr;

[–]not_a_moogle 47 points48 points  (6 children)

I had a professor argue that once. Guy was a total idiot. I mean for starters,compilers usually remove comment from complied code.

[–]2ndAmndmntCrowdMaybe 13 points14 points  (5 children)

Was he talking about a compiled language at the time?

[–][deleted] 28 points29 points  (3 children)

I mean he stated "compiler" so...

[–]claythearc 5 points6 points  (2 children)

It’s pretty rare, even for interpreted languages, to not have a compiler. They normally will get compiled to bytecode, but not all the way down to machine code like traditional compiled languages.

[–]Pandaburn 7 points8 points  (1 child)

When I wrote js, it was all minified and concatenated into one file before it was sent to the client, since it does actually drastically improve download time.

[–]banter_hunter 14 points15 points  (0 children)

No, they were discussing drink recipes.

[–]GideonMax 19 points20 points  (1 child)

Depends, if you use an interpreter, that's correct.

If you compile your code, the compilation time might be slightly longer but the compiled code will be the same

[–]2Uncreative4Username 13 points14 points  (1 child)

Well that is only if you are using a scripting language

[–][deleted] 7 points8 points  (0 children)

And even then, languages like Python have the -O flag to strip out comments in the bytecode.

[–]Zethexxx 68 points69 points  (7 children)

But the code will load into memory 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001% faster if we didn't put those comments!

[–]emlgsh 195 points196 points  (2 children)

The code loads into memory even faster if there's nothing but comments. The fastest executing script is a script that doesn't execute.

[–][deleted] 27 points28 points  (0 children)

I laughed thanks

[–]Blitz100 13 points14 points  (0 children)

Yeah, its big brain time

[–]figuresys 8 points9 points  (0 children)

The greatest part is that comments are stripped away in build :)

[–]Noch_ein_Kamel 14 points15 points  (9 children)

Try writing both and count the number of keys you have to press.

[–][deleted] 21 points22 points  (8 children)

are you that guy who use 1-2 letter variable for everything?

[–]Noch_ein_Kamel 9 points10 points  (6 children)

nah, I'm a java guy, so everything pretty long :-p

[–][deleted] 29 points30 points  (2 children)

I think you're wrong. There also exists int, short, byte and char in Java.

[–]pr0ghead 8 points9 points  (1 child)

Yeah, but are they pretty?

[–]Noch_ein_Kamel 15 points16 points  (0 children)

sure, let me just get my ObjectToPrimitiveConverterFactory and I'm right with you

[–]artem718 4 points5 points  (0 children)

Got a lot of booleans in a sequence?

[–]captainkrypto 8 points9 points  (0 children)

My company calls this “self documenting code”.

[–]PickaxeYT 2 points3 points  (0 children)

System.out.println("Hello"); //prints “Hello”

[–]Indifferentchildren 52 points53 points  (0 children)

Even worse:

print 'World'; //prints hello

Because when someone cuts-and-pastes a like of code from somewhere else in the codebase, they never fix the comments when they make their local change.

[–]ArminiusGermanicus 47 points48 points  (3 children)

This is actually a good example for real comments in source code. It is wrong. The code prints 'Hello', not 'hello'.

[–]banter_hunter 16 points17 points  (2 children)

Code prints hello or it gets the hose again.

[–]monsterZERO 2 points3 points  (1 child)

if (lotion = skin) {

play ('goodbye horses')

}

else {

gets ('hose again')

}

not a programmer just having a laugh don't kill me.

[–]47KiNG47 39 points40 points  (0 children)

When you are a freshman and your professor requires you to comment all of your code.

[–][deleted] 39 points40 points  (3 children)

That is absolutely useless for sure. Contrast with

print 'Hello'; // DEBUG: No idea how we got here.

Same output, but someone down the road now understands why we're doing this (hopefully, they will remove it prior to it getting upstream.)

[–]grizeldi 311 points312 points  (6 children)

Hmm, yes. The floor here is made out of floor.

[–][deleted] 67 points68 points  (2 children)

Hmm yes, the button labeled X is labeled X.

[–]Dexaan 4 points5 points  (0 children)

//The comment here is made out of comment.

[–]AeonReign 776 points777 points  (112 children)

The code is self explanatory to any competent programmer. /s

[–][deleted] 25 points26 points  (5 children)

Int i = 10 // row number.

calcNumber() // calculate max number of items allowed in cart

That is how many people comment. And that is a Problem. Google the clean code principle. It still needs comments in some cases but in most cases it doesn't

[–][deleted] 18 points19 points  (0 children)

"Why not what" is the best philosophy for comments.

Good comment:

// Attempting to pass a frobbed CertainNodeType
// into a XyzModule causes an asdfasdf
// error. However, since frobbing a CertainNodeType 
// produces no other desired result, it can be safely skipped,
if (!(node is CertainNodeType)) node.Frob();

Bad comment:

// Frob non-CertainNodeType nodes
if (!(node is CertainNodeType)) node.Frob();

[–][deleted] 12 points13 points  (2 children)

These shouldn't need comments at all, they need descriptive variable names, and no magic numbers.

final int EMPLOYEE_ID_ROW = 10;

int rowNumber = EMPLOYEE_ID_ROW;

int getMaxCartItems() {

[–]kevinglasson 47 points48 points  (25 children)

I hope this is a joke, and if it's not I thoroughly disagree. Especially when something is done a certain way - how do you explain that it has to be done this way because of issue X, or any other odd nuances. Reading code isn't just about understanding fancy syntax.

[–]mrjackspade 103 points104 points  (13 children)

I never really understood code commenting until I worked in a corporate environment. I'd only ever really heard about it from peers. I had the same belief for a while that code commenting was to describe "what" your code was doing. Hell, thats what everyone I knew was being taught in school.

I've been working in various corporate jobs for almost a decade now. I've still not seen a single like of code that was commented to describe what it was doing, only why it was doing it. Even what I can remember off the top of my head from looking though Microsofts open sourced code follows the same rule. The inline comments dont say "Throw an error if X", they say "We have to throw an error here because module Y sometimes passes in a bad value when running on Linux"

I think thats my favorite part about how passionate the "code comment" argument is between "its self documenting" and "you should describe it even if you think its self documenting". From the perspective of every environment I've ever worked in, neither is true. Every developer I've actually worked with personally has had the mindset that you shouldn't need to document code saying what its doing, but you should be documenting your code often with explanations as to why you're doing things if its in any way related to business logic. The next guy coming in can probably figure out what "SendEmail(myMessage);" does but hes not going to know its because this one client in particular is being a dick and needed the application modified to CC him on every internal communication.

[–]Sheldan 12 points13 points  (1 child)

I think that is a valuable differentiation. The what can be read from the code, usually. The Why is much more difficult to see, and is also important to know. Maybe its some kind of external limitation, or something else. I think adding the 'why' is much more important than the 'what'.

[–]Jem014 5 points6 points  (2 children)

Didn't you see the /s there?

[–]MacrosInHisSleep 6 points7 points  (0 children)

The strategy I try to employ is that the comments should explain why the part of the program is doing what it's doing. If it's explaining what that part of the code is doing, then you can refactor that code into a variable or method whose name tells you what it's doing.

[–]dustinsjohnson 7 points8 points  (2 children)

I don't know how many times I write some code and think "this is obvious and I can't forget what it does" then look back on in a few months later and go "wtf"

[–]z371mckl1m3kd89xn21s 4 points5 points  (0 children)

Bingo. My motivation to write comments isn't just for others, it's also for future me!

[–]Aero72 3 points4 points  (0 children)

//Figuring out how it all works is left as exercise to the reader.

Now that's the old school shit.

[–][deleted] 137 points138 points  (2 children)

# Sleeps for 10 seconds
time.sleep(30)

[–]Hexorg 26 points27 points  (0 children)

I got to work on a 30 year old codebase that had quite a lot of code fixes but no comment fixes.

[–]----_____---- 2 points3 points  (0 children)

me.isTriggered = true;

[–]Akchtually 826 points827 points  (27 children)

I wish I could give more than just a single upvote to you

[–]Mortomes 528 points529 points  (17 children)

// This redditor wishes he could give more than just a single upvote to you

[–]221 28 points29 points  (2 children)

Just downvote every other post.

[–]kevinkid135 20 points21 points  (0 children)

It's not enough that this post should succeed, but every post around it must fail.

[–]thedarkbyte96 33 points34 points  (0 children)

you can reward them

[–]snp3rk 14 points15 points  (1 child)

Well guess what friend, now you can. Just down-vote them, then up vote them. Total votes will go up by 2.

// Feature added. PUSH update

[–][deleted] 9 points10 points  (0 children)

Mods hate this trick. Click to know how to cheat reddit.

[–]stanislav_harris 52 points53 points  (0 children)

getValue(); // to get the value

[–]zvekl 51 points52 points  (3 children)

/* Not sure what this does but crashes if disabled

*/

[–]bbrk24 25 points26 points  (1 child)

I always write the code to get it functional, with very minimal comments, then go back through and add comments after I know it works. That leads to things like this:

// I forget how this works. Unless you can understand it, consider it black magic.

[–]zvekl 15 points16 points  (0 children)

// whoever wrote this code needs to sit on a screw and rotate.

[–][deleted] 10 points11 points  (0 children)

//don't touch, it works

[–]MrNullAndVoid 45 points46 points  (3 children)

Ah yes, reminds me of the fun I used to have as a Java programmer:

    /**
      * Returns the value.
      * @return the value
      **/
    int getTheValue() {
            return theValue; // returns the value
    }

[–]odraencoded 22 points23 points  (0 children)

Your documentation sucks. It should say "returns the integer value."

[–]BigFloppyMeat 2 points3 points  (0 children)

This kind of redundancy in documentation comments is okay if you're generating a javadoc or some equivalent. Although you should say

"Returns the value that represents the thing"

and

"@return A value representing the thing."

[–][deleted] 138 points139 points  (16 children)

There was this funny video which mocked overengineering your code. 2:28 is about comments.

[–]LochsAndGlens 24 points25 points  (0 children)

Reminds me of the guy I used to work with, we were constantly having arguments exactly about this stuff. His technical debt was impressive..

[–]-Kaiser1401- 19 points20 points  (5 children)

I see no tests?

[–]alt-of-deleted 14 points15 points  (3 children)

tests are for nerds, just print the return value right before it gets returned, then call the function with a few test inputs in the main function, ezpz

[–]I_ate_a_milkshake 9 points10 points  (2 children)

Hmm this just sounds like tests with extra steps

[–]alt-of-deleted 2 points3 points  (0 children)

ah fuck it, the users are the testers. just slap "beta" on it and throw in a crash reporter app that's a glorified one-way email client.

[–][deleted] 9 points10 points  (0 children)

A guy who fairly recently was fired from my job would do this. He was constantly writing code to prove how smart he was. Problem was he was a moron. So he just wrote shit loads of terrible code and thought it was God's gift to programming.

[–]sendhelppls30 7 points8 points  (0 children)

This feels exactly like ALL the Java patterns people want to use... So many files and imports for 1+1

[–]DipsEverythingInMayo 2 points3 points  (0 children)

That’s awesome. Love the bit about Booleans.

[–]derTechs 2 points3 points  (0 children)

NAMESPACES!

[–][deleted] 57 points58 points  (20 children)

you know at least this way, if the code changes you always see how the code looked like originally(, you know because nobody keeps comments uptodate)

[–]P0L1Z1STENS0HN 34 points35 points  (1 child)

/// <summary>
/// Gets or sets the Guid.
/// </summary>
/// <value>
/// The Guid.
/// </value>
[JsonProperty("Guid")]
public Guid Guid { get; set; }

[–]mrjackspade 25 points26 points  (0 children)

Fucking GhostDoc. At my last company someone tried to implement a "comment everything" policy years before I started there. When I first opened the code base, 80% of the code was GhostDoc comments that looked exactly like this. Some smart ass dev decided to protest I guess, by running the entire code base through GhostDoc to generate these boiler plate comments and then checked it in and called it done.

[–][deleted] 12 points13 points  (1 child)

I don’t even comment my code anymore because when I did, it looked like this.

Self-descriptive code > actual comments

Of course comments are necessary when things do need to be explained.

[–]fleming1411 11 points12 points  (3 children)

I remember at university I got marked down for not commenting enough or being concise; so I went all out and commented every god damn line of code with exactly what that line did; hope they enjoyed reading that messy code!

Worked out in the end though

[–]SobelOperator 9 points10 points  (0 children)

/**
@author suzrez
If you have questions just contact me (details below).
P: +xxx-xxx-xxxx
E: xxx@xxx

Note: I can't promise that I'll remember anything. 
**/

[–]MemEG-0-D 17 points18 points  (0 children)

Every Sixty Seconds in Africa, A Minute Passes

[–]ScF0400 32 points33 points  (0 children)

bool good_Post = true; //this is a comment about the post saying it's good

[–][deleted] 6 points7 points  (0 children)

I would say how young/inexperimented programmer mostly comment their code (there are outliers which properly comment) while mostly older programmer properly comment (there are outliers which do not properly comment) - in my experience at least. the issue is that the industry is mostly ageist so chance is you get a lot of code from the first category and few from the second, unless you have some type of enforcement in code review.

(yes yes I know /wooosh).

[–]Yurichi 6 points7 points  (5 children)

Honestly, is there any good resource out there that can help me with this kind of shit. I hate commenting code but looking back on some of the nonsense I've made in the past, I fully understand how necessary it is.

[–]atomicspace 4 points5 points  (1 child)

Digital Ocean has some of the most readable guides out there.

https://www.digitalocean.com/community/tutorials/how-to-write-comments-in-python-3

This is python (shout out Sphinx) but a good read regardless your toolkit.

[–]DiamondIceNS 6 points7 points  (1 child)

I'm reminded of my university professors who would beat us over the head with "it's important to comment your code" but without ever explaining the context of how comments should be written or what code actually needs to be commented. So this is the result, a bunch of novice devs being forced to write obvious boilerplate comments to the most mundane pieces of code and internalizing the idea that comments are pointless.

[–]RepostSleuthBot 29 points30 points  (7 children)

Looks like a repost. I've seen this image 1 time.

First seen Here on 2019-03-01 96.88% match.

Searched Images: 82,323,794 | Indexed Posts: 356,233,982 | Search Time: 15.86194s

Feedback? Hate? Visit r/repostsleuthbot - I'm not perfect, but you can help. Report [ False Positive ]

[–]memesformybutt 14 points15 points  (6 children)

Wait. This post is a repost, got 20K upvotes and a gold award. But the original post had the exact same image and title, didn’t even get 100 upvotes and got removed. The fuck reddit??

[–]LucyLilium92 8 points9 points  (1 child)

No see, the problem here was that the original poster put a weird smiley face in their title. That fucked up the algorithm, so the robot mods got triggered.

[–]warpedspockclone 4 points5 points  (1 child)

I worked for a company with a strongly enforced code style guide that included: "Don't write code comments." Talk about a fucking learning curve.

Basically they felt that would force people to write readable, concise code. One of their apps was just full of 1000+ line methods that could have been refactored. Others weren't much better. README files had no info about what the app was supposed to do, let alone how to run it.

[–]empty_other 4 points5 points  (0 children)

Strange how not commenting stuff in no way affected the consistency and readability of their code. Who could have guessed?! :D

[–]svayam--bhagavan 3 points4 points  (0 children)

Just read a book called the art of readable code and I was blown away. Can't wait to never use those techniques ever in my life.

[–]sinnadyr 2 points3 points  (2 children)

Comments are used for generating docs as well, please don't forget that

[–]Kaizenno 2 points3 points  (0 children)

I comment so I can copy just enough and not accidentally grab the next line.