DEV Community

Alin Climente
Alin Climente

Posted on

A pragmatic approach on writing clean code

I think clean code is highly opinionated. And, a piece of code may appear "cleaner" depending on who looks at it.

In a team you will encounter people with different backgrounds which developed a "sense" of how "clean code" looks like.

IMO there are 3 main stages the code should go thru:

  • code should work as expected;
  • code should scale;
  • code should be readable (clean);

Note that first stage may be enough and the other two may not be needed any time soon.

When writing code I consider the expected lifetime of the end product. I try not to waste time making something perfect because it will never be.

If the code I wrote satisfies the given requirements I'll do a shameless git push.

If I have some time left I'll make some speed optimizations I'll try to make it as "readable" as possible, almost "like a story". I will look for good variable names, flat and basic code, "eye pleasing" appearance. After a while if I'll encounter that code again I'll repeat the previous steps. Code will get in time better and better.

On a new web project I'll create the basic MVC structure. I start by defining the models/schemas and think of the relationships that occur.

After that, I will start creating the crud methods for those models and extend those methods if needed.

Once that's done, I'll start creating the required endpoints.

When working on the the frontend I may go back and modify some views, but at this point the changes are pretty small.

My current most productive stack is FastAPI, Jinja2 (with AlpineJs) and MongoDB. With FastAPI the ease you can create OpenAPI docs exceeds expectations. Not having to worry about a different JS stack makes things faster to develop. Jinja2 does a great job on generating html from custom data. For the things Jinja can't handle on the frontend I use AlpineJs - no build step, simple to use and surprisingly powerful. As a bonus SEO is great, no need to keep routing on the backend and on the frontend + other stuff.

Probably I'll get the same productivity if I'd have to use a full stack Javascript framework or other full stack frameworks in other languages, but Python is a personal preference.

Obsessing over clean code doesn't do any good to anyone. Having a pragmatic approach when it comes to writing software IMO is way better and benefits all.

In time with or without learning SOLID, KISS, DRY, design patterns etc everyone will end up naturally writing clean code. Learning them upfront will speed things up, but taking them one by one and having a deep understanding in practice on how to use them is better.

Trying to fit a problem in a design pattern may sometimes have a negative impact. First solve the problem, then see what you can improve later.

Clean code is the "holly grail" of software development. Many will say that they have a bespoke process and write the "cleanest" and most maintainable code. Lies, dark corners will be on every codebase that's running in the wild.

Top comments (0)