Thursday, October 09, 2008

Delegates

Covariance : a method can return a type that is derived from the delegate's return type.

Contra-variance : a method can take a parameter that is a base of the delegate's parameter type.

ex. delegate object MyCallback(FileStream s);
can bound to string SomeMethod(Stream s);

Covariance and Contra-variance are supported only for reference types, not for value types or void.

Shortcuts while working with delegates:

  • If a function expects delegate, just pass function name.
  • use anonymous method to create delegate.
  • don't specify callback method parameters, if you don't need
  • delegate method can use local variables of the invoking method.

ex. We have a delegate as follows, and the function which accepts the delegate :

delegate void TheDelegate(object o);
static void ProcessFunction(TheDelegate d,object o)
{
  d(o);
}

int x = 0;
ProcessFunction(Foo, 5);
ProcessFunction(delegate(object o){
  Console.WriteLine("Anonymous method");}, 5);
ProcessFunction(delegate{
  Console.WriteLine("Simpler Anonymous method");}, 5);
ProcessFunction(delegate{
  Console.WriteLine("Modifying local x to 9"); x = 9;}, 5);
Console.WriteLine("Value of x : " + x);

Wednesday, October 08, 2008

OOP Basics

Object-oriented programming (OOP) is a programming paradigm that uses "objects" and their interactions to design applications and computer programs. Programming techniques may include features such as encapsulation, modularity, polymorphism, and inheritance.

Why OOP?

  • It bridges the gap between problem and solution domain.
  • Lets you decompose problem into smaller sub-problems that can be solved separately.  Modularity.
  • Enables reuse

Abstraction

  • Abstraction, as a process, denotes the extracting of the essential details about an item, or a group of items, while ignoring the inessential details.
  • Abstraction, as an entity, denotes focused representation for an actual item.

We can have varying degrees/levels of abstraction.
Abstraction is most often used as a complexity mastering technique.
As we move to higher levels of abstraction, we focus on the larger and more important pieces of information.

Encapsulation

  • Encapsulation, as a process, means the act of enclosing one or more items within a (physical or logical) container.
  • Encapsulation, as an entity, refers to a package or an enclosure that holds (contains, encloses) one or more items.

The walls of the enclosure may be "transparent," "translucent," or even "opaque.
Everything that is encapsulated is not hidden.

Abstraction is a technique that helps us identify which specific information should be visible, and which information should be hidden.
Encapsulation is then the technique for packaging the information in such a way as to hide what should be hidden, and make visible what is intended to be visible.

Inheritance

Inheritance is a way to form new classes using classes that have already been defined. The new classes (derived classes), take over (or inherit) attributes and behavior of the pre-existing classes, which are referred to as base classes. It is intended to help reuse existing code with little or no modification.

Inheritance is also sometimes called generalization, because the is-a relationships represent a hierarchy between classes of objects.

If the derived class adds new interface elements to a derived type, it can still be substituted for the base type - but the substitution is not perfect. This can be described as an is-like-a relationship

We can have object of another class inside our new class, this concept is called composition/aggregation. Composition is often referred to as a "has-a" relationship.

Composition should be favored over inheritance.

Polymorphism

Polymorphism is the ability of objects belonging to different types to respond to method calls of the same name, each one according to an appropriate type-specific behavior. The exact behavior is determined at run time (this is called late binding or dynamic binding).

Compile time polymorphism : function and operator overloading.
Run time polymorphism : generics and overriding.

Templates

Templates are a feature of the C++ programming language that allow functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one.

Thursday, October 02, 2008

Refactoring (Part 3)

  • Decompose/Consolidate Conditional
    You have a complicated conditional (if-then-else) statement - Extract methods from the condition : if and else parts
  • Consolidate Duplicate Conditional Fragments
    The same fragment of code is in all branches of a conditional expression - Move it outside of the expression.
  • Remove Control Flag
    You have a variable that is acting as a control flag for a series of boolean expressions - Use a break or return instead.
  • Replace Nested Conditional with Guard Clauses
    A method has conditional behavior that does not make clear the normal path of execution - Use guard clauses for all the special cases : The guard clause either returns, or throws an exception.
  • Replace Conditional with Polymorphism
    You have a conditional that chooses different behavior depending on the type of an object - Move each leg of the conditional to an overriding method in a subclass. Make the original method abstract.
  • Introduce Null Object
    You have repeated check for a null value - Replace the null value with a null object.
  • Introduce Assertion
    Make the assumption explicit with an assertion.

Why access specifiers?

  • Without access specifiers, the client programmer can do anything with that class and there is no way to enforce rules
  • Library designers can change internal workings of the class without worrying about how it will affect the client programmer.

C++ uses three explicit keywords to set the boundaries in a class: public, private, and protected.

public means the following definitions are available to everyone. private means that no one can access those definitions except you, the creator of the type, inside member functions of that type. protected acts just like private, with the exception that an inheriting class has access to protected members, but not private members.