143

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
[-] dandi8@fedia.io -3 points 1 month ago* (last edited 1 month ago)

It makes me sad to see people upvote this.

Robert Martin's "Clean Code" is an incredibly useful book which helps write code that Fits In Your Head, and, so far, is the closest to making your code look like instructions for an AI instead of random incantations directed at an elder being.

The principle that the author of this article argues against seems to be the very principle which helps abstract away the logic which is not necessary to understand the method.

public void calculateCommissions() {
  calculateDefaultCommissions();
  if(hasExtraCommissions()) {
    calculateExtraCommissions();
  } 
} 

Tells me all I need to know about what the method does - it calculates default commissions, and, if there are extra commissions, it calculates those, too. It doesn't matter if there's 30 private methods inside the class because I don't read the whole class top to bottom.

Instead, I may be interested in how exactly the extra commissions are calculated, in which case I will go one level down, to the calculateExtraCommissions() method.

From a decade of experience I can say that applying clean code principles results in code which is easier to work with and more robust.

Edit:

To be clear, I am not condoning the use of global state that is present in some examples in the book, or even speaking of the objective quality of some of the examples. However, the author of the article is throwing a very valuable baby with the bathwater, as the actual advice given in the book is great.

I suppose that is par for the course, though, as the aforementioned author seems to disagree with the usefulness of TDD, claiming it's not always possible...

[-] TehPers@beehaw.org 2 points 1 month ago

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?

[-] dandi8@fedia.io 4 points 1 month ago

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@programming.dev 7 points 1 month ago

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.

[-] dandi8@fedia.io 1 points 1 month ago

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

[-] JackbyDev@programming.dev 1 points 1 month ago

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

[-] lolcatnip@reddthat.com 4 points 1 month ago

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.

[-] dandi8@fedia.io 1 points 1 month ago* (last edited 1 month ago)

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?

[-] lolcatnip@reddthat.com 3 points 1 month 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.

[-] TehPers@beehaw.org 4 points 1 month 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.
load more comments (44 replies)
this post was submitted on 10 Aug 2024
143 points (95.0% liked)

Programming

16996 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 !webdev@programming.dev



founded 1 year ago
MODERATORS