Get the 2024 Yearly Goals and Progress Tracker Notion Template for FREE!

How CSS Methodologies can enhance Code Efficiency

SERIES: The Productive Developer

Posted on: Mar 12, 2023

7 mins read


My experience on Debugging a Poorly Written Codebase to Create Responsive Webpages

AHow CSS Methodologies can enhance Code Efficiency

As developers, we've all been there: handed a codebase that was done without proper planning, leaving us to navigate through a tangle of disorganized code. This can be incredibly frustrating when it comes to debugging CSS, where the improper use of CSS can make development and troubleshooting more difficult and time-consuming. In this article, I'll share my experience working with a poorly written codebase, how I tried to debug responsive web design issues, and the importance of using proper CSS methodologies to make development more efficient and effective.

When you try to solve a bug in CSS by using !important everywhere and z-index:9999999.

The Problem and Challenges

I was working for a startup, and they were using Bulma CSS for their website. Most of the pages were already created but needed to be functional and more responsive, which was a significant issue. The problem was that the original developer had not used proper CSS methodologies. They used Bulma CSS alongside vanilla CSS and inline CSS to override styles. And when these methods didn't give them the desired result, they used the !important property even within the inline CSS, making the codebase a tangled mess. This made it very difficult to debug the CSS code and make the pages more responsive.

two ladies standing at a party staring at something in shock. one of them is saying 'this is a mess'.

In addition to that, I noticed that the developer had used unclear or nondescriptive names like pt and mb to target elements on the page. This made it difficult to understand the code and made it harder to make changes. To make matters worse, they also attempted to override the styles of the Bulma CSS class names by targeting names that were not working the way they wanted, resulting in a massive clash in styling, which made it nearly impossible to make significant changes without breaking something else.

However, I was determined to overcome these challenges and find a way to make the codebase more efficient and effective.

Lady saying 'And I am not going to let anything get in the way of making it amazing'.

The Solution

When I first encountered the poorly written codebase, I was initially hesitant to start from scratch. After all, the original developer had already put in a lot of work, and I didn't want to dismiss their efforts completely. However, as I dug deeper into the code, it became clear that the existing structure was too tangled and complex to work with efficiently. It was difficult even to understand what the original developer was trying to accomplish in some areas of the code.

A man talking to another man saying 'I am thoroughly confused. What is going on?'

So, I decided to take a step back and reassess the situation. I realized that the best way forward was to start with a clean slate and rebuild everything using proper CSS methodologies. It wasn't an easy decision to make, as it meant throwing away a lot of work that had already been done. However, I knew that trying to work with the existing code would ultimately be more time-consuming and frustrating in the long run.

Using the Bulma CSS library proved to be the right choice, primarily since it was already being used in the codebase. By starting fresh and utilizing a standardized library, I was able to create a more responsive and efficient codebase. It also allowed me to cut down the code significantly, making it easier to maintain and update in the future.

Overall, the experience taught me the importance of using proper CSS methodologies and how it can save a lot of time and effort in the long run. While it can be tempting to take shortcuts and use quick fixes like !important or inline styles, these approaches often lead to messy, difficult-to-maintain code. By prioritizing readability, organization, and consistency, developers can create high-quality code that is more efficient and effective in the long run.

A man saying 'Lessons have been learned'.

Importance of using proper CSS methodologies

CSS is a crucial aspect of web development, and it's critical to ensure proper CSS methodologies for the highest efficiency and effectiveness. One way to achieve this is by utilizing CSS frameworks or libraries, which provide standardized styles and functionality that make development more efficient.

Using a CSS framework or library can help improve the quality of CSS code. These tools provide standardized styles and functionality that can make development more efficient and collaborative. However, it's crucial to understand how the framework or library works and not rely solely on it without a solid understanding of CSS fundamentals.

It can be tempting to rely solely on these CSS frameworks or libraries to quickly achieve desired styles or layouts. Still, without a foundational understanding of CSS, this can quickly lead to a tangled mess of code. It's like trying to build a complex machine without first learning how the individual components work together.

A master saying to his pupil 'First learn stand, then learn fly'.

Writing clean, well-organized CSS code is crucial for easy readability, comprehension, and debugging. It also allows developers to work more efficiently and collaborate more effectively. Developers can avoid common pitfalls and create high-quality, maintainable code by prioritizing a deep understanding of CSS before using frameworks or libraries.

A consistent naming convention for CSS classes and IDs is also crucial for maintaining a clean and organized codebase. It's essential to use descriptive and meaningful names for classes and IDs that clearly convey their purpose and the element they are targeting. When naming classes and IDs, using lowercase letters and hyphens to separate words is also best. This not only helps you understand your own code but also makes it easier for other developers to read and work with your code.

Additionally, following proper naming conventions allows for better collaboration and communication between developers, making it easier for others to understand the code's purpose and target elements. By following a consistent naming convention, you can create more maintainable and scalable code, which is essential for long-term success in web development.

It's important to understand that the use of "!important" should be reserved for exceptional cases where it's necessary to override styles that cannot be changed otherwise. The overuse of "!important" can make code maintenance and debugging more complicated, especially when dealing with large and complex codebases. When "!important" is used excessively, it can create a complex mess of conflicting styles, making it difficult to understand and modify the code. Learn more about the dangers of using !important.

Although there are various CSS methodologies that developers can use to write clean and maintainable code, the codebase in question needed to be improved in these areas. By following established CSS methodologies, such as using descriptive class and ID names, keeping code organized and easy to read, and avoiding the overuse of "!important," developers can ensure that their code is maintainable and scalable. While learning and implementing these methodologies may take some extra time and effort, the benefits of a well-structured and maintainable codebase are worth it in the long run.

Conclusion

In conclusion, as developers, we must prioritize proper CSS methodologies for efficient and effective web development. Neglecting these practices can lead to tangled and disorganized code, making it challenging to make changes or maintain the codebase. Using a CSS framework or library can be helpful. Still, having a foundational understanding of CSS is crucial to avoid common pitfalls.

Moreover, it's essential to follow proper naming conventions and avoid using !important, which can make code maintenance and debugging more complicated. By writing clean, well-organized CSS code, we can work more efficiently and collaboratively, leading to a more maintainable and scalable codebase.

Overall, taking the time to develop a solid understanding of CSS and utilizing proper CSS methodologies can significantly improve the quality and efficiency of our codebase, making our work as developers more enjoyable and effective.

Thank you for making it this far. I would love to hear your experiences and thoughts on the topic. You can share them with me on X (Twitter) or LinkedIn! Till next time guys...

man smiling and waving goodbye

Image from

giphy.com

Connect With Me

Follow me on X(Twitter), and LinkedIn to stay updated with my latest content.

If you like my notes and want to support me, you can sponsor me on GitHub Sponsor, or you can buy me a virtual ice cream on ByMeACoffee or Selar. I would really appreciate it. 🙏

For other ways to support me, visit my Sponsorship page or Affiliate Links page.

Subscribe to my newsletter 💌

Stay in the loop on the latest articles, tutorials, projects, and exclusive content focused on web development! 💻📚✨