DEV Community

Balamurugan D
Balamurugan D

Posted on • Originally published at balamurugan16.hashnode.dev on

Does Clean Code really matter?

Programmers looking at my code : your code is disgusting | programmer-memes, code-memes, program-memes | ProgrammerHumor.io

Cooking is an art, just like coding. It should be hygienic and done with care because people will consume the end product. Adding garnishes to your food makes it more appealing. Similarly, in programming, clean code is crucial. It ensures that your code is accessible to your colleagues and adheres to coding standards. Try this activity, take some piece of code that you have written, give it to ChatGPT and ask to roast your code. You can also give the code written by your co-worker whom you don't like or wanna take sweet revenge and enjoy!

I asked ChatGPT to roast my code. I never want to code again | code-memes, variables-memes, try-memes, bug-memes, date-memes, IT-memes, rds-memes, variable name-memes | ProgrammerHumor.io

With that said, Let's discuss the importance of clean code, methodologies, tools, and common misconceptions๐Ÿš€๐Ÿ˜Š.

What is "Clean Code"?

Clean code is the holy grail. It's error-free, easy to read, and follows best practices. We're talking about code that makes sense, whether you're coding in TypeScript or any other language. Let's break down the essentials.

Proper Naming: ๐Ÿท

Imagine naming your variables, functions, classes, and files sensibly, just like naming your dishes. Compare these two:

const isLoading = false;

// Avoid
const x = true;

Enter fullscreen mode Exit fullscreen mode

The isLoading variable communicates its purpose, while x leaves you scratching your head. Consistency in naming conventions is key. Camel case is common in TypeScript, but if you feel like using snake case and you and your team can stay consistent with it, then go for it ๐Ÿ.

Unit Testing: ๐Ÿงช

I used to skip unit testing for most of my career ๐Ÿ™ˆ, but when I finally gave it a shot, oh boy, did I have an awakening! ๐Ÿš€ More than just testing, it's the mindset shift that struck me. Writing testable code nudged me toward clean code practices. Not every piece of code is a testing candidate; if it's poorly implemented, testing becomes a nightmare๐Ÿงฉ. But the confidence boost from unit testing is like a shot of espresso for a developer! And when that refactor season arrives, your trusty unit tests will be there to make sure it's a smooth ride, keeping the codebase as error-free as a sunny day๐Ÿ˜Ž. I've been exploring tools like Vitest lately, and let me tell you, it's a game-changer! ๐ŸŽฎ Especially the Vitest UIit's a total knockout feature! ๐Ÿ’ฅ. So, yes, I've joined the unit testing fan club, and it's been a game-changer๐Ÿคฉ. Most of the time, if you write your code well, you won't need to do much mocking. And let's be real, mocking your code can be as frustrating as a riddle without an answer!๐Ÿคฏ. As for Test Driven Development (TDD), well, let's just say it's not my cup of tea. So No thanks! ๐Ÿ˜„

Unit test tiers | code-memes, test-memes, unit test-memes, date-memes, tests-memes | ProgrammerHumor.io

Consistent Formatting and Linting: ๐Ÿงน

Proper formatting is the backbone of clean code. Poorly formatted code is like serving a messy plate. It's not only hard to digest but also reflects a lack of care. Properly formatted code, on the other hand, is a treat for your fellow developers. And linting? It's another tool in your clean code arsenal. Tools like ESLint and Prettier help you maintain quality. Pro tip: Stick with Prettier for formatting; ESLint is for linting. You can even automate linting and formatting with tools like Husky. But hey, I like to do it manually. ๐Ÿ™Œ

Code review gone wrong | code-memes, IT-memes | ProgrammerHumor.io

Comments and Documentation: ๐Ÿ“

Comments should be the spice, not the main course. Use them sparingly and only when they truly add value. Nothing's worse than scrolling through endless comments in a massive code file. Instead, let your code do the talking with well-named functions and variables. ๐Ÿค

Don't Repeat Yourself (DRY) and Single Responsibility Principle (SRP): ๐Ÿš€

Avoid code duplication like the plague. Break your code into functions and modules, each with a single, well-defined role. It's like creating a perfectly organized kitchen. ๐Ÿณ

Code Reviews: ๐Ÿ‘ฉ๐Ÿ’ป๐Ÿ‘จ๐Ÿ’ป

Engaging in code review sessions with your peers and seniors can be an enlightening experience. You get to tap into their wealth of knowledge, and your code gets a chance to shine brighter. But hold on, not all seniors are coding wizards ๐Ÿง™, so choose your reviewer wisely.

More Goodies: ๐Ÿ“Œ

  • Version Control: Think of it as your code's time machine. It helps you track changes and collaborate seamlessly.

  • Error Handling: Every chef knows how to handle unexpected situations. Apply the same care to error handling in your code.

  • File Size: Keep your code files bite-sized. In TypeScript, those import statements can get pretty verbose!

  • Design Patterns: These are like secret recipes for scalable code. Use them wisely. ๐Ÿฐ

Remember, clean code is a journey, not a destination. It demands continuous attention and discipline. If you're hungry for more insights into clean code, check out Uncle Bob's book, "Clean Code," or his YouTube videos. By embracing these principles and making the most of JavaScript's toolkit, you'll cook up a codebase that's a delight to understand, modify, and collaborate on. Happy coding! ๐Ÿš€๐Ÿ’ป๐Ÿ˜Š

Top comments (0)