Best Practices for C# Developers

 

The best practice is a method or technique that has been generally accepted as superior to any alternatives because it produces results that are superior to those achieved by other means or because it has become a standard way of doing things, e.g., a standard way of complying with legal or ethical requirements.

 

Best Practices Help:

  • Understand the code quickly.
  • Facilitate copying, changing, and maintaining the code.

1. Naming and handling classes

The benefits of proper class naming and naming conventions:

  • You know what to expect from a certain class without looking at code or documentation, even if you aren’t the person who created it or if it was written a long time ago.
  • It’s easy to search and navigate a codebase.
  • It’s easier to talk to your team when discussing problems/improvements.
  • It makes onboarding newcomers easier, quicker, and less confusing.

Names Rules

  1. Choose descriptive and unambiguous names.
  2. Make a meaningful distinction.
  3. Use pronounceable names.
  4. Use searchable names.
  5. Replace magic numbers with named constants.
  6. Avoid encodings. Don’t append prefixes or type information.

2. Naming and handling methods, fields, and properties

Method Overloading:

Overloading is used (recommended) when multiple methods have the same purpose but there is more than one way to start it.

Methods rules:

  1. Small.
  2. Do one thing.
  3. Use descriptive names.
  4. Prefer fewer arguments.
  5. Have no side effects.
  6. Don’t use flag arguments. Split method into several independent methods that can be called from the client without the flag.

Fluent interface & Method Chaining:

Method chaining is the practice of object methods returning the object itself in order for the result to be called for another method. Like this:

Examples:

Useful when you want to hide the array creation and provide a variable argument syntax to your methods
e.g. setting multiple properties or calling utility-type methods.

Using Properties Correctly:

You can think of a property as a member that provides a flexible mechanism to readwrite, or compute the value of a private field. Think of them as accessors (getters & setters) providing access to data with safety and flexibility.

The code we put into getters & setters depends on:

  1. What the property is?
  2. How it will be used?
  3. The protection it requires.

Examples include code to:

  1. check credentials- i.e. admin roles.
  2. Check application state- i.e. when an object is first created.
  3. Format strings.
  4. Log.
  5. Lazy loading.
  6. Validate incoming value (setter).
Lazyloading checks if an actor is Null — Validate an Actor name before setting its value

3. Running a unit test

Unit testing is important because it is one of the earliest testing efforts performed on the code and the earlier defects are detected, the easier they are to fix. Early bug-detection is also the most cost-effective for a project, with code fixes becoming more expensive the later they’re found in the lifecycle.

Code smells

  1. Rigidity. The software is difficult to change. A small change causes a cascade of subsequent changes.
  2. Fragility. The software breaks in many places due to a single change.
  3. Immobility. You cannot reuse parts of the code in other projects because of involved risks and high effort.
  4. Needless Complexity.
  5. Needless Repetition.
  6. Opacity. The code is hard to understand.

Add comment