How to write good code?

Discuss hot database and enhance operational efficiency together.
Post Reply
shukla7789
Posts: 57
Joined: Tue Dec 24, 2024 4:29 am

How to write good code?

Post by shukla7789 »

As a programmer, I remember the first program I ever wrote. I remember the difference between those first lines of code and the ones I write today. Although I am surprised by the natural evolution of our writing, I can't help but wonder why many of us don't constantly try to improve our code writing? And why do we stop improving when we reach a certain level?

I was about 16 years old when I first wrote instructions for a computer, a few pages in binary for a Zx Spectrum 128k . It was addictive, challenging and I loved it. I remember, above all, that it was my first frustration. Long hours copying code, without it working!

After all these years, I have not stopped loving writing code, constantly seeking to evolve in this area and learn techniques to avoid the feeling I experienced at the time.
Assuming that writing code has a greater influence on quality, when compared to requirements or tests, I share some rules that I have in my toolbox and leave some questions to make you think.

The cost of owning bad code
Anyone who is a programmer knows that, after a few years, they will kuwait whatsapp number database have to maintain code written by themselves or others. We have probably all had this experience, and probably had difficulty reading, maintaining and evolving that same code. We also know the feeling of creating a problem by trying to fix another, giving rise to what is commonly known as rot . This code is called legacy code .

Thus, as the code evolves, with each addition or modification, the team's productivity tends to decrease. At the limit, we could say that productivity tends to zero .
When faced with a new feature, one of the rules I maintain is that the code must be written with a view to being maintained and, of course, undergoing evolutions . Regarding the existing code, I always remember the good scout's rule that whenever you set out on a new piece of land, "you have to leave it cleaner than you found it" . We can translate these good practices into a simple rule: never lower the requirements regarding the quality of the code delivered.


Legacy Code
The term legacy code has a negative and heavy connotation, as it is understood as complicated code, difficult to maintain and evolve. I came across a description, which despite being debatable, states that legacy code is just code without tests.

I can't help but agree that good code must be accompanied by automatic tests, whether functional or unitary . Tests allow refactoring, while maintaining the rules of the original code , allowing it to be improved without fear for the programmer. For someone who has been part of an automatic testing team, my sensitivity to this subject is clear and, for each problem, I always evaluate its applicability from the point of view of the tests.


Code Reviews, Pair Programming and Mob Programming
Culturally, we have difficulty in carrying out code reviews and we lack discussions about architecture, so some questions arise for me: could a community disseminate best practices among programmers? Or is this principle based on the techniques that generate this code? How can we get people to write better code?

A basic principle is that teams that write good code can ensure that new code does not become legacy tomorrow. The same cannot be said for written code, as it will be difficult to improve and maintain.

We can talk about culture, or the lack thereof, but there is a difficulty in making code review requests . This is a way for me to personally evolve in my writing , allowing me to:

1) evolve with other people who write better code than me;
2) avoid mistakes;
3) transfer of knowledge.

Other techniques are just honorable mentions that don't even deserve a place outside the podium, sometimes reminiscent of public service, where one program and the other apparently do nothing. The idea is that, when applied correctly, these techniques serve to increase productivity and level knowledge among peers, minimizing the introduction of problems in the code.

About good code
After all, writing good code is an art , but what does writing good code mean? There is one definition that I would like to highlight:


"Clean code can be read, and enhanced by a developer other than its original author. It has unit and acceptance tests. It has meaningful names. It provides one way rather than many ways for doing one thing. It has minimal dependencies, which are explicitly defined, and provides a clear and minimal API. Code should be literate since depending on the language, not all necessary information can be expressed clearly in code alone.”

Dave Thomas


Writing code is a task that should be done with the next programmer, the next tester and, ultimately, the client in mind. The programmer will be able to improve the code, the tester will be able to perform higher quality tests and the client will benefit from a more resilient application that can include features without breaking existing ones.

This definition ended up leaving out an important rule that involves avoiding code duplication , which is the arch-enemy of those who write code and the main cause of increased complexity, increased risks and increased work. The programmer must adopt the rule of the lazy or the stutterer that dictates that "the less code the better" . Duplication hides, in many cases, serious architectural problems.

Conclusion

Each programmer has the techniques available in their toolbox , and there are other topics that also deserve attention, from the importance of knowing how to debug , to refactoring mechanisms , as well as the famous term technical debt .

I believe that the programmer has a greater influence on quality than any other role within a development team, and that writing code is of vital importance to the final result of a program. That is why writing better code is the responsibility of every programmer, so that he can be called a true professional. There is no excuse for not doing the best possible!
Post Reply