In a meeting last week, I was discussing with my colleagues what is good code.

What is good code and how to define good code.

To put it simply, the code you’ve just written is good code, and the code everyone else has written is garbage. At the more complex level, high cohesion, low coupling, OCP, SRP, ISP, all sorts of concepts could make a big book. Is a programmer who writes 1000 lines of code a better programmer than one who writes 100 lines of code a day?

Big men have been talking about this for decades at their leisure, most notably in the words of Uncle Martin.

The only valid measure of code quality :WTF/min — Robert C. Martin

With this classic picture, it almost perfectly defines what good code is.

There is no best code, only cursed code, the number of cursed, determines the code is good or bad.

So how do you write code that’s less cursed?

In a larger sense, it’s “clean,” which is at the heart of “Clean Code.”

Clean code has several cores:

  1. Style neat
  2. Logic is clean and tidy

First of all, write code had better have some cleanliness, variable name prefix, function name, change line blank line, these most basic format, it is best to do team unity, at least individual unity, each time to write a part of the code, through formatting to unify the style.

Clean code is like beautiful prose. – Grady Booch

In fact, code writing is just like literary creation. When writing poems, the ancients first looked at the matching of levels and gradients and the rhyme of music. Only with the unified style can we have a pleasant feeling when reading.

Second, write code to think first, programmers write code is not for machines to see, but for people to see. Good code, bad code, in the machine’s view, in fact, there is not much difference (except efficiency), nothing more than machine code.

Any fool can write code that a computer can understand. A good programmer is one who writes code that humans can easily understand. – Martin Fowler

Writing code is not for showmanship. A piece of code that is difficult to read will always invite more WTF. And this is why the Kotlin syntactic sugar in some places are often the cause of the WTF, everyone in the team’s perspective and ideas may not be the same, so it is sometimes the most simple code is one of the best code instead, although Java code syntax verbose, nonsense, but reads like a Chinese, easier to understand and maintain. Kotlin, on the other hand, if you don’t have a deep understanding of syntactic sugar and are ambiguous about the idea of functional programming, it’s easy to write code that doesn’t look like it. It’s known as Java-style Kotlin code. Many people write Kotlin just to use the syntactic sugar and write fewer lines of code.

Above, we can summarize, from the macro point of view, how to write good code.

  1. Standardize the naming style, such as the difference between public and private variables, naming resources in pinyin or translation, and at least don’t write generateWoDeDaiMa
  2. Common process unified code templates, such as request interface, display list data, refresh and load more, such operations as unified as possible to avoid thousands of people in front of
  3. Function single responsibility, as far as possible functional programming, function without side effects, reduce the use of member variables, global marker variables

Let’s get more specific and look at what good code looks like from a mobile perspective.

The mobile end, or big front end, is very different from the back end. The back end code is more logical code, with very little interface, and most of the maintenance is also logical change, while the big front end is the opposite. The UI is almost tied to the logic, and most of the changes are made to the UI.

When you understand this, you can laugh at those who argue over whether to use MVC, MVP, MVVM or MVVVVVIP in a higher dimension. The starting point of these arguments is the wrong one. From the front end, these layers are hard to fit in all code. “There is no silver bullet in software engineering.”

In the back-end architecture, the decoupling of hierarchical architectures such as MVC can make the whole easier to maintain and extend, but on the front end, both MVC and MVP must be tailored to specific usage scenarios.

Mobile business scenarios can be roughly divided into two categories: business logic code and functional logic code.

For functional logic code, similar to the back-end, good architecture can make the code more robust, and for business code, these architectural redundant easily instead, believe that a lot of people have been Presenter crazy experience, originally very simple business logic, with the use of an MVP, at the time of change, A bunch of redundant code changes. So, for business logic code, the idea is to write code that is easy to remove.

Business logic code usually does not contain too many complex logic (if there is, it should be borrowed from functional logic code), and in most cases, these have to deal with the logic and UI binding together, so, this time, easy to remove the code, can make the business easier maintenance, after all, under the condition of the current iteration, No piece of innocent code will survive the next big release.

