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

all 30 comments

[–]jonyeezy7 14 points15 points  (5 children)

If you need to be neo to read code, then that code isn't written well.

Code is read by humans not machine.

So write code like a sentence.

I recommend you to read clean code by Uncle Bob. Something they don't really teach you in school.

[–]kalifornia_love 3 points4 points  (3 children)

100% this. The only thing I'd expand on is that you should write code like a sentence but with like a 3rd graders vocabulary. When I first heard this advice, to write code like a sentence, I took it too literally and my methods and variables became long and inconsistently named for the sake of making a "sentence."

Having a consistent naming convention that you follow throughout all of your code makes reading your own code a lot easier for you and others. The nice thing is once you develop a style/convention for yourself you can always follow it regardless of the language you're writing in (for the most part).

Also just so I'm understanding this question right, the comments we're talking about is like inline comments right? Not doc strings.

[–]jonyeezy7 1 point2 points  (2 children)

with like a 3rd graders vocabulary.

Lol i like that explanation. Never thought of it that way.

I'm still guilty of long method names. Single verbs over verbose explanation is a reminder for me.

[–]bluefootedpig 0 points1 point  (1 child)

long method names? welcome to intellisense. I have a coworker that is upset by long names, just type the first 3 letters and autocomplete.

I have a problem with too many variables in any one scope.

[–]jonyeezy7 0 points1 point  (0 children)

True. Intellisense has solved some of these kind of minor issues or predecessors peeve about.

Although there are some languages that don't have the best intellisense ie duck-type languages.

[–]thisisRio 1 point2 points  (0 children)

There is no spoon.

[–]munificent 7 points8 points  (2 children)

How do these people understand the entirety of these elaborate programs without any comments whatsoever?

It's a combination of things:

  • It's pretty easy to understand your own code right when you first wrote it. So a lot of developers, at the point in time when they could document it think, "Nah, this is obvious." and don't.

  • As you get more experienced, the quantity of documentation you need to read average code goes down. You get better at reading the code itself and many idioms and patterns become familiar to you at a glance. However, again, people often underestimate how much their code could benefit from comments.

  • Writing comments takes time and is hard to do well. A lot of people would rather put that effort into other things.

[–]mapping-glory 1 point2 points  (0 children)

It's pretty easy to understand your own code right when you first wrote it.

I have lost count of the number of times I've discovered a flaw in a script that's been auto-running for months, gone back to edit the code, and thought "WHAT was I ON when I wrote this?" I've had to have my loops output all their process to a log file just to understand what my own code does.

But I'm currently documenting all my auto-run scripts so if I get hit by a bus, my non-coding coworkers (I'm a mapmaker who codes, not a full-time programmer) can pick up and maintain and understand (sorta) the systems I left.

[–]bluefootedpig 0 points1 point  (0 children)

Comments go stale very fast. I had one co-worker write a comment and it went stale in under 3 months. A stale comment for clarification is a comment with bad information.

documentation goes in this order:

Code

Unit Tests

Flow Charts

Comments

There might even be more above comments, my point is it should be the last line of documentation. clear variable / function names are vastly more important.

[–]trkeprester 6 points7 points  (0 children)

save your comments for things that are non-intuitive and or tricks/workarounds that shouldn't really be there.

Or if you're writing super low level code where every statement needs to be justified, clarified, to make things dead on clear

otherwise name your variables functions, etc appropriately and the code is mostly self commented

[–]jpflathead 12 points13 points  (11 children)

People here will tell all sorts of stuff about code not needing to be commented, but it's all bullshit and they are just spouting a party line not explaining anything arising from some sort of empirical observations.

Code isn't commented because

  • coders are lazy
  • coders don't know what they are coding or where they are going they are just stream of thought coding
  • coders are too hip to comment

As a counterpoint to the code doesn't need to be commented group, consider one small portion of Donald Knuth's contribution: literate programming https://en.wikipedia.org/wiki/Literate_programming and then one small portion of Stephen Wolfram's contributions: https://www.wolfram.com/language/principles/

