My opinion on efficient code => Li Qifeng

There is still one month to end the sophomore life. The following are just some of the experiences and insights I have learned from my arrogant freshman year and I have learned from less than two years of study and work. Please leave a message in the comment area for deficiencies

Project conception and detailed design

After meeting the needs, first sort out the general logic, and then conceive the project structure and process. It is best to put the result of the idea into the outline design and the detailed design, so that the code will be more smooth.
Code mind often before there is a prototype begin to attack at the time of writing, and later will continue to promote and improve the project, we found that many do not take into account some of the things before, the result is often to be reconstructed prior to
detailed design Included content:
1. Program design description
2. Program description
3. Function and performance
4. Input and output items
5. Algorithm and process logic
6. Interface
7. Storage allocation
8. Annotation design
9. Unresolved problems
10 , UML class diagram

Make good use of packaging

In the team, a project is divided into several modules, and everyone is only responsible for completing and maintaining their own modules. Therefore, when writing code, it is often necessary to consider the scenario where others are calling their own code. How to complete a project efficiently and clearly. That is, the modules that each person is responsible for are relatively independent but can be connected to each other. At this time, packaging is needed. Every time a function is completed, it is encapsulated as a function. Functions are independent of each other and call each other.
I now feel that writing code is like building blocks. An independent module is made up of many concrete building blocks, and multiple modules can be built into a concrete object. Often a box of building blocks can be assembled into many objects, but the assembled structure is different.
The same is true for writing code. A function consists of lines of code and can complete a specific function. Splicing together a plurality of functions can be carried out actual production applications
take advantage of the package, good assembly

Project ability, coding ability, learning ability

For me, I think a qualified software practitioner needs to have the following three abilities

  1. Project ability: The ability to understand the project. In the product, it can also be considered the ability to understand the demand. This is specifically reflected in the ability to understand and analyze the composition and requirements of a project when getting the project, then conceive the project structure and process, select the required technology and language, and be able to come up with a set of preliminary solutions. I think this part of the ability accounts for 40% of me. I can do at most 20% now. Just give me a project, I can understand the composition and requirements of a project, and then write the corresponding functions according to the requirements
  2. Code ability:
    One refers to the level of code writing. Efficient, beautiful, concise, standardized, clearly hierarchical, and highly readable.
    The second refers to the ability to implement requirements, that is, how to implement a specific requirement or function through code. I think the above two abilities account for 35% of me.
    For the first point, what I can do is to write complete comments, class comments, variable comments, and function comments. Use inheritance and encapsulation code more often, try to ensure that the hierarchy is clear. As for efficiency, I don’t think it’s enough. Although every time I finish writing the code, I have to go back and check it twice to see if there is anything to be optimized, but I feel that something is missing. In the final analysis, it is a lack of understanding of the language itself and some underlying mechanisms. This aspect is also my focus of strengthening in the future.
    Regarding the second point, when I was in school, I wrote code step by step, just imitating the code given by the teacher or the book. At work, it's all on your own. At present, for my book, I can only realize some simple requirements by myself. For the kind of functions that rely on distributed, asynchronous, and coroutines, I still can't do anything.
  3. Learning ability: In actual work, you will always be exposed to new technologies and new tools. How to get started quickly is also an indispensable ability for a software practitioner. I think this part of the ability accounts for 25% of me. I own on my evaluation, it started new technologies speed is not too slow, with the accumulation of future expansion of knowledge, experience, and I believe the new technology will be learning more and handy
    above three points is my summary The company is exposed to the experience summarized in the actual project. To tell the truth in school, it is still too comfortable. The step-by-step learning will make one blind and inflated, thinking that one's level is enough, and it is easy to deal with the needs of teachers or books. The result is often a blow to the enterprise. I think: studying in school is like building a bungalow, you just need to be able to pile up the bricks. The work of an enterprise is like building a building, and the difficulty and complexity increase exponentially with the size of the project

Flexible use of grammar

How to write efficient and concise code: Use the syntactic sugar that comes with the code
Example: Use of lambda in Java

//传统方式遍历列表
List features = Arrays.asList("Lambdas", "Default Method", 
"Stream API", "Date and Time API");
for (String feature : features) {
   System.out.println(feature);
}

//使用lambda遍历列表
List features = Arrays.asList("Lambdas", "Default Method", "Stream API",
 "Date and Time API");
features.forEach(n -> System.out.println(n));

It can be seen that the use of lambda is helpful to the conciseness and readability of the code.

Note: I did not how specific contact lambda, lambda above example is just the easiest to use
on the java lambda more in-depth explanation can refer to this blog: in-depth understanding of Java 8 Lambda
usage can refer to the article: the Java 8 ten Examples of lambda expressions

Example 2: Use of list comprehension in python

# 使用传统的遍历将列表中的单词全都变为小写
word_list = ['Word', 'List', 'Python', 'Liqifen']
low_list = []
for word in word_list:
    low_list.append(word.lower())

# 使用列表生成式将列表中的单词全都变为小写
word_list = ['Word', 'List', 'Python', 'Liqifen']
low_list = [word.lower() for word in word_list]

The list generation is not only more concise, but also more efficient because of the syntax that comes with python

Each language has its own characteristics and unique grammar. Good use of these grammars will improve code efficiency and simplify code structure.

Selection and use of design patterns

This is an extended content, and I will add it later when I become proficient in my work. The singleton model has been the most exposed so far.
However, it is obvious that with the increase of code, it is very important to choose a suitable design pattern. I can't say exactly where it is important. After all, Taoism is too shallow...

Small summary

From freshman to now, learning bits and pieces of Andrews, jsp, JavaScript, Redis, MongoDB , MySQL, Hadoop, Spark, ELKB framework ... and some can not remember
, but there have been only three in the learning and use of One, Java, Python and Linux, and now I started to learn Scala because I wanted to develop the Spark project.
How to put it, I feel that when making a project or making a product, the choice of language, tools, and database is very important, but no matter what tool or database, whether it is open source, whether it is integrated by others, or it is a function written in code by yourself In the end, it is necessary to use code to connect them into a whole, and also to write a complete set of process logic, so that each component can complete specific production work according to the process.
So I think that the tools or language is not the most important thing in the work. The most important thing is project ability, coding ability and learning ability.

The above are the five points of experience I have summarized so far. I will add more practical problems later.

Guess you like

Origin blog.csdn.net/mrliqifeng/article/details/80699348