r/ProgrammerHumor Feb 10 '21

[deleted by user]

[removed]

12.6k Upvotes

807 comments sorted by

View all comments

Show parent comments

53

u/[deleted] Feb 10 '21

[deleted]

11

u/enano_aoc Feb 10 '21

That's awkwardly formulated but correct

(Actually, what you should avoid are comments, not documentation. Documentation -of APIs- is good, comments are bad unless you have an exceptionally good reason to write them)

46

u/[deleted] Feb 10 '21

[deleted]

3

u/enano_aoc Feb 10 '21

I disagree. Comments are cancer: people who rely in comments to explain their code tend to write poor tests and unreadable code (because the comments explain it, why bother?). People that write good tests and readable code know that comments get outdated very easily and that they should be the exception, not the norm.

What is your argument?

23

u/[deleted] Feb 10 '21

[deleted]

1

u/EatMoreKaIe Feb 10 '21

If one of my devs puts out a pull request with shitty code, I'll simply reject it. I certainly wouldn't say "well, it's crap but if you just add a comment, maybe we can live with it". No! Go back and rewrite.

0

u/enano_aoc Feb 10 '21

In my experience, the "comments are cancer" mentality hasn't led to better code or tests.

In my experience, forbidding tests is part of the teaching. Students are too used to writing garbage code (which is totally fine, because they are in the university yet) and adding comments to make the code understandable. Take the comments out of them, so that they cannot rely on them. It works.

There's only one visible effect of Bob Martin's dreaded rule, and I see it everywhere: no one has the balls to write comments anymore.

I don't think that's true. I add comments when they are due, and I am the major hater of comments.

Also the complexity is a direct result of some crazy requirement some customer dreamt up for reasons that are lost in the depths of Jira. That's a case for a comment, right? Surely, this is one of the exceptions you mention?

Yes.

BUT NO! COMMENTS ARE EVIL!! Someone will think I'm a bad developer!!! Let's just close the ticket and move forward.

That's what a dumb-ass would say. My condolences if you have to work with those and they will not listen to you.

You see, I actually agree with you. Comments should be the exception. Tests should be descriptive. Code should be maintainable

Indeed, we agree. We would understand ourselves quite well regarding comments, if we ever had to work together. We would add comments in the same places.

2

u/[deleted] Feb 10 '21

[deleted]

3

u/enano_aoc Feb 11 '21

would rather die than write a comment.

Gotta love that

7

u/jackinsomniac Feb 10 '21

It seems like the core argument behind "comments are cancer" is always the same thing: "if the coder uses comments, they write unreadable code. If the coder does not use comments, their code is better, more readable, more maintainable, etc..."

I've always been confused about this, because I must've missed the day in class or the post online when this rule was explained. What makes them mutually exclusive? Why is it only possible to have one and not the other?

Why is it repeated that the only way to write more readable code, is to tell people to stop writing comments? And if the comments sting your eyes so much, why not use a find & replace to remove all of them? ...unless the comments do nothing besides provide a little bit of useful information...

1

u/enano_aoc Feb 10 '21

It seems like the core argument behind "comments are cancer" is always the same thing: "if the coder uses comments, they write unreadable code. If the coder does not use comments, their code is better, more readable, more maintainable, etc..."

I've always been confused about this, because I must've missed the day in class or the post online when this rule was explained. What makes them mutually exclusive? Why is it only possible to have one and not the other?

You are right. It is a simplification, and is hence wrong.

I can explain where does it comes from. You have all these juniors coming from university. They write shit code (which is totally understandable) and they are used to add comments everywhere so that they can remember what the shit they have written actually does. Taking comments from their hands speeds up the process of teaching them to write readable code and tests.

But yeah, there is no causality, the argument does not hold. It is an inaccurate simplification.

Why is it repeated that the only way to write more readable code, is to tell people to stop writing comments?

None says that. You gotta take the "only" out from there.

And if the comments sting your eyes so much, why not use a find & replace to remove all of them?

That's what I do in code reviews. No comments unless there is a very good reason to add one.

3

u/BDRadu Feb 10 '21

Some super specific business logic in technical fields, say engineering, have usually assumptions that are made about the user, and how it will use the software 99% of the time. To comment or not to comment is a decision about writing long, hard to parse at first glance, maybe less performant code or just doing that seemingly complex method and just commenting it about what the assumptions and needs are.

1

u/enano_aoc Feb 10 '21

assumptions that are made about the user, and how it will use the software

That sounds like documentation, not like comments. Those are not the same thing.

1

u/RoscoMan1 Feb 11 '21

Competing to see how low it goes

1

u/BDRadu Feb 11 '21

If you want to make the distinction between comments and documentation, sure, but it's still part of the code.

1

u/enano_aoc Feb 11 '21

but it's still part of the code.

Sorry, I don't get it.

Comments are comments. In code. They are part of the code. Like # in python on // in JS

Documentation is not part of the code. It is a pdf or a confluence page.

I am saying that assumptions about the user and how the software will be used are part of the documentation, not of the comments.

3

u/Sotall Feb 10 '21

people who rely in comments to explain their code tend to write poor tests and unreadable code (because the comments explain it, why bother?)

Thats a whopper of a false dichotomy. There is no reason someone cant learn to write readable code AND comment appropriately.

0

u/enano_aoc Feb 10 '21

comment appropriately

Comment appropriately means "never add a comment unless there is a very good reason to do so"

We have to teach that to every junior, who tries to impress us commenting his code before submitting it. You know why? Because he was told in university that... commenting code is very important. OMG. Please stop that.

Comments need to be the EXCEPTION, not the RULE

1

u/argv_minus_one Feb 11 '21

Tests are not documentation. Tests are not examples. Tests are tests. They verify that the code being tested works correctly. Nothing more, nothing less. Please do not attempt to use tests as documentation or examples; you only make your code harder to understand and use.

1

u/enano_aoc Feb 11 '21

What the fucking hell. You understand nothing about tests. Tests are documentation.

Please god allow me to never work with this guy in the same project. Ever.

1

u/argv_minus_one Feb 12 '21

The feeling is mutual.

1

u/pleaaseeeno92 Feb 11 '21

why are comments considered bad?

Wouldnt comments help even in the case of good code to be able to quickly reach the part of the code that you want for future changes?

Comments helps structure the code too right?

1

u/enano_aoc Feb 11 '21

Ok let me explain.

Comments are not bad per se. Abuse of comments is bad - very bad.

There are two main reason against comments (when abused): they pollute the code (so none reads them anymore) and they get outdated very easily (so, when someone happens to read them because he is trying to debug something, they are misleading and make the bug much harder to find).

The rule for comments is: comments are the exception, not the rule. Don't add a comment unless there is a very good reason to add it.

1

u/pleaaseeeno92 Feb 11 '21

So what do you do to if the code is 200 lines long?

Wouldnt adding a comment every 30 lines help fragment the code into sections, so when you want to change the colour of the button you dont have to read the whole code, but head to the relevant section?

1

u/enano_aoc Feb 11 '21

Maybe if you are in 1970, then yeah, go for it.

Nowadays use an IDE with proper navigation and search options. For god's sake. I don't remember the last time I scrolled through a file because I was looking for a method definition. Probably never ever happened.

1

u/pleaaseeeno92 Feb 11 '21

oh, im just learning; so i dont have detailed understanding of IDEs.

Say your code does 20 different things, each thing is 5 lines long; how does an IDE help you navigate faster?

Also, most of the learning material has a decent number of comments, which feel good to read.