Nick Francisco Capstone

CS 462 – Blog Post 1

An article I read about clean code comes from freeCodeCamp.org [1]. This article provides several tips and best practices for writing clean code. One tip from the article that I want to work on more often is the efficiency of my code. I can create effective code but I often know that it can be more efficient. The article gives an example of inefficient and efficient code shown in figure 1 below. The efficient code only requires one iteration over the array and has fewer lines of code which helps with its simplicity. Improving my efficiency will not only help my code run faster but can help my code simplicity which will make it easy to understand and follow.

I also have some practices that I want to avoid while coding as an article on code smells from refactoring.guru [2] points out. One of these practices that I want to avoid is having code bloat, specifically long functions. In previous coding assignments I would often place all code for one task in one main function instead of creating helper functions that can help reduce the main function. Doing this creates a long and bloated main function that is harder to understand and follow. Creating helper functions to reduce the main function will help me avoid code bloat and can help me improve code readability or simplicity.

// Inefficient Example
function sumArrayInefficient(array) {
let sum = 0;
for (let i = 0; i < array.length; i++) {
sum += array[i];
}
return sum;
}

// Efficient example
function sumArrayEfficient(array) {
return array.reduce((a, b) => a + b, 0);
}

Figure 1 – Inefficient vs Efficient code example

Citation

  1. How to Write Clean Code – Tips and Best Practices (Full Handbook). (2023, May 15). freeCodeCamp.org. https://www.freecodecamp.org/news/how-to-write-clean-code/
  2. Code Smells. (n.d.). Code Smells. https://refactoring.guru/refactoring/smells

Print Friendly, PDF & Email

Posted

in

by

Tags:

Comments

Leave a Reply

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