此回购重点介绍了总体上的软件工程准则。-.NET开发

  • o2_433269
    了解作者
  • 7.8KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-18 02:26
    上传日期
此回购重点介绍了总体上的软件工程准则。 其中大多数是行业惯例,因此使用它们将确保不是您的人容易阅读您的代码。 C#编码准则和实践此回购重点介绍了总体上的软件工程准则。 其中大多数是行业惯例,因此使用它们将确保不是您的人容易阅读您的代码。 编码标准是任意的,这是事实。 但是,成功进行可维护项目的关键不是您遵循哪个标准,而是您要保持一致。 请记住,这篇文章与您应该如何缩进代码无关(tabs vs sp
DotNet-Coding-Guidelines-main.zip
  • DotNet-Coding-Guidelines-main
  • README.md
    19.3KB
  • LICENSE
    1KB
内容介绍
# C# Coding Guidelines & Practices This repo highlights the list of software engineering guidelines in general. Most of these are industry-wide conventions, thus using them will ensure that your code is easily readable by people who are not you. Just keep in mind that this post isn't about how you should indent your code, but it's more of a guidlines on how to write clean code that are easy to manage. With that said, if you are leading a team or even a single contributor developer who wanted to become better, having a set of coding guidelines is a great start to make that happen. Also, feel free to add your own tips through pull requests. ### Try to avoid boxing like in the following: ``` int x = 32; object o = x; ``` Boxing is the process of wrapping a value type such as a primitive or struct inside an object that lives on the heap. Unboxing is gettting the orginal value back out again. 👍 Map to matching primitive or struct variable instead: ``` int x = 32; int y = x; ``` ### Try to avoid the traditional if-else statements like in the following: ``` int statusCode; if (condition) { statusCode = 1; } else { statusCode = 2; } ``` 👍 Do use ternary conditional operator (?:) instead: ``` int statusCode = condition ? 1 : 2; ``` The preceding code is much cleaner, easier to read and understand. On top of that, it's more concise. ### Try to avoid nested Ternary operators: ``` public string GetJobStatus(Job job) { return job.IsRunning ? "Running" : job.HasErrors ? "Failed" : "Succeeded"; } ``` 👍 Use another line to express the nested operation as a separate statement for readability: ``` if (job.IsRunning) { return "Running"; } return job.HasErrors ? "Failed" : "Succeeded"; ``` Nesting ternary operators results in the kind of code that may seem clear as day when you write it, but six months later could leave maintainers (or future you) scratching their heads. ### Try to avoid using if statement for null checks like in the following: ``` if (something != null) { if (something.Other != null) { return something.Other.Whatever; } } ``` 👍 Do use null conditional (?.) operator instead: ``` return something?.Other?.Whatever; ``` The preceding code is also much cleaner and concise. ### Try to avoid complex if-else statements for null checks like in the following: ``` if (something != null) { if (something.other != null) { return something.other.whatever; } else { return string.empty; } } else { return string.empty; } ``` 👍 Do use null coalescing (??) operator instead: ``` return something?.other?.whatever ?? string.empty; ``` ### Try to avoid using the following code when returning a default value when an object is null: ``` int? number = null; var n = number.HasValue ? number : 0; ``` 👍 Do use null coalescing (??) operator as well: ``` var n = number ?? 0; ``` 👍 or alternatively, you could do: ``` var n = number.GetValueOrDefault(); ``` ### Try to avoid using the equality operator (==) or HasValue for nullable variable check like in the following: ``` int? number = null; if (number == null) { //do something } if (!number.HasValue) { //do something } ``` 👍 While the preceding code is fine, we can still improve that by using the is keyword like in the following: ``` int? number = null; if (number is null) { //do something } ``` ### Avoid code without braces ({}) for single conditional if statement, for and foreach loops like in the following: ``` if (condition) action; ``` Without the braces, it is too easy to accidentally add a second line thinking it is included in the if, when it isn’t. 👍 Always use braces instead: ``` if (condition) { action; } //or better if (condition) { action; } ``` ### Try to avoid using multiple if-else statements like in the following: ``` if (condition) { //do something } else if (condition) { //do something } else if (condition) { //do something } else (condition) { //do something else } ``` 👍 Do use switch statements instead: ``` switch (condition) { case 1: //do something break; case 2: //do something break; case 3: //do something break; default: //do something else break; } ``` 👍 But prefer switch expressions over switch statements where possible like in the following: ``` condition switch { 1 => //do something; 2 => //do something; 3 => //do something; _ => //do something else; } ``` The preceding code is more concise yet, still easy to read and understand. (Note, only available in C# 8 or later versions) 💡 Exceptions - There are cases that if-else statements would make more sense than using switch statements. One such example could be, if the condition involves different objects and complex conditions, though if the conditional logic here boils down to checking whether an object matches a certain shape in terms of property values, you might want to explore recursive pattern matching. ### Do use the using statement when working with objects that eat resources or implements IDisposable interface: ``` using (MemoryStream stream = new MemoryStream()) { // do something } ``` 👍 Or prefer to use the new using declaration introduced in C# 8 like in the following: ``` using var stream = new MemoryStream(); // do something ``` The preceding code reduces the number of curly braces in your method, but it can still be seen easily where a resource is disposed. For more information, see: "pattern-based using" and "using declarations". ⚠ Do not use either of the above if the IDisposable is the return value of your method: ``` Stream GetABrokenFileStream() { // this is wrong! The stream will be disposed when you exit the method using var fileStream = File.OpenRead("path to my file"); return fileStream; } ``` ### Avoid concatenating strings with the + sign/symbol like in the following: ``` string space = "Vianne"; string greetings = "Hello " + name + "!"; ``` 👍 Use string.Format() method instead: ``` string name = "Vynn"; string greetings = string.Format("Hello {0}!", name); ``` 👍 Or prefer using string interpolation ($) instead where possible: ``` string name = "Vjor"; string greeting = $"Hello, {name}!; ``` The preceding code is much more concise and readable compared to other approaches. ### Try to avoid string.Format() when formatting simple objects like in the following: ``` var date = DateTime.Now; string greetings = string.Format("Today is {0}, the time is {1:HH:mm} now.", date.DayOfWeek, date); ``` 👍 Use string interpolation instead: ``` var date = DateTime.Now; string greetings = $"Today is {date.DayOfWeek}, the time is {date:HH:mm} now."); ``` The preceding code is much easier to understand and concise. However, there are certain cases that using the string.Format() would makes more sense. For example, when dealing with complex formatting and data manipulation. So, use your judgement when to apply them in situations. ### Avoid using specific type for complex objects when defining variables like in the following: ``` List<Repository.DataAccessLayer.Whatever> listOfBlah = _repo.DataAccessLayer.GetWhatever(); ``` 👍 Use the var keyword instead: ``` var listOfBlah = _repo.DataAccessLayer.GetWhatever(); ``` 👍 Same goes for other local variables: ``` var students = new List<Students>(); var memoryStream = new MemoryStream(); var dateUntilProgramExpiry = DateTime.Now; ``` ### Try to avoid one-liner method implementation with curly braces like in the following: ``` public string Greeter(string name) { return $"Hello {name}!"; } ``` 👍 Do use Expression-bodied (=>) implementation instead: ``` public string Greeter(string name) => $"Hello {name}!"; ``` The preceding code is more concise while maintaining readability. ### Avoid object initialization like in the following: ``` Person person = new Person(); perso
评论
    相关推荐