DEV Community

Cover image for Clean Code in C# Part 1 Meaningful Names
Caio Cesar
Caio Cesar

Posted on • Updated on

Clean Code in C# Part 1 Meaningful Names

Introduction

This post contains suggestions for a cleaner code approach in the C# programing language, most of the knowledge presented here is in the book with practical emphasis on the C# programming Language.

Writing clean code is important so that other humans can easily interpret your code. This guide has several practical examples of how to write cleaner code in C# based on the book Clean code by Robert C. Martin.

1. Meaningful Names

Naming occurs more often than we realize, variables, methods, namespaces, projects, solutions, tables, folder etc. As authors of code developers need to be aware that creating meaningful names often makes code more maintainable.

Names with purpose

Creating names to variables, methods and classes that reveal purpose can facilitate the understanding of the program. The code below is a Person factory method that returns a new person object with the given name and age set.

public P PF(int a, string n)
{
  return new P()
  {
    a = a,
    n = n
  };
}
Enter fullscreen mode Exit fullscreen mode

The refactored code below demonstrates how meaningful names can change how we look at code.

public Person PersonFactory(int age, string name)
{
  return new Person()
  {
    Age = age,
    Name = name
  };
}
Enter fullscreen mode Exit fullscreen mode

Avoid misleading names

A collection in C# can be used to work with groups of objects.

Collection<string> books= new Collection<string>();
Enter fullscreen mode Exit fullscreen mode

However using the collection as part of the name in a method and class when not actually using a collection could be misleading.

public BookCollection GetBookCollection()
{
  return new BookCollection()
  {
      Title = "C# Programming 101"
  };
}
Enter fullscreen mode Exit fullscreen mode

Avoid abbreviations

Due to the context of the business, those who have more time programing in a business environment might be used to abbreviations, however these practices can be confusing to people who are new to the organization.

public DateTime GetUDob()
{
  DateTime uDob = new DateTime(2000, 12, 12);
  return uDob;
}
Enter fullscreen mode Exit fullscreen mode
public DateTime GetUserDateOfBirth()
{
  DateTime userDateOfBirth = new DateTime(2000, 12, 12);
  return userDateOfBirth;
}
Enter fullscreen mode Exit fullscreen mode

Class and Method Naming

A class name should be a noun. Examples of class names are Client, BankAccount, and School. Avoid verbs and names like Data, Collection, Think, Sing, and List.

A method name should be a verb. Examples of methods are Insert, Commit, Calculate, and GetFullName.

When working in a solution avoid multiple prefixes in method names to represent the same meaning such as get, fetch and obtain.

Making changes to a method

In the example code below we have two segments of code that delivers the same result. The second section has more significant names.

public void PrintBookSize(List<Book> books)
{
 foreach (Book b in books)
 {
    int count = b.Pages;

    if (count < 100)
        type = "small";
    else if (count > 100 && count < 200)
        type = "medium";
    else
        type = "large";

    Console.WriteLine($"The book {b.Name} is {type}");
 }           
}
Enter fullscreen mode Exit fullscreen mode

In the example below variables and methods were used to identify the book type. The number of lines of the program is larger, however more significant names were used. These changes in large codebases can have huge effects in changes.

public class BookStatistics
{
    const string SMALL_BOOK_TYPE = "small";
    const string MEDIUM_BOOK_TYPE = "medium";
    const string LARGE_BOOK_TYPE = "large";

    public void PrintSize(List<Book> books)
    {
        foreach (Book book in books)
        {
            string bookType = GetBookType(book.Pages);

            Console.WriteLine($"The book {book.Name} is {bookType}");
        }
    }

    public string GetBookType(int pages)
    {
        if (pages < 100)
            return SMALL_BOOK_TYPE;
        else if (pages > 100 && pages < 200)
            return MEDIUM_BOOK_TYPE;
        else
            return LARGE_BOOK_TYPE;
    }
}

Enter fullscreen mode Exit fullscreen mode

References

  1. Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin.

Top comments (0)