this post was submitted on 10 Aug 2024
145 points (95.0% liked)

Programming

17313 readers
93 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 1 year ago
MODERATORS
 

Seeing that Uncle Bob is making a new version of Clean Code I decided to try and find this article about the original.

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 2 points 2 months ago (1 children)

If those functions are huge units of work or pretty complex, I can agree. For most cases though, a simple code comment should do to explain what's going on?

[–] [email protected] 4 points 2 months ago (3 children)

Comments should never be about what is being done. They should only ever be about why it is being done.

If you write your code like suggested in the book, you won't need to rely on possibly outdated comments to tell you what's going on.

Any comment about "what is being done" can be replaced with extracting the code in question to a separate, well-named method.

[–] JackbyDev 7 points 2 months ago (1 children)

I disagree about comments should never be about what is being done. If what is being done is not obvious then they're important. Take assembly code as an example. Or complicated bit operations. I agree the why is more important to document than the what but saying the what is never important seems misguided.

Also, this may be a semantics thing, but oftentimes the code's specification is in doc comments. I don't believe you're claiming code shouldn't ever have specifications, this isn't meant as a gotcha lol.

[–] [email protected] 1 points 2 months ago (1 children)

You're talking about assembly in a thread about OOP...

[–] JackbyDev 1 points 2 months ago

I think commenting what can be important in OOP too though.

[–] [email protected] 4 points 2 months ago

I think it's good to document why things are done, but extracting things out into another function is just documenting what is being done with extra steps. This also comes with a number of problems:

  1. Not all languages are readable. Documenting what is being done is important in some C, or when working with some libraries that have confusing usage syntax.
  2. Not all people reading the code know the language or libraries well. Those people need guidance to understand what the code is trying to do. Function names can of course do this, but...
  3. Not all types can be named in all languages. Some languages have a concept of "opaque types", which explicitly have no name. If parameter and return types must be specified in that language, working around that restriction may result in unnecessarily complicated code.
  4. Longer files (the result of having dozens of single-use functions) are less readable. Related logic is now detached into pointers that go all over the file all because of an allergic reaction to code comments, where a simple // or # would have made the code just as readable.
  5. Function names can be just as outdated as code comments. Both require upkeep. Speaking from personal experience, I've seen some truly misleading/incorrect function names.
[–] [email protected] 4 points 2 months ago (1 children)

A function name can be misleading just like a comment can, in the same scenarios and for the same reasons, plus it's harder to update because you have to change it in at least two places.

[–] [email protected] 1 points 2 months ago* (last edited 2 months ago) (1 children)

And yet, outdated comments are far, far more common than outdated function names.

Also, if you're changing a comment which explains the "what", you should likely change the method name, as well.

It's important for the client to know what the method does by looking at the name, so why would you duplicate your effort?

[–] [email protected] 3 points 2 months ago

And yet, outdated comments are far, far more common than outdated function names.

Because people don't try to squeeze a complete description of what a function does into a single identifier, which is what you you would have to do if you want function names to take the place of comments. I for one don't want to strip all the spaces and punctuation out of my comments so I can use them as function names, and I really didn't want to read someone else's code written in that style.