当前位置:网站首页>"My" C code specification
"My" C code specification
2022-06-21 13:42:00 【Guo Mahua】
Code specification is one of the most overlooked skills . For leaders and developers , Implement business requirements without Bug It's the ultimate goal . If there is no sound Code review Mechanism , Projects can easily degenerate into “Shit mountain”.
I believe the first language that most of us learn about programming is C Language .C Language is an excellent process oriented language , We can learn programming by using basic data structure and program flow . So some students will develop the habit of imperative programming , For example, we usually see such programs :
If(…)
//do some thing
If(…)
//do some thing
Else
//do some thing
//do some thing
//do some thing
as time goes on , More and more people handle this code , This method is getting longer and longer .
It's easy to write clean code , But there is no clear standard , If you happen to take over a project with very standard code , I will learn quickly . I can only pass some principled , Theoretical requirements , And what you see and feel at work , Give some advice .
Functional programming principle requirements :
- Avoid parameter mutation , That is, whether the input parameter of the current method is a value type or a reference type , Should be treated as immutable , The target result should be used as the return value of the method . Common mistakes are void FillOrderInfo(Order order) etc. . You should use order.BuildInfo(…); or Order GetOrderDetail(…); Methods such as .
- Write appropriate higher-order functions . I.e. accepting the entrustment ( Method ) As the reference , Or by commission ( Method ) As a function of return value . Such as Linq Medium Where,Select etc. .
- Write testable code , Pure functions . A pure function is a function whose result is determined only by the input parameters , If you use... In your function DataTime.Now, Because it gets the external operating system time , Therefore, the current function depends on the external environment , Not a pure function . The current time should be obtained externally , Pass in as a parameter , Similar behaviors include database queries , Network call, etc .
Functional programming advocates one line of functions ,C# Its new syntax is also developing towards functional programming , such as C#9.0 Medium Init, Top level statement , Record type and previous pattern matching , All of these programming concepts are shown .
Business code :
- Do not use any method more than 50 That's ok , Within ten lines is the best , Get into the habit of layering and splitting abstractions , Special algorithms, etc .
- Delete all unnecessary code , Even if you want to back up , And don't leave comments in the project .
- Use... Correctly public and private.
- One .cs Only one interface should be defined in the file , Or type . The two types of association should be logically associated in the form of subfolders and naming .
Such as :Event/OrderUpdateEvent.cs; Handler/ OrderUpdateHandler.cs.- Method signature means that method design should minimize the range of parameters .
Error model :
DbContext AddDbContext<T>(Configure conf)
{
connString = conf[“ConnString”];
// do some thing;
}
Write it correctly :
DbContext AddDbContext<T>(string connString)
{
// do some thing;
}- The definition and call of network interface shall comply with the basic rules RESTful standard ( Don't put the Post The request body is disassembled into key value pairs and attached to Url Medium !).
- Asynchronous methods should return Task,Task<T>, And preferably Async End to name , Such as Task<T> QueryOrderAsync(string id).
- Reduce the branch complexity as much as possible , About to have more if-else Change it to switch, Or use the policy mode .
- Recommended if Interrupt mode of , namely if(…) return; instead of if(…) { //do some thing; }
- Write concise attributes , about TotalPrice,IsValid And other values that need calculation or logical judgment , You can use read-only attributes flexibly {get=>…;}
- Be careful I/O Whether the request is written in the loop .
- You can use the extension method , Try to avoid using static classes , Field etc. .
- Any variable , function , Names such as types should be careful and concise . Non consensus situations , Do not use abbreviations , abbreviation , Meaningless letters , Chinese characters ( Except for unit testing ) etc. .
To have a comprehensive understanding of the current development technology , And use it flexibly :
- Use IOC Containers , Dependency injection .
- Use EventBus To optimize the code .
- Use AutoMapper To do entity mapping .
- Be able to use ORM Don't use it SQL.
- Use Polly For troubleshooting .
- Consider introducing or designing a configuration management platform when there are too many configuration items .
- Learning Domain Driven Design .
- ......
边栏推荐
- 3. function improvement
- CVPR2022 | 上科大x小红书首次提出动作序列验证任务,可应用于体育赛事打分等多场景
- Automatic operation and maintenance 2 - common modules in ansible
- How to write test cases
- SCCM creates a client collection based on the installed app and periodically pushes application updates
- Using slurm cluster computing node debugger in vscode
- Kotlin - i/o flow
- scrapy_ Redis distributed crawler
- Installation and application of MySQL 8.0 under Linux
- MySQL - table constraints
猜你喜欢

Work content of service governance

seaborn绘图风格的设置

Open source FTP server FileZilla server

SCCM基于已安装的 APP创建客户端集合并定期推送应用更新
![[deeply understand tcapulusdb technology] tmonitor background one click installation](/img/0a/742503e96a9b51735f5fd3f598b9af.png)
[deeply understand tcapulusdb technology] tmonitor background one click installation

The xdd-plus login prompt on the Qinglong panel is "the current login environment is abnormal. To ensure the security of your account, you cannot log in temporarily. It is recommended to connect the t

Navigation bar switching, message board, text box losing focus

Babbitt yuancosmos daily must read: wechat may ban a official account for the first time on the grounds of "involving secondary transactions in digital collections", and the new regulations of the pla

SCCM creates a client collection based on the installed app and periodically pushes application updates
Collection reference type in JS
随机推荐
Questions and answers No. 43: application performance probe monitoring principle PHP probe
MySQL - adding, deleting, querying and modifying tables
【深入理解TcaplusDB技术】Tmonitor后台一键安装
Kubernetes快速实战与核心原理剖析
Heat mapping using Seaborn
Detailed explanation and examples of common parameters of curl
Repair for a while, decisively reconstruct and take responsibility -- talk about CRM distributed cache optimization
Master the basic usage of SQLite3
Voltage detection and current detection based on stm32
Redis学习(3)—— 持久化机制
3. operator
Generalized Focal Loss: Learning Qualified and Distributed Bounding Boxes for Dense Object Detection
Don't know how to interview? Test interview questions summary, teach you to face the interview questions
Modification method of EKF extended Kalman filter for omnidirectional ground
TOOD: Task-aligned One-stage Object Detection
CVPR2022 | 上科大x小红书首次提出动作序列验证任务,可应用于体育赛事打分等多场景
Are you still using generator to generate crud code of XXX management system? Let's see what I wrote
Deep understanding of convolution in convolution neural network
MySQL - view properties
The new plan for national treasures - the exclusive digital collection of the four museums is coming!