DEV Community

Cover image for 5 coding habits you need to stop!
Imam Abubakar
Imam Abubakar

Posted on

5 coding habits you need to stop!

Habits play an important part in molding our programming skills and productivity. As developers, we frequently slip into code habits that stifle our progress and limit our capabilities. In this post, I'd like to share my personal story as well as shed light on five coding habits that I had to break in order to improve my coding skills. You can improve your coding skills and become a more efficient developer by understanding the negative impact of these behaviors and embracing good improvements.

Habit 1: Writing Long and Complicated Functions

When I first started coding, I had a habit of writing long and complex functions. I believed that complicated code was required to solve complex problems. Long functions, on the other hand, are difficult to understand, debug, and maintain. I increased code readability and made my applications easier to understand and troubleshoot by using a more modular approach and splitting my code into smaller units with clear responsibilities.

Here's why you should stop writing long and complicated functions:

  • Lengthy functions make it difficult to understand the code at a glance.
  • Maintaining and updating such functions becomes a daunting task, leading to potential errors.
  • Debugging complex functions becomes time-consuming and challenging, especially when issues arise.

For example:

// Long and complex function
function calculateTotalPrice(products) {
  let totalPrice = 0;

  for (let i = 0; i < products.length; i++) {
    let product = products[i];

    if (product.quantity > 0 && product.price > 0) {
      totalPrice += product.quantity * product.price;
    }
  }

  return totalPrice;
}

// Refactored into smaller, modular functions
function calculateProductPrice(product) {
  if (product.quantity > 0 && product.price > 0) {
    return product.quantity * product.price;
  }

  return 0;
}

function calculateTotalPrice(products) {
  let totalPrice = 0;

  for (let i = 0; i < products.length; i++) {
    totalPrice += calculateProductPrice(products[i]);
  }

  return totalPrice;
}

Enter fullscreen mode Exit fullscreen mode

In the example above, we start with a long and complex function called calculateTotalPrice. It iterates over an array of products, calculates the total price based on their quantity and price, and returns the result.

By refactoring the code, we break it down into smaller functions. The calculateProductPrice function calculates the price for an individual product, and the calculateTotalPrice function now utilizes this smaller function to calculate the total price for all products. This approach improves code readability, maintainability, and allows for reusability of the calculateProductPrice function elsewhere in the codebase.

By adopting this habit of breaking down code into smaller, modular functions, we can make our code more manageable and pave the way for better code organization and understandability.

Habit 2: Neglecting Code Documentation

Documentation is frequently regarded as a time-consuming process that may be easily postponed or ignored. I used to do this because I thought my code was self-explanatory, and honestly up until a few years ago, I didn't usually document my code. This technique, however, caused uncertainty for both myself and other developers working on a project. Adopting comprehensive documentation not only made it easier to understand and adjust my code in the future, but it also improved cooperation with coworkers, lowering development time and boosting overall product quality. If you don't know where to start, check out the links below:

Habit 3: Failing to Write Unit Tests

For many years, I thought unit testing was a waste of time that would only slow down my development process, especially when I'm freelancing on a project and the timeline is very short. However, I quickly recognized the enormous value of writing unit tests thanks to a very good friend. I noticed that by investing time early to establish detailed test cases, my code became more reliable, bug-free, and easier to maintain in the long term, especially since I switched to typescript. It's safe to say that part of the current criteria for getting a developer job nowadays includes unit testing and you might increase your chances of getting a job with just that. Unit testing also helped me refactor my code with confidence, knowing that I wouldn't introduce new issues by accident. This habit shift provided important peace of mind, a good night sleep and code stability.

Habit 4: Not Seeking Criticism/Assistance

I used to operate in isolation as a coder, rarely seeking help or criticism from other developers, mostly because I was an introvert, but that was not a substantial reason not to try. I thought that relying entirely on my own abilities would suffice and that apart from StackOverflow, StackExchange or GeeksforGeeks I would not need anyone's help or assistance either while trying to debug codes or finalizing a cool project.

Honestly, my coding abilities skyrocketed as I began aggressively seeking input from friends and professional developers, and that has helped me a lot in my career and improving my skillset (psst! My secret superpower is debugging other people's code). Feedback from people helped me gain new insights, identify potential weaknesses, and learn more efficient coding strategies.

Habit 5: Ignoring Version Control

One of my biggest career mistakes was not using GitHub back in 2016, when all my peers were using it. I overlooked the usefulness of version control systems like Git. I frequently found myself manually copying and saving code files with multiple names, which resulted in a disorganized and error-prone codebase. I started using GitHub in 2019, and I acquired the capacity to track changes, collaborate smoothly with others, and easily return to previous working versions by implementing version control methods. This habit increased my productivity and assisted in maintaining code integrity throughout the development process. So whether you are just starting out or you have been developing for a while now and you aren't using version control or aren't interested in using it, you are really getting behind. The best part is that you can start using Git right away.

Breaking coding habits might be difficult, but the benefits are enormous. I saw a substantial improvement in my coding abilities by reducing long and complex functions, embracing good documentation, using version control, creating unit tests, and seeking assistance and feedback. These habits boosted my productivity, reduced bugs, improved teamwork, and made me a more effective developer in general. So, take a step back, evaluate your own coding habits, and dare to break free from the ones that are stifling you. Your coding journey will thank you.

Thanks for reading.

Top comments (0)