当前位置:网站首页>How to write high-quality code?
How to write high-quality code?
2022-07-05 10:13:00 【People mail asynchronous community】
Catalog
Per person Software Engineering Teachers want to write high-quality code , High quality code is equivalent to easy maintenance 、 Easy to read 、 easy Expand 、 flexible 、 concise 、 Reusable 、 Testable code .
Want to write high-quality code that meets the above code quality evaluation criteria , We need to master some details 、 A landing series Process methodology , Including object-oriented design paradigm 、 Design principles 、 Code specification 、 Refactoring skills and design patterns . And master this The ultimate goal of some programming methodologies is to write high-quality code . These programming methodologies are the focus of the subsequent chapters , Let's get familiar with them first .
1 object-oriented
at present , Programming paradigm or programming style mainly includes 3 Kind of : Process oriented 、 Object oriented and functional programming . object-oriented programming Style is the mainstream . Most of the popular programming languages now belong to object-oriented programming languages . in addition , Most projects are also It is developed based on object-oriented programming style . Object oriented programming has rich characteristics ( encapsulation 、 abstract 、 Inheritance and multi state ), Can realize a lot of complex design ideas , therefore , It is a lot of design principles 、 The foundation of design pattern coding .
For object orientation , Readers need to master the following 7 A knowledge ( See the first 2 Chapter ).
1) Four characteristics of object-oriented : encapsulation 、 abstract 、 Inheritance and polymorphism .
2) The difference and connection between object-oriented programming and process oriented programming .
3) Object oriented analysis 、 Object oriented design and object oriented programming .
4) The difference between interface and abstract class , And their respective application scenarios .
5) Design ideas based on interfaces rather than implementation programming .
6)“ Multipurpose combination , Use less inheritance ” design idea .
7) Process oriented “ anemia ” Model and object-oriented “ congestion ” Model .
2 Design principles
Design principles are the experience summary of code design . The design principle has a feature : These design principles look Abstract , The definition and description are vague , Different people have different interpretations of the same design principle . therefore , If we are simple Memorize their definitions , So programming 、 The improvement of design ability is not very helpful . For every design principle , I We need to know what problems and application scenarios it can solve . Only by mastering these contents , We can be flexible in the project 、 Appropriate Apply these design principles . actually , The design principle is mind method , Design patterns are moves . therefore , The design principle is better than the design model Universal formula 、 important . Only master the design principles , We can clearly understand why we use a certain design pattern , And just To apply design patterns profitably , It can even create new design patterns .
For design principles , Readers need to understand and master the following 9 Kinds of principles ( See the first 3 Chapter ).
1) Principle of single responsibility (SRP).
2) Opening and closing principle (OCP).
3) Richter's principle of substitution (LSP).
4) Interface isolation principle (ISP).
5) Dependency Inversion Principle (DIP).
6)KISS principle 、YAGNI principle 、DRY Principles and LoD The laws of .
3 Design patterns
Design pattern is a set of solutions or design ideas summarized for some design problems often encountered in software development . Most design patterns solve the decoupling of code 、 Scalability issues . Relative to design principles , Design patterns are not that popular like , And most of them are not difficult to understand , The code implementation is not complicated . Learning design patterns , We need to focus on it What problems and typical application scenarios can we solve , And not overuse .
With the evolution of programming languages , Some design patterns ( As in the singleton mode ) Becoming obsolete , Even become anti pattern , Some settings Meter mode ( Such as iterator mode ) Is built into the programming language , There are also some new design patterns , Such as singlet mode .
In this book , We will focus on 22 A classic design pattern , They fall into three categories : Creation type 、 Structural type and row It is type . Here 22 Of the design patterns , Some design patterns are commonly used , Some design patterns are rarely used . For commonly used settings Meter mode , We need to spend more time understanding and mastering . For less commonly used design patterns , We can understand .
By type , We have simply classified the design patterns mentioned in this book .
1) Create design patterns : The singleton pattern 、 Factory mode ( Including simple factory mode 、 Factory method model 、 Abstract work Factory mode )、 Builder mode and prototype mode .
2) Structural design pattern : The proxy pattern 、 Decorator mode 、 Adapter pattern 、 Bridging mode 、 Facade mode 、 Combine Mode and sharing mode .
3) Behavioral design patterns : Observer mode 、 Template method pattern 、 The strategy pattern 、 Responsibility chain pattern 、 The state pattern 、 Iterator pattern 、 Visitor mode 、 Memo mode 、 Command mode 、 Interpreter pattern and mediation pattern .
4 Code specification
Code specification mainly solves the problem of code readability . Relative to design principles 、 Design patterns , The code specification is more Body and emphasis on code details . If the project developed by the software engineer is not complex , Then you don't have to understand the design principles and master Design patterns , But at least master the code specification , Such as variable 、 Naming conventions for classes and functions , Specification of code comments etc. . therefore , Compared with the design principles 、 Design patterns , Code specification basis and important .
however , Relative to design principles 、 Design patterns , Code specifications are easier to understand and master . Learning design principles and design patterns requires a lot of personal understanding and thinking , But learning code specifications doesn't need . Every code specification is very simple and clear indeed , Readers just follow suit , therefore , This book does not spend much time explaining all the code specifications , It's a summary The author believes that it can effectively improve the code quality 17 Rules .
In addition to code specifications , The author will also introduce some code “ Bad taste ”, Help readers understand what kind of code is not Conforming to the specifications , And how to optimize . Refer to code specification , Readers can write readable code ; After understanding the generation The code “ Bad taste ” after , Readers can find out the readability problems of the code .
5 Refactoring technique
In software development , As long as the software keeps iterating , There is no once and for all design . As demand changes , The code is not Stop stacking , The original design must have problems . To address these issues , We need to refactor the code . Refactoring is software development An important part of hair . Continuous refactoring is an effective way to keep the quality of code , Can effectively avoid code “ corrupt ” To “ past hope ” The point of .
Refactoring tools include object-oriented programming paradigm 、 Design principles 、 Design patterns and code specifications . actually , Design principles and The important application scenario of design pattern is refactoring . We know , Although design patterns can improve the scalability of code , But too Or use it inappropriately , It will increase the complexity of the code , Affect the readability of the code . At the beginning of development , Unless necessary , I We must not over design , Apply complex design patterns , But when the code goes wrong , Let's focus on the problem , Apply design principles and design patterns to refactor , This can effectively avoid the early over design problem .
About refactoring , This book focuses on the following 3 Aspects of . Through the explanation of these contents , I hope readers can not only Master some refactoring skills , The more important thing is to establish the awareness of continuous reconstruction , Refactoring as part of development , Integrate into the daily opening In hair .
1) The purpose of refactoring (why)、 object (what)、 opportunity (when) And methods (how).
2) Technical means to ensure that refactoring does not go wrong : unit testing , And the testability of the code .
3) Two different scale refactorings : Big reconstruction ( On a large scale , A high level ) And small refactoring ( small , Low level ).
Here is a summary of object-oriented programming 、 Design principles 、 Design patterns 、 The relationship between code specification and refactoring skills .
1) Because of its rich characteristics, object-oriented programming paradigm ( encapsulation 、 abstract 、 Inheritance and polymorphism ), Can achieve many complex Design idea , therefore , It is a lot of design principles 、 The foundation of design pattern coding .
2) Design principles are some experience summaries that guide code design , It is the mental method of code design , Indicates the size of the code design Direction . Compared with design patterns , It is more universal .
3) Design pattern is a set of solutions or design ideas for some design problems often encountered in software development road . The main purpose of applying design patterns is decoupling , Improve the extensibility of the code . In terms of abstraction , The design principle is better than Design patterns are more abstract . Design patterns are more specific , Easier to implement .
4) Code specification mainly solves the problem of code readability . Compared with the design principles 、 Design patterns , The code specification is more specific 、 More emphasis on code details and more practical implementation . Continuous small refactoring mainly depends on the theory of code specification .
5) Refactoring is an effective way to keep the quality of code , It depends on the object-oriented programming paradigm 、 Design principles 、 Theoretical knowledge of design patterns and code specifications .
actually , Object oriented programming paradigm 、 Design principles 、 Design patterns 、 Code specification and refactoring skills are both maintained or improved Methodology of code quality , In essence, it is all about writing high-quality code . When we see this essence after , Many choices are clear about how to do . for example , In a certain scene , Whether to use a design pattern , The criterion of judgment is Whether it can improve the code quality .
actually , Want to write high-quality code , In addition to accumulating the above theoretical knowledge , We also need to carry out a certain intensity deliberate practice . Many programmers have mentioned , Although I have learned relevant theoretical knowledge , But it's easy to forget , And in the face of problems I can't think of the corresponding knowledge points . actually , This is the lack of deliberate training of combining theory with practice . for example , At school Hou , After the teacher explained a certain knowledge point , Often cooperate to explain several examples , Then let's strengthen it through exercises after class This knowledge point . such , When we encounter similar problems again , You can immediately think of the corresponding knowledge points .
In addition to mastering theoretical knowledge 、 Beyond deliberate training , Having awareness of code quality is also very important . Before you write the code , We want to Think more about the expansion needs in the future , Which part of the code will change , Which part of the code is unchanged , Writing code like this will not It will make it difficult to add new functions in the future 、 The readability of the code is not high . With such code quality awareness , It's not far from writing high-quality code .
The above answer is taken from 《 Beauty of design pattern 》
This book combines real project cases , From the object-oriented programming paradigm 、 Design principles 、 Code specification 、 Refactoring techniques and design patterns 5 Three aspects describe in detail how to write high-quality code .
The first 1 Chapter is an overview , It briefly introduces the modules involved in this book , And the links between the modules ; The first 2 This chapter introduces the object-oriented programming paradigm ; The first 3 Chapter introduces the design principles ; The first 4 Chapter introduces the code specification ; The first 5 Chapter introduces refactoring techniques ; The first 6 This chapter introduces the creative design pattern ; The first 7 This chapter introduces the structural design pattern ; The first 8 This chapter introduces behavioral design patterns .
This book can be used as a study for various R & D engineers 、 Furter Reading , It can also be used as a teaching and learning book for teachers and students of related majors in Colleges and universities , And the teaching materials of computer training school .
边栏推荐
- RMS to EAP is simply implemented through mqtt
- Optimize database queries using the cursor object of SQLite
- Z-blog template installation and use tutorial
- La vue latérale du cycle affiche cinq demi - écrans en dessous de cinq distributions moyennes
- Applet image height adaptation and setting text line height
- Flutter development: a way to solve the problem of blank space on the top of listview
- 苹果 5G 芯片研发失败?想要摆脱高通为时过早
- Swift tableview style (I) system basic
- 《微信小程序-基础篇》小程序中的事件与冒泡
- 让AI替企业做复杂决策真的靠谱吗?参与直播,斯坦福博士来分享他的选择|量子位·视点...
猜你喜欢
盗版DALL·E成梗图之王?日产5万张图像,挤爆抱抱脸服务器,OpenAI勒令改名
Design and Simulation of fuzzy PID control system for liquid level of double tank (matlab/simulink)
@SerializedName注解使用
Those who are good at using soldiers, hide in the invisible, and explain the best promotional value works in depth in 90 minutes
. Net delay queue
《微信小程序-基础篇》小程序中的事件与冒泡
如何判断线程池已经执行完所有任务了?
高级 OpenCV:BGR 像素强度图
《天天数学》连载58:二月二十七日
Meitu lost 300 million yuan in currency speculation for half a year. Huawei was exposed to expand its enrollment in Russia. Alphago's peers have made another breakthrough in chess. Today, more big new
随机推荐
MySQL字符类型学习笔记
QT realizes signal transmission and reception between two windows
Constraintlayout officially provides rounded imagefilterview
[C language] the use of dynamic memory development "malloc"
RMS TO EAP通过MQTT简单实现
[论文阅读] CKAN: Collaborative Knowledge-aware Atentive Network for Recommender Systems
Matrix processing practice
ThreadLocal source code learning
Mysql80 service does not start
NCP1342芯片替代料PN8213 65W氮化镓充电器方案
Kotlin Compose 与原生 嵌套使用
Node red series (29): use slider and chart nodes to realize double broken line time series diagram
MySQL数字类型学习笔记
程序员如何活成自己喜欢的模样?
天龙八部TLBB系列 - 关于技能冷却和攻击范围数量的问题
TypeError: Cannot read properties of undefined (reading ‘cancelToken‘)
基于单片机步进电机控制器设计(正转反转指示灯挡位)
学习笔记4--高精度地图关键技术(下)
Cent7 Oracle database installation error
一个程序员的职业生涯到底该怎么规划?