Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Clean Code in C# Part 9 Rules for Results
Caio Cesar
Caio Cesar

Posted on

     

Clean Code in C# Part 9 Rules for Results

Introduction

Following a couple of simple rules when writing software could be a decision that helps teams write code that applies principles such as the Single Responsibility and Dependency Inversion Principle.

According to Kent Beck a project isSimple if it follows these rules:

  1. Create all tests
  2. No code duplicity
  3. Express the purpose
  4. Minimize the number of methods and classes

These rules are in order of reverence and will be reviewed in order.

Rule 1: Create all tests

A software must execute as expected, validation of expected results in software is crucial. Software that can be tested and that passes all test cases can be validated, software that cannot be tested and cannot be validated needs to be refactored to comply with rule 1.

When creating unit tests it is wise to apply the Single Responsibility Principle (SRP), to simplify testing. Therefore, clean tests implies in cleaner code, that results in better systems.

Avoiding abstractions in most cases makes it difficult to create tests. When code is tested it should be refactored to make testing simple and efficient. Having well tested software makes for better projects with loose coupling.

Rule 2: No code duplicity

Code repetition represents unnecessary code that implies in more complexity. In the nextexample there are code duplication when values are swapped.

Example 1:

publicstaticvoidSelectionSort(int[]input){for(vari=0;i<input.Length;i++){varmin=i;for(varj=i+1;j<input.Length;j++){if(input[min]>input[j]){min=j;}}if(min!=i){varlowerValue=input[min];input[min]=input[i];input[i]=lowerValue;}}}
Enter fullscreen modeExit fullscreen mode
publicstaticvoidBubbleSort(int[]input){varitemMoved=false;do{itemMoved=false;for(inti=0;i<input.Count()-1;i++){if(input[i]>input[i+1]){varlowerValue=input[i+1];input[i+1]=input[i];input[i]=lowerValue;itemMoved=true;}}}while(itemMoved);}
Enter fullscreen modeExit fullscreen mode

The refactored code eliminates code duplicity and now change to this code is structure to one Swap method:

Example 2:

publicstaticvoidSelectionSort(int[]input){for(vari=0;i<input.Length;i++){varmin=i;for(varj=i+1;j<input.Length;j++){if(input[min]>input[j]){min=j;}}if(min!=i){Swap(input,min,i);}}}
Enter fullscreen modeExit fullscreen mode
publicstaticvoidBubbleSort(int[]input){varitemMoved=false;do{itemMoved=false;for(inti=0;i<input.Count()-1;i++){if(input[i]>input[i+1]){Swap(input,i+1,i);itemMoved=true;}}}while(itemMoved);}
Enter fullscreen modeExit fullscreen mode
publicvoidSwap(int[]input,intfirstPosition,intsecondPosition){varswapValue=input[firstPosition];input[firstPosition]=input[secondPosition];input[secondPosition]=swapValue;}
Enter fullscreen modeExit fullscreen mode

Rule 3: Expressiveness

Writing code that compiles is trivial, but developing software that other humans comprehends can be challenging. High costs in software development are usually a result of software maintenance. To minimize time it takes for others to understand code, a software project must express its purpose clearly.

There are a couple of ways to better express purpose through code as displayed in the following list:

  • Good naming conventions for method and classes.
  • Small classes and methods that follows SRP.
  • Unit tests that express what is being tested and expected result.
  • Design patterns naming that one can easily understand its purpose like UnitOfWork instead of UOW.

According to uncle bob the most important rule to follow when writing expressive code is to try. Sometimes software developers want to deliver code fast, but spending a little more time thinking about simple naming convention can make for cleaner code.

Rule 4: Classes and methods

When following the other 3 rules for testing, duplicity and expressiveness, one might think that a new class is necessary for everything.

Creating classes and methods should be something moderated and well planned. The goal of this rule is to create a small overall system while having minimal number of classes and methods. Too many classes and methods can lead to unnecessary complexity depending on the nature of the application.

References

  1. Clean Code: A Handbook of Agile Software Craftsmanship by Robert
  2. Extreme Programming Explained: Embrace Change by Kent Beck
  3. https://dotnetcoretutorials.com/2020/05/10/basic-sorting-algorithms-in-c/

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Architect | Researcher
  • Education
    Computer Science
  • Work
    Software Architect
  • Joined

More fromCaio Cesar

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp