Coding Practices


There are many coding practices that are recommended these days as programming becomes a more widespread profession and hobby. More and more people are looking for recommendations on where to get started and how to code well as their journey starts, however it is also good practice for those who have been coding for years or even decades to review good practices, and study practices that are commonly avoided. In this research, I found things that I often think about when coding, but also some new things that I hope to implement soon into my own code as I continue my career and journey as a software engineer.

Coding Best Practice

A coding best practice that I found is reducing the number of characters in each line. This is something that I have more recently found helpful, and have been advised about doing this as well. There are a number of reasons to do this.

One reason is that you don’t want to have to scroll horizontally. It can already be annoying enough to scroll vertically, lets not add another axis of scrolling. You want to keep everything on the screen and so there are a few different recommendations out there, but the most common are anywhere between 80-120. This is quite the range, and it can also depend on the language you are using. Some languages allow for more quick and concise syntax, while others require longer, more elaborate syntax. This can also come down to personal preference.

Another reason to cut down the line length is for readability. Commonly, a new line of code is a new statement. I know personally, I can stay more focused on what is happening with a statement if it is broken up into a few lines, rather than having one super long line of code. This can obviously bring to light a code smell, but if this is used along with other good coding practices, it shouldn’t be too much of an issue.

Lastly, another reason to do it is it can force someone writing the code to re-evaluate their logic if their statement is too long. Sometimes, when writing long statements on the same line, you don’t realize just how long the statement is getting until you break it up into multiple lines. This can force someone to realize that the line length might be too long and it is time to rethink the logic. This can result in making code more readable in more than one way.

This coding best practice is something that I hope to be integrating into my own code and be keeping in mind as it helps in many ways of readability, logic, and even maintainability.

Code Smells

I, like many others, can sometimes get in the zone of just writing away, implementing new functions and methods to set data, get data, pass data, etc. While this can be a great place to be. It can sometimes leave me in a spot where I have duplicated almost identical code twice, three times, or even more times over. This is unnecessary and leads to other code smells which are advised to avoid.

Code duplication is something that is easy to do without even realizing it. Code reviews and just simply going back over old code and methods can be a truly horrifying experience when you see what you have done. However it can also be great to go back and see what you can improve, making your code that in your mind was already great, even better. Reusing code is always the way to go when trying to avoid long functions, large classes, and even longer source files. Frameworks can be complicated to maintain and reusing methods to accomplish similar task can easily help with reducing complexity.

For example, we can see an example of a code with code duplication here

Image from Rafael Melo, 5 Most common code smells that you should avoid

You can see that there is a duplicated code block that is the exact same except for the variable in the if block. We can also assume that this is going to be repeated several times. By parametrizing this, and creating one function, you can more easily implement this as seen below.

Image from Rafael Melo, 5 most common code smells that you should avoid

By reducing code duplication, you can also write functions that can be more broad (within reason of course) so that there isn’t a new 25 line function with only one line different than another method. It is best to be able to write things in a way that can easily be used over and over again.

This can also help with reducing the time it takes to create something. If a developer has to write a new method or a new function each and every time they want to do something new, it can force someone to take longer to develop the same thing, had they written functions and code that was reusable by other classes or functions.

This coding practice is something that I would like to implement and be more thoughtful of as it can greatly improve the usability and complexity of my code.

Overall, this dive into helpful techniques and things to avoid has been useful to see things that I can be thoughtful of and things that I need to avoid in order to be successful. These things can help me write better code and set myself and others up for future success when developing with the code set.


Resources:

Melo, Rafael. “5 most common code smells that you should avoid.” medium, 21 July 2021, medium.com/geekculture/5-most-common-code-smells-that-you-should-avoid-86ae41cb1dc7.

Sanjuan, Claudia. “10 tips for writing cleaner code in any programming language.” unosquare, www.unosquare.com/blog/10-tips-for-writing-cleaner-code-in-any-programming-language/. Accessed 23 Jan. 2023.

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *