0%

Book Description

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.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Clean Code in C#
  3. Dedication
  4. About Packt
    1. Why subscribe?
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Reviews
  7. Coding Standards and Principles in C#
    1. Technical requirements
    2. Good code versus bad code
    3. Bad code
    4. Improper indentation
    5. Comments that state the obvious
    6. Comments that excuse bad code
    7. Commented-out lines of code
    8. Improper organization of namespaces
    9. Bad naming conventions
    10. Classes that do multiple jobs
    11. Methods that do many things
    12. Methods with more than 10 lines of code
    13. Methods with more than two parameters
    14. Using exceptions to control program flow
    15. Code that is difficult to read
    16. Code that is tightly coupled
    17. Low cohesion
    18. Objects left hanging around
    19. Use of the Finalize() method
    20. Over-engineering
    21. Learn to Keep It Simple, Stupid
    22. Lack of regions in large classes
    23. Lost-intention code
    24. Directly exposing information
    25. Good code
    26. Proper indentation
    27. Meaningful comments
    28. API documentation comments
    29. Proper organization using namespaces
    30. Good naming conventions
    31. Classes that only do one job
    32. Methods that do one thing
    33. Methods with less than 10 lines, and preferably no more than 4
    34. Methods with no more than two parameters
    35. Proper use of exceptions
    36. Code that is readable
    37. Code that is loosely coupled
    38. High cohesion
    39. Objects are cleanly disposed of
    40. Avoiding the Finalize() method
    41. The right level of abstraction
    42. Using regions in large classes
    43. The need for coding standards, principles, and methodologies
    44. Coding standards
    45. Coding principles
    46. Coding methodologies
    47. Coding conventions
    48. Modularity
    49. KISS
    50. YAGNI
    51. DRY
    52. SOLID
    53. Occam's Razor
    54. Summary
    55. Questions
    56. Further reading
  8. Code Review – Process and Importance
    1. The code review process
    2. Preparing code for review
    3. Leading a code review
    4. Issuing a pull request
    5. Responding to a pull request
    6. Effects of feedback on reviewees
    7. Knowing what to review
    8. Company's coding guidelines and business requirement(s)
    9. Naming conventions
    10. Formatting
    11. Testing
    12. Architectural guidelines and design patterns
    13. Performance and security
    14. Knowing when to send code for review
    15. Providing and responding to review feedback
    16. Providing feedback as a reviewer
    17. Responding to feedback as a reviewee
    18. Summary
    19. Questions
    20. Further reading
  9. Classes, Objects, and Data Structures
    1. Technical requirements
    2. Organizing classes 
    3. A class should have only one responsibility
    4. Commenting for documentation generation
    5. Cohesion and coupling
    6. An example of tight coupling
    7. An example of low coupling
    8. An example of low cohesion
    9. An example of high cohesion
    10. Design for change
    11. Interface-oriented programming
    12. Dependency injection and inversion of control
    13. An example of DI
    14. An example of IoC
    15. The Law of Demeter 
    16. A good and a bad example (chaining) of the Law of Demeter
    17. Immutable objects and data structures
    18. An example of an immutable type
    19. Objects should hide data and expose methods
    20. An example of encapsulation
    21. Data structures should expose data and have no methods
    22. An example of data structure
    23. Summary
    24. Questions
    25. Further reading
  10. Writing Clean Functions
    1. Understanding functional programming
    2. Keeping methods small
    3. Indenting code
    4. Avoiding duplication
    5. Avoiding multiple parameters
    6. Implementing SRP
    7. Summary
    8. Questions
    9. Further reading
  11. Exception Handling
    1. Checked and unchecked exceptions
    2. Avoiding NullPointerExceptions
    3. Business rule exceptions
    4. Example 1 – handling conditions with business rule exceptions
    5. Example 2 – handling conditions with normal program flow
    6. Exceptions should provide meaningful information
    7. Building your own custom exceptions
    8. Summary
    9. Questions
    10. Further reading
  12. Unit Testing
    1. Technical Requirements
    2. Understanding the reasons for a good test
    3. Understanding the testing tools
    4. MSTest
    5. NUnit
    6. Moq
    7. SpecFlow
    8. TDD methodology practice – fail, pass, and refactor
    9. Removing redundant tests, comments, and dead code
    10. Summary
    11. Questions
    12. Further reading
  13. End-to-End System Testing
    1. E2E testing
    2. The login module (subsystem)
    3. The admin module (subsystem)
    4. The test module (subsystem)
    5. Testing our three-module system using E2E
    6. Factories
    7. Dependency injection
    8. Modularization
    9. Summary
    10. Questions
    11. Further reading
  14. Threading and Concurrency
    1. Understanding the thread life cycle
    2. Adding thread parameters
    3. Using a thread pool
    4. Task Parallel Library
    5. Parallel.Invoke()
    6. Parallel.For()
    7. ThreadPool.QueueUserWorkItem()
    8. Using a mutex with synchronous threads
    9. Working with parallel threads using semaphores
    10. Limiting the number of processors and threads in the thread pool
    11. Preventing deadlocks
    12. Coding a deadlock example
    13. Preventing race conditions
    14. Understanding static constructors and methods 
    15. Adding static constructors to our sample code
    16. Adding static methods to our sample code
    17. Mutability, immutability, and thread safety
    18. Writing code that is mutable and not thread-safe
    19. Writing code that is immutable and thread-safe 
    20. Understanding thread safety
    21. Synchronized method dependencies
    22. Using the Interlocked class
    23. General recommendations
    24. Summary
    25. Questions
    26. Further reading
  15. Designing and Developing APIs
    1. Technical requirements
    2. What is an API?
    3. API proxies
    4. API design guidelines
    5. Well-defined software boundaries
    6. Understanding the importance of good quality API documentation 
    7. Swagger API development
    8. Passing immutable structs instead of mutable objects
    9. Testing third-party APIs
    10. Testing your own APIs
    11. API design using RAML
    12. Installing Atom and API Workbench by MuleSoft
    13. Creating the project
    14. Generating our C# API from our agnostic RAML design specification
    15. Summary
    16. Questions
    17. Further reading
  16. Securing APIs with API Keys and Azure Key Vault
    1. Technical requirements
    2. Undertaking the API project – dividend calendar
    3. Accessing the Morningstar API
    4. Storing the Morningstar API key in Azure Key Vault
    5. Creating the dividend calendar ASP.NET Core web application in Azure
    6. Publishing our web application
    7. Using an API key to secure our dividend calendar API
    8. Setting up the repository
    9. Setting up authentication and authorization
    10. Adding authentication
    11. Adding authorization
    12. Testing our API key security
    13. Adding the dividend calendar code
    14. Throttling our API 
    15. Summary
    16. Questions
    17. Further reading
  17. Addressing Cross-Cutting Concerns
    1. Technical requirements
    2. The decorator pattern
    3. The proxy pattern
    4. AOP with PostSharp
    5. Extending the aspect framework
    6. Developing our aspect
    7. Injecting behaviors before and after the method execution
    8. Extending the architectural framework
    9. Project – cross-cutting concerns reusable library
    10. Adding the caching concern
    11. Adding file logging capabilities
    12. Adding the logging concern
    13. Adding the exception-handling concern
    14. Adding the security concern
    15. Adding the validation concern
    16. Adding the transaction concern
    17. Adding the resource pool concern
    18. Adding the configuration settings concern
    19. Adding the instrumentation concern
    20. Summary
    21. Questions
    22. Further reading
  18. Using Tools to Improve Code Quality
    1. Technical requirements
    2. Defining good-quality code
    3. Performing code cleanup and calculating code metrics 
    4. Performing code analysis
    5. Using quick actions
    6. Using the JetBrains dotTrace profiler
    7. Using JetBrains ReSharper
    8. Using Telerik JustDecompile
    9. Summary
    10. Questions
    11. Further reading
  19. Refactoring C# Code – Identifying Code Smells
    1. Technical requirements
    2. Application-level code smells
    3. Boolean blindness
    4. Combinatorial explosion
    5. Contrived complexity
    6. Data clump
    7. Deodorant comments 
    8. Duplicate code
    9. Lost intent
    10. The mutation of variables
    11. The oddball solution
    12. Shotgun surgery
    13. Solution sprawl
    14. Uncontrolled side effects
    15. Class-level code smells
    16. Cyclomatic complexity
    17. Replacing switch statements with the factory pattern
    18. Improving the readability of conditional checks within an if statement
    19. Divergent change
    20. Downcasting
    21. Excessive literal use
    22. Feature envy
    23. Inappropriate intimacy
    24. Indecent exposure
    25. The large class (aka the God object)
    26. The lazy class (aka the freeloader and the lazy object)
    27. The middleman class
    28. The orphan class of variables and constants
    29. Primitive obsession
    30. Refused bequest
    31. Speculative generality
    32. Tell, Don't Ask
    33. Temporary fields
    34. Method-level smells
    35. The black sheep method
    36. Cyclomatic complexity
    37. Contrived complexity
    38. Dead code
    39. Excessive data return
    40. Feature envy
    41. Identifier size
    42. Inappropriate intimacy
    43. Long lines (aka God lines)
    44. Lazy methods
    45. Long methods (aka God methods)
    46. Long parameter lists (aka too many parameters)
    47. Message chains
    48. The middleman method
    49. Oddball solutions
    50. Speculative generality
    51. Summary
    52. Questions
    53. Further reading
  20. Refactoring C# Code – Implementing Design Patterns
    1. Technical requirements
    2. Implementing creational design patterns
    3. Implementing the singleton pattern
    4. Implementing the factory method pattern
    5. Implementing the abstract factory pattern
    6. Implementing the prototype pattern
    7. Implementing the builder pattern
    8. Implementing structural design patterns
    9. Implementing the bridge pattern
    10. Implementing the composite pattern
    11. Implementing the façade pattern
    12. Implementing the flyweight pattern
    13. Overview of behavioral design patterns
    14. Final thoughts
    15. Summary
    16. Questions
    17. Further reading
  21. Assessments
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 7
    8. Chapter 8
    9. Chapter 9
    10. Chapter 10
    11. Chapter 11
    12. Chapter 12
    13. Chapter 13
    14. Chapter 14
  22. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think
3.15.229.113