Discuss programming principles

Tram Ho

What is the programming principle.

Building and developing software is a process that involves many steps, starting from writing code, writing tests, then fixing bugs, and then maintaining … this process is repeated. And how to make that process better, better from the beginning of code writing, people start thinking about programming principles, and hopefully when people follow, everything will be improved. good.

So after there are programming principles, will your programming become better, that I would like to let you answer yourself.

How many programming principles are there.

When I started to learn about programming principles, an open world, and then it made you many times want to stop learning. Too many principles. Too many tips. Too much imposition that we have to do this, have to do that.

There are really many people in us who don’t Google but remember the names of those principles.

Why it is difficult to comply with the rules.

We can easily create principles for others, not principles for ourselves. And people often tend to break the rules. Following the principles requires proper understanding, maintaining and establishing habits, and of course thinking about it is difficult.

Microchip offers software that helps programmers “speed up” the design
How to choose a programming language is reasonable?

Why don’t we create our own rules?

Principle of code so simple (simple)

When the code is always thinking about making it as simple as possible. Simple code will have benefits:

  • Team members can follow it easily.
  • Later, new people are also easy to receive
  • We ourselves re-read later to improve and maintain easily.
  • In particular, when they are simple, failure will be easy to repair.

It’s easy to say, but the computer program itself is a very complicated thing, we today are code that is simple and easy, both a process of improvement for many years, and advancement in the way New legislation was born. Imagine, this old web code with complex frameworks, immediately on Laravel, Reactjs makes PHP or Javascript code much easier. The most complex parts have been hidden inside.

So how can the code be simple, that the author doesn’t know, but we can avoid one thing that leads to complex code.

  • Please divide the big function into smaller functions.
  • Don’t write algorithms, some algorithms are like to optimize this and the other, save this one and the other, then make the code difficult to understand.

Principle code so neat, clean (clear & clean)

In general, you like people who are neat, clean, neat, clean, no one likes code looking messy and headaches. How to make the code neat and clean:

  • Give yourself a set of rules (Coding Convention)
  • Let’s start by naming variables neatly, easy to understand
  • Design functions that are easy to read and understand
  • Make the functions have clear input and output
  • Inside functions restrict if / else / for in a way that is too deep

And be patient to pursue your principles thoroughly, remember that it is not simple to make things neat and clean, go through many improvements, draw experience, review You just set yourself up a set of skills needed to maintain those habits.

Principle of code so that it can be easily reused (reusable)

If you can’t reuse the code again and again, every time you fix or change it, you can easily create a bug, because forgetting this and that, and reading all the code is hard to understand.

The code to reuse is also very difficult, because at first you just try to write it to work, not write code for tomorrow, for the other guy it uses, worrying about tomorrow is a very bad novel structure and fantasy.

But writing code to reuse, help you think about the problem, benefit you, when you think about it, means you are isolating your problems to help you see. out better problems.

Principle of separating code according to independent class / module / package (decoupling)

  • Each class should only do one thing, so separate the program into different classes.
  • Each module / package should only do its domain work.
  • Inside each class / module / package hide, the implementations are not necessarily public.

In addition, nowadays, all languages ​​have management packages such as PHP’s Composer, npm of Nodejs, Go has Go Module, etc. This, helps not only code, but also organization for an entire environment more, like architecture for companies, even public we become open sources to contribute to others.

Principle of composition rather than composition over inheritance

Inheritance leads to complexity because they are interdependent, and to avoid dependence we must constantly create overlapping inheritance. And it’s only complicated to trace the features from the beginning.

The computer world itself is designed from electronics, not biology, I myself believe there is a whole set of independent components and are put together. Because of that, basic programming is also independent components that are tied together through interfaces, and communicate through interfaces.

  • Let’s look at recent advances like React that are components that are tied together.
  • Notice that the Go is embedded in the struct
  • Notice that today, PHP has traits

Epilogue

So what do you think of your principle?

Principle of KISS
WHAT IS SOLID – APPLY SOLID PRINCIPLES TO BECOME MEMBER CODE “HARD
Share the news now

Source : viblo