A Programmer’s Mindset
A Programmer’s Mindset
Before we finish the chapter, we need to have a word about programming style. Obviously the point of writing a computer program is to make it do what it’s supposed to do, but the style of the program is also important. Your code should be clear and concise, and also be consistent in the way it follows conventions—from the names you choose for variables to the amount of whitespace you leave between blocks of code. Your code should also be easy to read and include comments that explain what the code does.
You should always be looking to improve your code to make it more efficient and easier to follow. This process is called refactoring, and should be done regularly so that your code stays up to date and doesn’t become stale.
An important principle in programming is the rule “Don’t Repeat Yourself”, or DRY. This means that you should always be looking to avoid repeating lots of code. Following this principle will help make your code more flexible and easier to maintain and update. (It’s always easier to change one bit of code than hundreds!) If this sounds a bit like programmers are lazy, then you’re absolutely right! A good programmer will always be looking to write code that does the job in the most efficient and elegant way possible. Less code means less chance of bugs, and it’s easier to maintain.
A good program doesn’t just appear instantly. It starts as a plan, and grows organically into its finished state. Developing a program involves a lot of problem solving, and sometimes you’ll take some wrong turns and get stuck. This is normal, and you should be prepared to make changes and adapt. A good plan and well-commented code that follows the DRY principle will stop your code becoming a complex mess that’s impossible to follow and comprehend.
Challenges
Try writing pseudocode to describe some common tasks such as brushing your teeth or riding a bike.
Write pseudocode that will find the largest number in a list of numbers.
Try logging some different messages to the console using
console.log(). You can see my code on CodePen.Add some more colors to the array in the “I Can Code a Rainbow” program. You can see my code on CodePen.
Comments
Post a Comment