▶Book Description
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.
▶What You Will Learn
- Write code that allows software to be modified and adapted over time
- Implement the fail-pass-refactor methodology using a sample C# console application
- Address cross-cutting concerns with the help of software design patterns
- Write custom C# exceptions that provide meaningful information
- Identify poor quality C# code that needs to be refactored
- Secure APIs with API keys and protect data using Azure Key Vault
- Improve your code’s performance by using tools for profiling and refactoring
▶Key Features
- Write code that cleanly integrates with other systems while maintaining well-defined software boundaries
- Understand how coding principles and standards enhance software quality
- Learn how to avoid common errors while implementing concurrency or threading
▶Who This Book Is For
This coding book is for C# developers, team leads, senior software engineers, and software architects who want to improve the efficiency of their legacy systems. A strong understanding of C# programming is required.
▶What this book covers
- Chapter 1, Coding Standards and Principles in C#, looks at some good code contrasted with bad code. As you read through this chapter, you will come to understand why you need coding standards, principles, methodologies, and code conventions. You will learn about modularity and the design guidelines KISS, YAGNI, DRY, SOLID, and Occam's razor.
- Chapter 2, Code Review – Process and Importance, takes you through the code review process and provides reasons for its importance. In this chapter, you are guided through the process of preparing code for review, leading a code review, knowing what to review, knowing when to send code for review, and how to provide and respond to review feedback.
- Chapter 3, Classes, Objects, and Data Structures, covers the broad topics of class organization, documentation comments, cohesion, coupling, the Law of Demeter, and immutable objects and data structures. By the end of the chapter, you will be able to write code that is well organized and only has a single responsibility, provide users of the code with relevant documentation, and make code extensible.
- Chapter 4, Writing Clean Functions, helps you to understand functional programming, how to keep methods small, and how to avoid code duplication and multiple parameters. By the time you finish this chapter, you will be able to describe functional programming, write functional code, avoid writing code with more than two parameters, write immutable data objects and structures, keep your methods small, and write code that adheres to the Single Responsibility Principle.
- Chapter 5, Exception Handling, covers checked and unchecked exceptions, NullPointerException, and how to avoid them as well as covering, business rule exceptions, providing meaningful data, and building your own custom exceptions.
- Chapter 6, Unit Testing, takes you through using the Behavior-Driven Development (BDD) software methodology using SpecFlow, and Test-Driven Development (TDD) using MSTest and NUnit. You will learn how to write mock (fake) objects using Moq, and how to use the TDD software methodology to write tests that fail, make the tests pass, and then refactor the code once it passes.
- Chapter 7, End-to-End System Testing, guides you through the manual process of end-toend testing using an example project. In this chapter, you will perform End-to-End (E2E) testing, code and test factories, code and test dependency injection, and test modularization. You will also learn how to utilize modularization.
- Chapter 8, Threading and Concurrency, focuses on understanding the thread life cycle; adding parameters to threads; using ThreadPool, mutexes, and synchronous threads; working with parallel threads using semaphores; limiting the number of threads and processors used by ThreadPool; preventing deadlocks and race conditions; static methods and constructors; mutability and immutability; and thread-safety.
- Chapter 9, Designing and Developing APIs, helps you to understand what an API is, API proxies, API design guidelines, API design using RAML, and Swagger API development. In this chapter, you will design a language-agnostic API in RAML and develop it in C#, and you will document your API using Swagger.
- Chapter 10, Securing APIs with API Keys and Azure Key Vault, shows you how to obtain a third-party API key, store that key in Azure Key Vault, and retrieve it via an API that you will build and deploy to Azure. You will then implement API key authentication and authorization to secure your own API.
- Chapter 11, Addressing Cross-Cutting Concerns, introduces you to using PostSharp to address cross-cutting concerns using aspects and attributes that form the basis of aspectoriented development. You will also learn how to use proxies and decorators.
- Chapter 12, Using Tools to Improve Code Quality, exposes you to various tools that will assist you in writing quality code and improving the quality of existing code. You'll gain exposure to code metrics and code analysis, quick actions, the JetBrains tools called dotTrace Profiler and Resharper, and Telerik JustDecompile.
- Chapter 13, Refactoring C# Code – Identifying Code Smells, is the first of two chapters that take you through different types of problematic code and show you how to modify it to be clean code that is easy to read, maintain, and extend. Code problems are listed alphabetically through each chapter. Here, you will cover such topics as class dependencies, code that can't be modified, collections, and combinatorial explosion.
- Chapter 14, Refactoring C# Code – Implementing Design Patterns, takes you through the implementation of creational and structural design patterns. Here, behavioral design patterns are briefly touched upon. You are then given some final thoughts on clean code and refactoring.