当前位置:网站首页>"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 :

  1. 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 .
  2. 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. .
  3. 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 :

  1. Use IOC Containers , Dependency injection .
  2. Use EventBus To optimize the code .
  3. Use AutoMapper To do entity mapping .
  4. Be able to use ORM Don't use it SQL.
  5. Use Polly For troubleshooting .
  6. Consider introducing or designing a configuration management platform when there are too many configuration items .
  7. Learning Domain Driven Design .
  8. ......

原网站

版权声明
本文为[Guo Mahua]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221433592126.html