DEV Community

Discussion on: What is Clean Code and why should you care?

Collapse
 
cvuorinen profile image
Carl Vuorinen

Learn to use the extract method refactoring to split long methods. Look for clues when to use it, like if you feel like you should write a comment to explain some operation. Sometimes when writing a new public method/function I first list what the method should do as 2-4 lower level operations and write these as comments. I then start implementing each step as a separate function. This is more rare though, as I said in the article, clean code rarely happens on the first try, so learn to refactor.

Regarding the variables, if you are working with a language that allows you to specify variables as read-only then use that as a default (e.g. use const rather that let or var in javascript) and only allow mutability when needed (makes you more aware when this happens and why it's needed). Also, splitting long functions into smaller ones helps with this problem as well ;)