Book Image

Clean Code in C#

By : Jason Alls
Book Image

Clean Code in C#

By: Jason Alls

Overview of this book

Traditionally associated with developing Windows desktop applications and games, C# is now used in a wide variety of domains, such as web and cloud apps, and has become increasingly popular for mobile development. Despite its extensive coding features, professionals experience problems related to efficiency, scalability, and maintainability because of bad code. Clean Code in C# will help you identify these problems and solve them using coding best practices. The book starts with a comparison of good and bad code, helping you understand the importance of coding standards, principles, and methodologies. You’ll then get to grips with code reviews and their role in improving your code while ensuring that you adhere to industry-recognized coding standards. This C# book covers unit testing, delves into test-driven development, and addresses cross-cutting concerns. You’ll explore good programming practices for objects, data structures, exception handling, and other aspects of writing C# computer programs. Once you’ve studied API design and discovered tools for improving code quality, you’ll look at examples of bad code and understand which coding practices you should avoid. By the end of this clean code book, you’ll have the developed skills you need in order to apply industry-approved coding practices to write clean, readable, extendable, and maintainable C# code.
Table of Contents (17 chapters)

To get the most out of this book

The majority of the chapters can be read independently of each other and in any order. But to get the most out of this book, I recommend that you read the chapters in the order presented. As you work through the chapters, follow the instructions, and carry out the tasks presented. Then, when you reach the end of a chapter, answer the questions and carry out the recommended further reading to reinforce what you have learned. For maximum benefit when working through the contents of this book, it is recommended that you meet the following requirements:

Software/hardware covered in the book

Requirements

Visual Studio 2019 Windows 10, macOS
Atom Windows 10, macOS, Linux: https://atom.io/
Azure resources Azure subscription: https://azure.microsoft.com/en-gb/
Azure Key Vault Azure subscription: https://azure.microsoft.com/en-gb/
The Morningstar API Obtain your own API key from https://rapidapi.com/integraatio/api/morningstar1
Postman Windows 10, macOS, Linux: https://www.postman.com/

It will be useful if you have these in place before you start reading and working your way through the chapters.

If you are using the digital version of this book, we advise you to type the code yourself or access the code via the GitHub repository (link available in the next section). Doing so will help you avoid any potential errors related tothe copying and pasting of code.

You should have basic experience of using Visual Studio 2019 Community Edition or higher, and basic C# programming skills, including writing console applications. Many examples will be in the form of C# console applications. The main project will be using ASP.NET. It will help if you are capable of writing ASP.NET websites using the framework and core. But don't worry – you will be guided through the steps that you need to go through.

Download the example code files

You can download the example code files for this book from your account atwww.packt.com. If you purchased this book elsewhere, you can visitwww.packtpub.com/supportand register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register atwww.packt.com.
  2. Select theSupporttab.
  3. Click onCode Downloads.
  4. Enter the name of the book in theSearchbox and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub athttps://github.com/PacktPublishing/Clean-Code-in-C-. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available athttps://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here:https://static.packt-cdn.com/downloads/9781838982973_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText:Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles.Here is an example:"The InMemoryRepository class implements theGetApiKey()method ofIRepository. This returns a dictionary of API keys. These keys will be stored in our_apiKeysdictionary member variable."

A block of code is set as follows:

using CH10_DividendCalendar.Security.Authentication;
using System.Threading.Tasks;

namespace CH10_DividendCalendar.Repository
{
public interface IRepository
{
Task<ApiKey> GetApiKey(string providedApiKey);
}
}

Any command-line input or output is written as follows:

          az group create --name "<YourResourceGroupName>" --location "East US"
        

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "To create the app service, right-click the project you created and select Publish from the menu."

Warnings or important notes appear like this.
Tips and tricks appear like this.