SSW Foursquare

Do you avoid Empty code blocks?

Last updated by Brady Stroud [SSW] about 1 month ago.See history

Empty Visual C# .NET methods consume program resources unnecessarily. Put a comment in code block if its stub for future application. Don’t add empty C# methods to your code. If you are adding one as a placeholder for future development, add a comment with a TODO.

Also, to avoid unnecessary resource consumption, you should keep the entire method commented until it has been implemented.

If the class implements an inherited interface method, ensure the method throws NotImplementedException.

public class Example
 {
       public double salary()
       { 

       }
 }

Figure: Bad Example - Method is empty

public class Sample
 {
       public double salary()
       {
               return 2500.00;
        }
 }

Figure: Good Example - Method implements some code

public interface IDemo
 {
       void DoSomethingUseful();
       void SomethingThatCanBeIgnored();
 }
public class Demo : IDemo
 {
       public void DoSomethingUseful()
       {
              // no audit issues
             Console.WriteLine("Useful");
       }
       // audit issues
      public void SomethingThatCanBeIgnored()
      {
      }
 }

Figure: Bad Example - No Comment within empty code block

public interface IDemo
 {
       void DoSomethingUseful();
       void SomethingThatCanBeIgnored();
 }
public class Demo : IDemo
 {
       public void DoSomethingUseful()
       {
              // no audit issues
              Console.WriteLine("Useful");
       }
       // No audit issues
       public void SomethingThatCanBeIgnored()
       {
              // stub for IDemo interface
       }
 }

Figure: Good Example - Added comment within Empty Code block method of interface class

Adam Cogan
We open source. Powered by GitHub