Don't comment out code while programming

preview_player
Показать описание
Commented out code is a major code smell. Avoid it like the plague.
.
.
.
#shorts #devbriefs #code #coding #programming #programmer #comments #java #vscode #learncoding #softwareengineer #javascript #csharp #compile #scroll #codesmell
Рекомендации по теме
Комментарии
Автор

As uncle Bob said:
If I see commented out code I delete it immideately because they'll find it in version control

max_ishere
Автор

Commenting out code is fine temporarily if you're currently refactoring a part of your code.

You definitely should delete it some time after checking if it works, though.

NOT_A_ROBOT
Автор

I only comment out code until I've finished testing its replacement code. After that, I'm not afraid of forgetting how I originally wrote it because I successfully updated it.

Nofxthepirate
Автор

This just seems like an argument for pruning comments after the code commented has been replaced, not against commenting out code. I've deleted code before since I had a better implementation, encountered a bug, then realized that I overwrote my history buffer so I can't even go back to reference my old code...

inferior
Автор

Minor adjustment, don't check in commented out code. Commenting out code can be helpful when debugging.

bwill
Автор

I don't think commenting out code is bad. It keeps the idea you had writing the code visible. Of course after some time when it becomes clear that the code won't be needed anymore you delete it (at last before pushing to git). But more often than not you later stumble on this commented out code and it helps you solve your problem.

redcrafterlppa
Автор

These hard-and-fast “do/do not” coding tips I see around online and on YouTube specifically are a bit… problematic? Like from a teaching pov. They all seem to be nothing more then actions you should or should not take that don’t give a beginner any room for thinking. A better way to phrase this would be something like “don’t keep vestigial code around”, which not only addresses the actual issue by directly referring to what we’re trying to avoid, but it also asks the person hearing it to actually engage their brain. Additionally, by cutting to the heart of it instead of just giving actions to perform, you avoid all of the corner cases for which the actions are inappropriate. You might be able to think of a few reasons why you’d be fine commenting out code and leaving it there, but because code that is vestigial is not useful by definition you’d find it much harder to find a reason to flaunt the “rule”. It also opens you up to talk about examples of why this is a good thing that have nothing to do with comments, like the exchange that lost millions and had to close because some vestigial code path went hot for some reason and made some bad trades.

hotfishdev
Автор

that's why i always have a separate file to dump those useless code in case i might need them in the future. keeps my code clean and still keep those code for reference.

monq
Автор

I intentionally always comment out code. It let's me know what I've tried, what has worked, and what hasn't. Especially as someone who's learning, it's good to have access to other ideas.

Besides. I use comments as a notebook in my code, so there's tons of comments anyways.

rmt
Автор

I comment out code when I am debugging

jjophoven
Автор

I think the more accurate advice would be to never commit commented out code. For debugging/refactoring, it can be useful, but if you have that code in version control, there is no reason for the comment.

botondhetyey
Автор

There's a better solution in my opinion: insert in the commented code in the end of the file and write a comment where the code was

menilev
Автор

Hardline advice like that is misleading. Comment/delete as necessary - everyone has their own best practice that suits them. If there's too much, commented code, surround it with a block and minimize it, or delete the bits that are unlikely to be reused.

paulcarter
Автор

It's 2022 and people who make the IDE can't invent some kind of log system to store your old code alongside of your code

Instead we rely on comment for everything.
We use comment for everything, from Task management to storing the API key.

Are we not suffering enough?

jensenraylight
Автор

I couldn't agree more! After spending the last two weeks doing a refactor. Keep parts while refactoring sections for reference, but as soon as you get it working, either delete it or store it in a local backup. Leave the commented code out of the production code.

WhyNotProgram
Автор

Yes, I've seen comment bloat a lot in professional environments. Someone else also gave the version control reason so I'm going to give another one. Commented out code makes actual comments less visible too.

EverRusting
Автор

I comment out code, but rarely would I ever leave it there once I'm done doing what im doing. I like using it as a bookmark and reminder of where my train of thought was- for me a commented out funtion means "this funtion is under construction, there is no working funtion for this yet, I'm in the middle of rewriting/rethinking it and need to not forget an idea present here, " But once it's no longer needed, either me finishing the rewrite or simply didnt need the info anymore and could just do a "//-continue here" bookmark with nothing else, I delete it.

ForkedMan
Автор

I only ever comment out code temporarily and remove it by the time I'm pushing

Rakesh
Автор

I found myself in a project where even entire files were commented out.

chawkichalladia
Автор

Commenting is fine when refactoring if you haven't made commits yet.

HumanBeingSpawn