So the rebar will say, are these masters so many years of experience useless?

Of course not, the architecture must be combined with the specific business to design, as mentioned earlier, logical function business, how to architecture how to architecture, design patterns and so on, write as many as possible. The pure business function, the design pattern, is to write as much as you can.

For business functions, these design patterns, or layered architecture ideas, are usually used only in parts of the appropriate scenario.

APP, for example, some of the design of the components have the same effect, these things can be known as components, such as Windows, buttons, dialog boxes, such as bigger again, start reading, bookshelf in the list, these things may have to use in many places, at that time, the VM MMVM, you can come in handy. Through the extraction of ViewModel, the reusability of UI can be strengthened. Through the design patterns such as Builder, policy pattern, factory pattern, the code can be reused more and used better.

So a little bit more specific, specific code, what is good code to write

Senior programmer Yang Guo has also been thinking about what is a good code this problem, think can not jump off a cliff, but fortunately in the end a blessing in disguise, Chaowen Road, xi can die.

Crape myrtle soft sword, used before 30 years old, accidentally wounded a righteous man ominously, but abandoned deep valley

Xuan iron epee, epee without front, big qiao does not work, before the age of 40 rely on it to run amok

After 40 years of age, it is not stuck in things, plants, trees, bamboo and stones can be used as swords. From then on, it is refined and gradually becomes a place where no sword can win with a sword

This is actually the true picture of programmers, just graduated programmers, with high spirit, ready to start the world with a keyboard, code how to write cow force how to write, set on a variety of frameworks, a variety of layers, a variety of design patterns, write still do not forget to add comments, this is a piece of dogu to fail code. After that, the business tumor smoothed the edges and corners, began to return to simplicity, every line of code is written as steady as a dog. Finally, I get into a state of self-obsession with writing code, often going days without writing a single line of code, and writing several lines at a time.

Therefore, what Yang Guo understood at the age of 40, we should understand early now, otherwise we may be unemployed at the age of 35.

First of all, no thought is permanent. Ten years ago, object-oriented programming was king. Ten years later, functional programming was king. Ten years hedong hexi, ten years in the software development world too much, who would have thought the concept of coroutines in c #, even after many years, been Kotlin take fire, the young programmers don’t debate, in order to reform the ideas to solve the problem of thought is good thoughts, more to learn about different ideas, let them in your body collision collision, It might just be a mess.

Second, want to code, first in the chest code, in order to see no code, write code first spend some time to sort out the logic, clear branch and abnormal situation, and then go to work, not in a hurry that little time, not to knock faster than who.

Again, use inheritance carefully, this should be singled out.

Inheritance, one of the three core principles of object-oriented development. Inheritance, which regulates a class with similar behavior as a child parent, creates the famous Banana Monkey Jungle problem that many developers have encountered. When you copy a class from another project to an existing project, or modify a newly introduced class, You also need to copy its parent class, and even its parent class, and a chain of classes, not to mention the inheritance diamond problem.

The problem with object-oriented languages is that each class carries a hidden environment with it. You want a banana, but you get a monkey with a banana and the jungle.

So inheritance should be used with caution, but how? In fact, there are two ways to solve this problem. One is to modify the architecture to avoid invalid and excessive inheritance design as much as possible. The other is to conduct behavior constraint through interface rather than inheritance, and conduct architecture through composition as much as possible.

The whole idea of Flutter is composition, everything is based on composition, and there are also mixins to provide blending of functionality, using functionality from other classes without pressure, rather than inheritance.

Ancient and modern into a big business, university, will pass through three realms: “last night the west wind wither green trees, alone on the high-rise, looking at the end of the world road.” This first scene is also. “I am not regretful for I am pining away.” This second situation is also. “Find him thousands of baidu in the crowd, suddenly look back, that person is in, the lights dim.” This third realm is also.

Anyone who is interested in Flutter can join my Flutter group.