Categories
Uncategorized

Smelly Code

Welcome back to my Senior Capstone Journey where my team and I are developing VR Arcade Classics. Today we’ll be exploring good and bad coding practices to help improve your skills as a programmer. It’s important to learn good habits early on to save yourself and your team from avoidable mistakes that can cost you valuable time. Specifically, code readability.

DON’T…

Use random names for functions and variables. This quick adjustment improves the readability of your code tenfold. Using vague names can become confusing quickly. It forces you to remember what everything does, rather than just knowing by reading the name. It may seem unnecessary when writing short pieces of code, but let me demonstrate the difference it can make.

Let’s look at this chunk of Python code here, which contains a plethora of bad coding practices.

We could analyze this, mentally keeping track of each variable. However, simply changing the names can provide some clarity on what this function does.

DO…

Follow standard naming conventions. When everyone follows the same rules for naming parts of your code, it makes it easier to read others work as well as your own.

Now that we’ve changed the function and variable names, we already get an idea of what this function does. It calculates the average number of an array and prints it. Much better!

DON’T…

Expect yourself to remember what all of your code does. Comments are incredibly beneficial and an overlooked aspect of coding. When caught up in the moment it can be easy to stop adding comments because you assume it’s self-explanatory or you won’t need it. However, as your projects get more complex you’ll begin to forget and re-analyze your code to find what you need. Furthermore, if you’re working on a team it’s essential to make clear comments to quickly catch them up to speed. Utilize comments in all your code to form this good habit.

DO…

Add useful comments that explain the logic of the code. This both saves you time and mental power when reading your code. This is especially useful when coming back to old code or when you’re having a code review.

Now we quickly understand the purpose of this function and how it works. We also know that ‘array’ is simply a sample list and can be replaced later on.

Conclusion

Let’s compare our code from the beginning to what we finished with.

Stinky Clean

Without changing any logic, we can now clearly understand what this function is doing. With the combination of good naming conventions and comments, it takes only a few seconds to understand the importance of each part of our code.