Friday, November 28, 2014

Tell, don't ask design principle

In this article, we will be discus about a design principle called Tell, Don't Ask. This principle is aimed at designing the classes in such a way that, when they are used, instead of querying their object/instance and performing the operation based on the output it provides, the instance should handle the logic itself.
In other words, it means, the instance of the class should take the decision itself, rather then calling code telling it what to do. So let's discuss with a code sample which violates the principle and than will implement the same using this principle.
Consider the following piece of code. Here, we have the following business logic implemented.

We check for the discount applicable on the vehicle. If it is greater than 5%, we subtract 5000 from the actual price else 3000, as a discount. We also have a net price price defined for the vehicle. After applying the discount, if net price is more than this base price, we can further provide free accessories else we do not.
This type of code will have following issues:
1. If we have same type of logic in other areas of application, changes in the logic will require changes in all the locations.
2. Client code is now aware of the business logic to calculate the cost of the vehicle.
3. Some of the member functions/properties are unnecessarily exposed as public members, to the client code.
This is where we can use the Tell, Don't Ask principle. So we change our code to shift the entire logic into the CostCalculator class and pass the required data to it. It calculates the net price and based on results, it decides whether it further needs to make a call to the ApplyAccessories() method or not. Our code now changes to:

So in this case, the entire calculation logic, along with logic to add accessories, inside the CostCalculator class. We simply provide the required data as input to this class. Further, the ApplyAccessories method is now converted into a private method and client code has no idea of the cost calculation business logic. Client code will simply pass the data to the CostCalculator and rest will be done by its NetCalculation method. In other words, we are telling the CostCalculator instance to perform the calculations, rather than asking anything from it.

Generic classes using generic type parameter in C#

In this article, we will discuss the concept of the generic classes in C#. Before we start, let us try to figure out what exactly does this concept of generics means.
As a .net developer, you will be aware of the data type object, which is the base type of all the data types in the .net framework. We can declare a variable of type object and store any kind of value in it, whether it is a string or integer or any other type. So object data type here acts as a generic type. Similarly, we can specify classes and methods with a generic definition and any type can be specified in place of it when it is being used. In simple language, we can declare a container and use this container for any data type
To start with we will be creating a class type which specifies it to be generic using the following syntax :

Here, we have created a blue print of class, which can be used for any of our data type like integer, string or any custom class. We specify it to be a generic by using the <T> as  type parameter. This is the most important part of the syntax, without which, it would have been a simple class. This i.e. the <T> is also known as generic type parameter.
Now we will be adding a method, which will be again using a parameter of type <T>,  and then we will be using this to illustrate the functionality of generic type.

Here, we have created a method which takes an input parameter of type T, which can be any type that can be specified when we use this class, and adds it to a list which is also of type T. Finally this list is returned by the function created.
So to use this, we will be using the code as :

So here, we have used the generic class to create the integer type of listing. If you try to add any string or any other type of value, it will result in compile time error as we have declared the type of the list to be integer when we first instantiated the class as integer type. Similarly you can use this for the string type or any other type of your choice. Thus, it provides the type safety and checks the type for which it is to be used at the compile time itself.
Some of the built in generic classes that we have in C# include, Stack, LinkedList, Queue etc in the System.Collections.Generic namespace.


Func - generic delegate in C#

In this article, we will discuss about the concept of Func generic delegate. In C# 3.5, a generic delegate was provided with the keyword Func, which acts like a template for the user, to create and delegates. The general signatures of this delegate are  Func<T, TResult>.
Here, T is the input parameter for the method and TResult is the return parameter of the method that will be called using this delegate. This is just a single declaration. You can have the signatures like Func<Int32, Int32, Boolean> or Func<String, Int32, Int32, Boolean>. Here, the last parameter is always the result type and rest of them are the input parameter types of the methods, that can be encapsulated by these delegates.
So let's discuss this with a simple example. We will be creating a delegate which will take two input parameters of type integer, and result a boolean value, which specifies whether the first number is greater then second or not. So our generic delegate signatures will be Func<Int32, Int32, Boolean>. Then we will be creating a function which will be encapsulated by this delegate.
See the code below :

Next, to use this delegate, we will be assigning the method with the same signatures, to this delegate and call the delegate object and pass the required parameters to it. Check out the code below :

So this will return the true or false, based on your input parameters. Similarly, you can declare the delegate with more parameters, of the required type. If you try to go to the definition of this declared delegate, it will display the generic signatures of it, like below. By generic signature, here we mean that depending on the signature of the delegate we declared, its definition is displayed dynamically.

One more interesting point is the use of the anonymous methods or the lambda expression with this delegate. You could have avoided writing the method CheckNumbers, by using the following anonymous or lambda expression declarations.

So next time you need a delegate, go for this one. So this was all about the concept of Func delegate. Happy Coding...!!!