Clean Code: A Brief investigation

One of the most important aspects of coding is making sure that others can easily read and understand your code. That way, they can use what you have created and make changes to it in the future. In a team setting, such as our capstone course, it is especially critical to encourage and enforce clean code for all developers and establish clear conventions to follow as a team.

I’ve reviewed two articles on clean code and code smells and put together two examples: one clean code practice that I would like to implement more often, and one not-so-good coding practice I would like to avoid.

Clean Code Practice

Reduce number of parameters/arguments in methods [1]

This can be accomplished by creating a new class of related parameters, if applicable, and sending the object itself as a parameter instead.

For example, let’s consider a method that automatically fills form fields in a new user form as part of a testing script:

public void FillUserForm (
      string username, 
      string address,
      datetime birthDate, 
      string email, 
      string password)
{
         FilllUsernameField(username);

         FilllAddressField(address);

         // Etc.
}

This can be updated to:

// Create class definition for UserFormValues with all parameters as properties.
public class UserFormValues()
{
   // Add each property as part of the class.
}

public void FillUserForm (
      UserFormValues userFormValues)
{
         FilllUsernameField(userFormValues.Username);

         FilllAddressField(userFormValues.Address);

         // Etc.
}
Why do this?

Methods are easier to read when there aren’t so many parameters and are less complex. This further modularizes the code and simplifies it for easier reading, which is why I would like to do this more often.

Coding Practice to Avoid

Don’t implement super long functions [2]

Really long functions can be harder to reuse and take a much longer time to understand for someone who isn’t familiar with the code. In addition, long functions may also mean that a function is performing too many different goals or certain steps can be grouped into additional methods.

For example, consider this function for a potential daily routine:

public void DoDailyRoutine ()
{
       // Morning Routine.
       GetOutOfBed();
       TakeShower();
       FeedCat();
       HaveBreakfast();

       // Do regular work.
       LogInToWorkComputer();
       ReviewTestScriptFailures();
       FixScripts();
       ReviewPullRequests();
       LogOutOfWorkComputer();

       // Do homework.
       CompleteReading();
       TakeQuiz();
       WriteBlogPost(); 
       WriteDiscussionReply();         
}

This can be updated to:

public void DoDailyRoutine ()
{
       DoMorningRoutine();

       DoWork();

       DoHomework();       
}

// Create new methods.
public void DoMorningRoutine()
{
      GetOutOfBed();
      TakeShower();
      FeedCat();
      HaveBreakfast();
}

public void DoWork() 
{
      LogInToWorkComputer();
      ReviewTestScriptFailures();
      FixScripts();
      ReviewPullRequests();
      LogOutOfWorkComputer();
}

public void DoHomework()
{
      CompleteReading();
      TakeQuiz();
      WriteBlogPost(); 
      WriteDiscussionReply();
}
Why avoid this?

I want to avoid the coding practice of too long of functions (around 10 lines or more) to help keep code cleaner, more modular, and easier to understand. Another reason would be to make the three new methods easier to reuse outside of the main method, since writing out the group of methods they call every time would no longer be needed.

Summary

Clean code will always be a crucial part of programming and there are many interesting practices that have been documented to help reach this goal. Through researching these two articles, I will continue to work towards writing cleaner code that helps not only future me, but my capstone team, and my coworkers ❤️

References

[1] O. Olsson, “My favorite code smells ️❤️,” Medium, https://medium.com/@happybits/my-favorite-code-smells-%EF%B8%8F-%EF%B8%8F-a14942335c9b (accessed Jan. 17, 2024).

[2] S. Hettiarachchi, “10 clean coding practices,” Medium, https://medium.com/swlh/10-clean-coding-practices-e37ac283184d (accessed Jan. 17, 2024).


Posted

in

,

by

Tags:

Comments

Leave a Reply

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