Clean Code for .NET Coding Craftsman

Written by thangchung | Published 2018/12/18
Tech Story Tags: programming | clean-code | csharp | dotnet | software-craftmanship

TLDRvia the TL;DR App

When I started my very first career in software development many years ago, I remembered that I really like to learn and achieve a lot of skills in software development via read notably books by gurus in this fields as well as read blogs of them. Some of the very famous books that I have read are Code Complete: A Practical Handbook of Software Construction, The Clean Coder: A Code of Conduct for Professional Programmers, Design Patterns: Elements of Reusable Object-Oriented Software, Refactoring: Improving the Design of Existing Code, and so on. And I strongly believed that all developers just like me also stepped into the software development industry in this way.

By the time, I wondered how to share what I have learned to the software development communities out there so that it will help them save their efforts for the first stage of their career. Just the last year, I saw there are couples of projects about Clean Code in JavaScript and PHP languages, and now is Ruby language. Then I thought that it would be great if I started the list just like them but customized sections for .NET/.NET Core, and that pushed me to start immediately in that night to create the very first Clean Code for .NET developers list at https://github.com/thangchung/clean-code-dotnet

To make it easy to readers, .NET developers in specially, I start with the different way by other lists (JavaScript, PHP, or Ruby language). I grouped them to the big group with collapsible sub-items which help readers couldn’t be scary when they walk through the big list with huge items accordingly. In case they want to investigate or read in details, they can click on the title of the sub-item to read more. Tony — my colleague — is also eager to join this project so that I told him to help me and he’s a person who proposed this IDEAS as well, and actually it makes the list became short and succinct. Talking a lot of the history of this list, let now go into details to see what inside those group.

Naming

  • Avoiding using a bad name
  • Avoid disinformation name
  • Use pronounceable names
  • Use camel-case notation
  • Use domain name.

Variables

  • Use meaningful and pronounceable variable names
  • Use the same vocabulary for the same type of variable
  • Use searchable names
  • Use explanatory variables
  • Avoid nesting too deeply and return early
  • Avoid Mental Mapping
  • Don’t add unneeded context
  • Use default arguments instead of short circuiting or conditionals
  • Avoid magic string

Functions

  • Function arguments (2 or fewer ideally)
  • Functions should do one thing
  • Function names should say what they do
  • Functions should only be one level of abstraction
  • Don’t use flags as function parameters
  • Avoid Side Effects
  • Don’t write to global functions
  • Don’t use a Singleton pattern
  • Encapsulate conditionals
  • Avoid negative conditionals
  • Avoid conditionals
  • Avoid type-checking
  • Remove dead code

Objects and Data Structures

  • Use getters and setters
  • Make objects have private/protected members

Classes

  • Use method chaining
  • Prefer composition over inheritance

SOLID

  • Single Responsibility Principle (SRP)
  • Open/Closed Principle (OCP)
  • Liskov Substitution Principle (LSP)
  • Interface Segregation Principle (ISP)
  • Dependency Inversion Principle (DIP)
  • Don’t repeat yourself (DRY)

Testing

  • Single concept per test

Concurrency

  • Use Async Await

Error Handling

  • Don’t ignore caught errors
  • Use consistent capitalization
  • Function callers and callees should be close

Formatting

  • Uses .editorconfig file

Comments

  • Only comment things that have business logic complexity
  • Don’t leave commented out code in your codebase
  • Don’t have journal comments
  • Avoid positional markers

Recap

We’re still working on this list, and I believe that it will be longer and accuracy. Our team looks forward to having all of you to involve, contribute and help to make this list more useful for .NET/.NET Core Community.

Again, the clean code list for .NET/.NET Core can be found at https://github.com/thangchung/clean-code-dotnet

Thanks for reading! If you enjoyed this article, be sure to click 👏 symbol below so others will see it.


Written by thangchung | Software Developer and Architect
Published by HackerNoon on 2018/12/18