Clean Code in C#
Refactor your legacy C# code base and improve application performance by applying best practices
Jason Alls
- 500 pages
- English
- ePUB (adapté aux mobiles)
- Disponible sur iOS et Android
Clean Code in C#
Refactor your legacy C# code base and improve application performance by applying best practices
Jason Alls
Ă propos de ce livre
Develop your programming skills by exploring essential topics such as code reviews, implementing TDD and BDD, and designing APIs to overcome code inefficiency, redundancy, and other problems arising from bad code
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
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
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.
Foire aux questions
Informations
- Understanding the thread life cycle
- Adding thread parameters
- Using a thread pool
- Using a mutual exclusion object with synchronous threads
- Working with parallel threads using semaphores
- Limiting the number of processors and threads in the thread pool
- Preventing deadlocks
- Preventing race conditions
- Understanding static constructors and methods
- Mutability, immutability, and thread safety
- Synchronized method dependencies
- Using the Interlocked class for simple state changes
- General recommendations
- The ability to understand and discuss the thread life cycle
- An understanding of and ability to use foreground and background threads
- The ability to throttle threads and set the number of processors to use concurrently using a thread pool
- The ability to understand the effects of static constructors and methods in relation to multi-threading and concurrency
- The ability to take into account mutability and immutability and their impact on thread safety
- The ability to understand what causes race conditions and how to avoid them
- The ability to understand what causes deadlocks and how to avoid them
- The ability to perform simple state changes using the Interlocked class
Understanding the thread life cycle
- Foreground threads: By default, threads run in the foreground. A process will continue to run while at least one foreground thread is currently running. Even if Main() completes but a foreground thread is running, the application process will remain active until the foreground thread terminates. Creating a foreground thread is really simple, as the following code shows:
var foregroundThread = new Thread(SomeMethodName);
foregroundThread.Start();
- Background threads: You create a background thread in the same way that you create foreground threads, except that you also have to explicitly set a thread to run in the background, as shown:
var backgroundThread = new Thread(SomeMethodName);
backgroundThread.IsBackground = true;
backgroundThread.Start();
Adding thread parameters
private static int Add(int a, int b)
{
return a + b;
}
private static void ThreadParametersExample()
{
int result = 0;
Thread thread = new Thread(() => { result = Add(1, 2); });
thread.Start();
thread.Join();
Message($"The addition of 1 plus 2 is {result}.");
}
internal static void Message(string message)
{
Console.WriteLine(message);
}
static void Main(string[] args)
{
ThreadParametersExample();
Message("=== Press any Key to exit ===");
Console.ReadKey();
}
Using a thread pool
- Using the Task Parallel Library (TPL) (on .NET Framework 4.0 and higher)
- Using ThreadPool.QueueUserWorkItem()
- Using asynchronous delegates
- Using BackgroundWorker