The Paradox of Code Comments

The Paradox of Code Comments

We all know that comments are very important in any programming language. And who hasn’t come across code that was impossible to understand, even if it was written by yourself just six months ago? :)

But should we really use comments between the lines of our code? Note that I’m not talking about comments typically used to describe what a function does, which are compiled into JavaDoc or something similar.

The clarity of code is essential; no one questions that. Any programmer who comes across your code should be able to understand it within a few minutes and without much effort. But could using comments be a sign of something wrong?

But how can explaining your code be a bad thing? In the book Clean Code, Uncle Bob says that readable code should be like good prose. Each line of your code should, by itself, tell a piece of the story. The point here is that if you need to comment on your code to make it easier to understand, perhaps it needs to be refactored.

Let’s look at an example:

Reading the comments, we can clearly understand what each line does. But what if we refactor the code, extracting each part into a private function and giving each function a descriptive name? Check out the modified code:

So, what do you think? Do you prefer to comment on your code or extract everything into smaller functions? Share your opinion in the comments.

#Java #CleanCode #Refactoring

要查看或添加评论,请登录

Diego Fialho的更多文章

  • Git and GitFlow: How to Improve Your Repository Organization

    Git and GitFlow: How to Improve Your Repository Organization

    Nowadays, we can't live without a version control tool for software development, and this need becomes even more…

    1 条评论
  • What to Do When Faced with a Bug?

    What to Do When Faced with a Bug?

    If you’ve been in the development field long enough, you’ve likely encountered errors or strange software behavior…

    2 条评论
  • Do @Annotations create a dependency relationship?

    Do @Annotations create a dependency relationship?

    No one doubts the effectiveness of annotations in Java. Since they were included in the language, their use has been…

    1 条评论
  • BDD: A Discovery example

    BDD: A Discovery example

    In our last article, we discussed tasks and activities in BDD. Now it's time to delve a little deeper into the first…

    4 条评论
  • BDD: Tasks and Activities

    BDD: Tasks and Activities

    It's a beautiful Monday, and your project leader, after spending a weekend at a software quality conference, comes to…

  • BDD: the missing link between requirements and code

    BDD: the missing link between requirements and code

    Well, at this moment you might be wondering if this gap between requirements and code isn't what we generally call…

社区洞察

其他会员也浏览了