DEV Community

Jeff Chavez
Jeff Chavez

Posted on

Can I Write A Clean Code?

I have been having difficulties in writing clean code. But the series of posts in Medium helped a lot. So here are the links to the posts and my takeaways that are worth sharing from part 1, part 2, part 3, & part 4 of the Writing Clean Code series by Logeshvar L.

  1. Avoid redundant information. E.g: UserWithNameAndAge is more redundant and is very lengthy. Prefer User
  2. Follow the casing conventions according to the language you’re using. Example: camelCase in Java/Javascript, snake_case in Python and so on.
  3. Be consistent in naming your variables/methods. Example: Avoid using getAge(), fetchName(), retrieveMail(). Instead, be consistent and name them getAge(), getName(), and getMail().
  4. Avoid slang, unclear abbreviations and disinformation (indicative of wrong data structure in the variable name, etc).
  5. Avoid comments that say the obvious or those which are misleading.
  6. Never comment out code, just remove.
  7. Use comments only when you want to provide licensing information, warnings or there is a need to provide API documentation.
  8. Use comments when code is difficult to grasp at a single sight and it would make it easier by providing some comments as explanations (e.g: regex).
  9. Properly format the code by adding blank lines to separate unrelated concepts and grouping similar/ related concepts together.
  10. Do not write an unreadably long line of code or name in a single line. Split them into multiple lines and use precise short names.
  11. Write functions with a minimum number of parameters.
  12. Keep your functions simple to read and understand without writing 100s of lines of code inside a single function. Instead, split into multiple short functions.
  13. Consider splitting of functions, but be sensible.
  14. Increase reusability and make code DRY (Don’t Repeat Yourself).
  15. Try not to mix different levels of abstraction in the function.
  16. Avoid unexpected side effects.
  17. Check if your function makes unit testing easier.

These are the takeaways that you can pretty much read in the blog posts. After reading the posts. I hope we can all answer the question above that Yes! We can write clean code with practice and tips provided in the blog posts.

I encourage you to read each post as he provides helpful examples and detailed steps/tips to implement writing clean code.

I'll surely get back to these posts as I write codes.

To God be the glory!

Discussion (1)

nombrekeff profile image

This might help, clean code comes with a lot of time and effort, but I promise If you keep practicing, learning and applying it to any code you do, and you will get better at it. Nobody starts writing clean code, believe me. And most of us still do quite a bit.

Also clean code is a process. It's not clean the first time is written most times, it's after refactoring, and investing some time in is, that you get to clean-code.

So keep going and I'm sure you will get better at it!