DEV Community

Cover image for Test-Driven Development with C# Part 2
Saoud
Saoud

Posted on

Test-Driven Development with C# Part 2

Abstraction

Terminology

Abstraction: The process of hiding and encapsulating complex code so that it's easier to use.

Terminology: Interfaces

Extends: Adding additional functionality to a class with an interface.

Interface: A blueprint of things (such as declarations, properties and methods) that must be included within any class that utilizes the interface.

Example

This is the syntax for extending a class with multiple interfaces:

public class ClassToBeExtended : InterfaceToInclude, InterfaceToIncludeTwo
Enter fullscreen mode Exit fullscreen mode

This is a sample interface. Interface names begin with a capital I.

interface IMotor
{
    string OnSwitch();
}
Enter fullscreen mode Exit fullscreen mode

Abstract Classes

Terminology

Inheritance: The process of passing on functionality from a parent class to a child class.

Abstract class: A class that is never instantiated.

Single inheritance: When a programming language allows a child class to inherit from only one parent class. This is in contrast to multiple inheritance.

Example

An example of an abstract class with an abstract method:

abstract class Animal
{
  public abstract string Walk();
}
Enter fullscreen mode Exit fullscreen mode

Difference Between Abstract Classes and Interfaces

  • A class can only inherit from one abstract class. However, a class can be extended by multiple interfaces.
  • An abstract class provides core functionality to its child classes. Meanwhile, an interface provides peripheral functionality to the classes it extends.
  • Abstract classes are more performant than interfaces.
  • Abstract classes can have fields and constants while interfaces cannot.

Overloaded constructor:

Terminology

When we define two or more constructors in a class.

Example

// Constructors below

public Item(string description)
{
    Description = description;
    _instances.Add(this);
}

public Item(string description, int priority)
{
    Description = description;
    Priority = priority;
    _instances.Add(this);
}
Enter fullscreen mode Exit fullscreen mode

When to Use Overloaded Constructors

  • Order of parameters: If we want to pass in a priority first and then a description, we could add an overloaded constructor that accounts for arguments being passed into the constructor in a different order.
  • Type of parameters: If priority is sometimes a string (high, medium, and low) and sometimes an int (1 through 5), we can use an overloaded constructor to account for this different type of data.
  • Flexibility of parameters: We can choose to have a different number of parameters (as seen in the example we use above) or even no parameters at all.

Try/Catch Blocks:

Terminology

Exception handling: The act of coding a program to handle exceptions in a manner that doesn't lead to the application crashing.

Throwing an exception: When a program crashes and has an error.

Try/catch block: Composed of two parts: try and catch. The try part is where we put the code that may cause an exception. The catch part to handles that specific exception.

Example

using System;

class Program
{
  static void Main()
  {
    try
    {
      int value = 1 / int.Parse("0");
    }
    catch (Exception ex)
    {
      Console.WriteLine("Message = {0}", ex.Message);
      Console.WriteLine("Source = {0}", ex.Source);
      Console.WriteLine("StackTrace = {0}", ex.StackTrace);
      Console.WriteLine("TargetSite = {0}", ex.TargetSite);
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

catch blocks can take an Exception as a parameter, which can help handle exceptions even more effectively. The Exception class has a number of useful properties:

  • Message: a short description of the exception;
  • Source: the application name;
  • StackTrace: the path to what caused the exception;
  • TargetSite: the name of the method where the exception occurred.

C# Coding Style Convention Review

Indentation and Spacing

  • Each brace should begin on a new line. This is called Allman Style. A single line statement block can go without braces but the block must be properly indented on its own line and must not be nested in other statement blocks that use braces.
  • Avoid more than one empty line at any time. For example, do not have two blank lines between members of a type.
  • Avoid free spaces. For example, avoid if (someVar == 0) . . ., where the dots mark the extra free spaces.
  • Be consistent with indentation. Epicodus uses two spaces of indentation with the .NET documentation suggests four spaces of indentation. Both are acceptable - as long as you remain consistent throughout your code.

Capitalization and Naming

  • Prepend an underscore _ to all private field names and use lower camelCase.
  • Use PascalCase to name all local variables and fields that should remain constant.

Other Conventions

  • Always specify the visibility, even if it is the default. For example, we should say private string Description, not string Description.
  • Always declare the variable type, including if that type is a class. For example, we should use the code Item newItem = new Item(...), since the type of newItem is Item. We should not use the code var newItem = new Item(...) since the type of newItem is Item. In general, var is too vague and shouldn't be used.
  • Namespace imports should be specified at the top of the file, outside of namespace declarations, and should be sorted alphabetically.

Top comments (0)