.NET Code Reviews - Classes

.NET Code Reviews - Classes

Few days ago, i started a series for code reviewing .NET applications. Follow this link to read the first article about C# code reviews. Today, we will proceed by writing down some of the patterns i see often with .NET Classes.

Singleton Pattern

public class Helper
{
    private Helper () {}
    static Helper helper = null;
    public static Helper Instance
    {
        get
        {
            if (helper == null)
            {
                helper = new Helper();
            }
            return helper;
        }
    }
    public void Output(string s)
    {
        Console.WriteLine(s);
    }
}

The same can be achieved in a more elegant way as follows.

public static class Helper
{
    static Helper()
    {
    }
    public static void Output(string s)
    {
        Console.WriteLine(s);
    }
}

// Client Code
Helper.Output("Hello World") is better than Helper.Instance.Output("Hello World");

Excessive Derivation

- Creating multiple layers of inheritance for it’s own sake is adding complexity.
- Complexity does not mean cleverness. Clarity is more important to me as Software Developer. You can add a property called 'Type' for classA instead of creating a class that inherits from ClassA. Sure, where applicable.
- Reduce the number of levels as much as you can when deciding what classes your system includes.

Auto properties vs. private members

- Define a private variable and a relevant property only when you have business logic to be written inside the property getter and setter.
- Use auto properties when you are dealing with dummy entities. The code would be much cleaner.

Next Article

We will either talk about ASP.NET or Software Architecture review. Tell me which one you prefer ? Happy coding till then,,,

It's my first comment so I will start by admiring your articles in general that I always read. Regarding singleton, your example is valid (when a void method that only outputs a parameter), but what do you think about a singleton that writes to a store? (saying caching) Also I would like the next article to be on the software architecture review topic thanks!

Like
Reply

To view or add a comment, sign in

More articles by Houssam Hamdan

  • .NET Code Reviews - C#

    I am often asked to look into an existing code base to give the company an idea of the quality of the code. This…

    4 Comments
  • Pluggable Architecture

    In the .NET Application Architecture Guide , 2nd edition, Microsoft recommends implementing pluggable design to…

    3 Comments
  • SOA Simplified

    What exactly is Service Orientation or Service Oriented Architecture / Application? Simply, it is the decomposition of…

    1 Comment
  • Security-related HTTP Response Headers

    This page lists useful security-related HTTP headers. In most architectures, these headers can be set in web server…

  • Beacons - The Untold

    Recently, i conducted a research about the beacons technology. After playing around with one of the vendor's SDK and…

  • Big Data - Not yet defined

    In this article we will be defining the term Big Data. In fact we'll be presenting a number of different definitions in…

  • Big Data - The Perfect Storm

    This series is designed to give a broad understanding of Big Data, both to those who already have some understanding of…

  • ASP.NET Security Secrets

    Most Software Developers lack in security knowledge. They simply don't understand how our applications are attacked.

    8 Comments
  • The Single Responsibility Principle

    We'll begin by defining The Single Responsibility Principle (aka SRP) and demonstrating what are the problems when…

    2 Comments
  • Clean Code

    Let me start by [Marin Fowler] quote about clean code. "Any fool can write code that a computer can understand.

    20 Comments

Explore content categories