T
1

My mentor said 'always comment your code' but a senior dev at work said the opposite - who was right?

I started learning Python about 4 months ago. My online course teacher kept saying to comment every single line so you know what it does. So I did that for a while. Then I showed my code to a senior dev at my office and he laughed. He said good code should be self-explanatory and too many comments just clutter things up. I felt pretty stupid. But now I'm wondering - for a beginner like me, is it better to over-comment or under-comment? What do you guys do?
2 comments

Log in to join the discussion

Log In
2 Comments
jade_grant95
I'd say comment the why not the what. I still comment my code but only when the logic is weird or I know future me is gonna be confused staring at it.
3
barnes.brian
Yeah, I went through the same thing when I started out. I was commenting everything like "this line adds 1 to the counter" and my coworker was like "dude, I can see it adds 1, tell me why you need to add 1." After getting roasted a few times I switched to just commenting the tricky stuff, like when I'm doing a weird workaround or something that looks wrong but is right. So for now, I'd say keep commenting stuff that explains the thinking behind your code, not the obvious stuff. Your senior dev probably just meant ease up on the obvious comments, not stop commenting entirely. Trust me, you'll figure out the balance after you write enough code and come back to it three weeks later wondering what past you was thinking.
1