Book Image

Clean Code with C# - Second Edition

By : Jason Alls
4.5 (2)
Book Image

Clean Code with C# - Second Edition

4.5 (2)
By: Jason Alls

Overview of this book

Traditionally associated with Windows desktop applications and game development, C# has expanded into web, cloud, and mobile development. However, despite its extensive coding features, professionals often encounter issues with efficiency, scalability, and maintainability due to poor code. Clean Code in C# guides you in identifying and resolving these problems using coding best practices. This book starts by comparing good and bad code to emphasize the importance of coding standards, principles, and methodologies. It then covers code reviews, unit testing, and test-driven development, and addresses cross-cutting concerns. As you advance through the chapters, you’ll discover programming best practices for objects, data structures, exception handling, and other aspects of writing C# computer programs. You’ll also explore API design and code quality enhancement tools, while studying examples of poor coding practices to understand what to avoid. By the end of this clean code book, you’ll have the developed the skills needed to apply industry-approved coding practices to write clean, readable, extendable, and maintainable C# code.
Table of Contents (18 chapters)

The SOLID software methodology

SOLID is an acronym that represents a set of five design principles for writing maintainable and scalable software. These principles were introduced by Robert C. Martin and are widely used in OOP. Let’s take a brief look at each of these SOLID principles.

SRP

A class should have only one reason to change, meaning that it should have only one responsibility.

The following code is an example of a wrong implementation that violates the SRP:

class Report{
    public void GenerateReport() { /* ... */ }
    public void SaveToFile() { /* ... */ }
}

This class performs the two responsibilities of generating a report and saving it to a file. Here is the correct implementation:

class Report{
    public void GenerateReport() { /* ... */ }
}
class ReportSaver
{
    public void SaveToFile(Report report) { /* ... */ }
}

As you can see, our correct implementation...