Should I Use Comments in My Code or Rely on Self-Documenting Techniques?

0
9
Asked By TechieTurtle97 On

Hey everyone,

I'm a junior Python developer at a mid-sized company and I try to take pride in writing clean, professional code. I initially included a lot of comments in my code to explain my thought process. However, during a pull request, I was told to remove many of these comments as they were seen as "noisy" and making the codebase larger.

I followed the advice but now I'm worried. If I keep this "no comments" approach, will it impact my effectiveness as a developer in the long run? I'm curious about your experiences and whether you think there's a balance between using comments and writing self-explaining code. Thanks in advance!

4 Answers

Answered By PragmaticProgrammer77 On

It’s a balancing act! Aim for code that is self-explanatory first. When you have to comment, ensure that it adds value by clarifying logic or decision-making rather than restating what the code does. More focused comments about 'why' decisions were made could be beneficial. Remember, the goal is to write code that is not only functional but also easy for others (and your future self) to read.

CodeCraftsman82 -

Right! And code documentation through test cases can often serve the same purpose as comments.

CleverCoder12 -

Exactly! A solid practice is to comment on potential pitfalls or complex chunks of logic.

Answered By OldCoder96 On

In my experience, extensive comments can lead to clutter if they repeat what the code already states. It’s essential to write clean, understandable code first—after that, add comments only when you’re doing something out of the ordinary or when the logic is intricate. However, using docstrings for function descriptions is valuable and helps create better documentation when needed.

CodeNinja88 -

Exactly! A well-structured codebase with clear function names should speak for itself. Just use comments wisely.

FreshCoder21 -

Definitely! Comments should be the exception, not the rule. If your code needs too many comments, consider that it might be time to revisit your design.

Answered By RefactorMaster55 On

As a general guideline, you really shouldn’t rely on comments to explain everything. Focus on naming your functions and variables to clarify purpose and functionality. Comments should serve as annotations for complex or unexpected code only. This way, when someone reads your code later, they can grasp its intention without sifting through excessive comments.

NewbieDev44 -

I like that approach! It keeps the code clean and understandable.

ExpertEye22 -

For sure! If your comments explain basic logic, it's time to rethink your code.

Answered By CodeWhisperer42 On

Honestly, I think comments should mainly explain *why* something is done rather than *what* it does. When writing code, the focus should be on making it understandable through clear naming conventions and functional decomposition. Comments can easily become outdated or misleading, so it's better to write self-explanatory code and limit comments to only those tricky cases that might need additional context. It's less about the quantity of comments and more about their quality.

DevGuru33 -

Yeah, I agree! Focusing on why is key. You can often convey the intent of your code through well-named functions or variables. Just remember, if you need to explain every line, maybe it’s a sign to refactor your code.

LearningDev99 -

Totally! Sometimes less is more. A good rule of thumb could be to comment on unusual decisions or logic that isn't clear at first glance.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.