Coding tip: Don't repeat yourself

We'll cover the following

One of the biggest problems that new programmers need to learn is the idea of “Don’t Repeat Yourself (DRY)”. This concept is that you should avoid writing the same code more than once. When you find yourself doing that, then you know that chunk of code should go into a function. One great reason to do this is that you will almost certainly need to change that piece of code again in the future and if it’s in multiple places, then you will need to remember where all those locations are AND change them.

Copying and pasting the same chunk of code all over is an example of spaghetti code. Try to avoid this as much as possible. You will regret it at some point either because you’ll be the one having to fix it or because you’ll find someone else’s code that you have to maintain with these sorts of problems.

Wrapping Up

You now have the foundational knowledge necessary to use functions effectively. You should practice creating some simple functions and try calling them in different ways.

Get hands-on with 1200+ tech skills courses.