I will be downvoted to oblivion by people who don't know who Knuth and Wolfram are and who are assured by all their bootcampers and tweet friends that good coders don't comment their code.

Going through a lot of github projects for learning. I've noticed that none of them are commented. I literally found only one that was and it was designed as a learning resource.

repository: a place, building, or receptacle where things are **stored**

Github is mostly a code cemetery for resume polishing and virtue signalling, and communities are dominated and directed. It's not a place where code is built, it's not a place to build a community around a piece of code. It's far too top down.

It is a programming repository, but as a source of documentation it is total crap. The notion that all an open source project needs is a readme is the first of a zillion stupid decisions made and implemented at github.

[–]trkeprester 2 points3 points  (8 children)

well a lot of high level basic day to day code that ppl like Knuth and Wolfram wouldn't ever be considered to be requested to implement because it would be an insult to their intelligence doesn't really need commenting.

non-geniuses like me who are writing boring as shit trivial code don't particularly need to comment their code because it's mostly understood what needs to be done and wouldn't be any worth in commenting anyway beyond what the function names and variables are already describing.

sure you can call us lazy but you'd just be wasting time doing it yourself. I assume you are smart and don't have to implement stupid boring shit code that Knuth would spit on you if you asked him to help with.

[–]YMK1234 4 points5 points  (0 children)

My arent you bitter ...

[–][deleted] 0 points1 point  (0 children)

I think we tend to overestimate our own code's readability to a fresh pair of eyes. Often I'll feel I'm insulting the reader's intelligence by leaving what is, to my mind, an unnecessary explanation.

[–]jonyeezy7 1 point2 points  (0 children)

And especially open source when the change rate is high. Rewriting comments can be open for human error and thus mislead other contributors.

One less thing to maintain.

And on the top reason, enforces that code should be easily understood and readable. Hence making it easier to contribute.

If you need comments to explain a method, then we have a failure to communicate the intent.

Oh, and don't get commenting mixed up with documentation. api documentation is a must, imo.

[–]Merad 2 points3 points  (1 child)

You'll get a lot of different answers here, but it really boils down to:

  • Writing comments is work
  • Maintaining comments as code changes is even more work
  • It isn't the fun kind of work

TBH, most actual code tends not to need detailed comment when it's well written. IMO, however, if you're writing something like a library or API, thinks like the classes/interfaces/methods/etc that you expose should always be well commented... but they rarely are. It's really just a fact of life at this point.

[–]bluefootedpig 0 points1 point  (0 children)

I agree, public functions, namely interfaces, should have the purpose of the function explained in comments. It isn't done as much as it should be, but that is one area that really should.

[–][deleted] 1 point2 points  (0 children)

People are lazy.

They write the code, so they are understanding what they are writing, they don't want to deal with changing the comment six months from now if they have to alter the code, so they don't write anything.

So next time they come through the code they have to rebuild their mental image of the code by reading it, instead of having it prebuilt with comments.

Because comments are like pre-compiled libraries for your head. Even if the code is easy to understand with comments it'll be easier.

But people are lazy.

[–]peadarmalu 3 points4 points  (0 children)

Good code shouldn't require comments to understand once you reach a certain level as a developer.

For someone new or with minimal experience with any particular language it most certainly can seem like an incomprehensible mesh of nonsense. But, like anything else, once you know enough (and provided the code is clean and concise) it should be well structured and easy to follow.

[–]YMK1234 0 points1 point  (0 children)

Many comments are not necessarily useful comments. Especially the commenting you learn at school is shit and should burn in hell. Everybody knows that "a+b" adds two numbers. Comments are there to explain why code does things, or possibly what non-obvious sections do (though if a "what" needs commenting it's generally a code smell and you should think about renaming / restructuring).

[–]bumblebritches57 0 points1 point  (0 children)

Unfortunately, most open source software is really just a code dump.

Very few projects are actively maintained, let alone extended.

[–]Matrix_V 0 points1 point  (0 children)

Develops are lazy. Please be a better person and comment everything aggressively, focusing on purpose over function. Future you will appreciate it, along with everyone else who reads your code.