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 ;)
I am a Software Dev girl who loves Uncle Bob, is drawn to the human side of software development and clean coded applications, and enjoys acting as a liaison between the business and tech.
Try to write functions so that it does one thing. If there are few steps that had to be done try splitting it into chunks and then extract those chunks into private functions to make it easier to read. When you extract your code to another function give it a meaningful name. Sometimes it is good to look at the function after 1-2 days to see if you still know what's going on. And refactor, refactor, refactor! Many programming IDEs has nice refactoring tools so take advantage of those.
When I write a function I start with each big step of the task it has and write it as a separate function call and then write the code of the function accordingly. It's a similar approach to Carl's comments. Sometimes it helps to start with paper, programming doesn't start with writing a code ;)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Any tips for a somewhat beginner, writing functions that tend to be way too long, re-using variables too often?
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 thatlet
orvar
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 ;)Try to write functions so that it does one thing. If there are few steps that had to be done try splitting it into chunks and then extract those chunks into private functions to make it easier to read. When you extract your code to another function give it a meaningful name. Sometimes it is good to look at the function after 1-2 days to see if you still know what's going on. And refactor, refactor, refactor! Many programming IDEs has nice refactoring tools so take advantage of those.
When I write a function I start with each big step of the task it has and write it as a separate function call and then write the code of the function accordingly. It's a similar approach to Carl's comments. Sometimes it helps to start with paper, programming doesn't start with writing a code ;)