Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Monday, August 31, 2015

Guidelines for using control structures(if/else if/Switch )

Important Note : program readability is most important after working software.

If statements :

  • Write nominal path through the code first ;then write the unusual cases last
  • Put the normal case after the if rather than the else
  • Try to consider the else clause 
If else if Statements :

  • Simplify the complicated condition checking with a function call
  • Keep the most common case first
  • Try to use switch/case statement instead of if else statements
Switch/Case statements :
  • Order cases alphabetically or numerically
  • Put the normal case first
  • Put the most frequently executed case first and the least executed cases last
  • Use default case for detecting legitimate defaults or errors

Guidelines for using and naming variable in C#

Important Note : program readability is most important after working software.

Guidelines in using variables :
  • Declare and define variable close to the where it is being used for the first time
  • Use const wherever possible
  • Keep variable scope as short as possible
  • Try not to use global variables
  • Use variable for one purpose only
  • Avoid variable with hidden messages
Guidelines in naming variables :
  • Code is read far more times than it is written. Favor read-time convenience over write-time convenience 
  • You should not give variable name like how you name your dog
  • Goodness/ Badness are determined by variable`s name
  • A good variable should be readable ,memorable and appreciate
  • Avoid the variables names like temp, x, y (who knows what does that mean ??)
  • Try to give names to problem domain
  • Come up with a standard naming conventions in cases like revenveTotal, totalRevenve.. etc.
  • For naming looping indexes, Try to come up with a more descriptive names than I, j, and k
  • Come with a better name than just using flag for naming status variables
  • Give temporary variables more descriptive names
  • Do not use negative variable names like “NotFound”, Use positive names like “Found”
  • Avoid misspelled word in variable name like cnt(count) ,msg(Message)
  • Do not differentiate variable names solely by capitalization

Monday, August 10, 2015

The C# dynamic Keyword in C# 4.0 and upwards

dynamic keyword a specialized form of System.Object, in that any value can be assigned to a  dynamic data type.
• a data point declared with the dynamic keyword can be assigned any initial value at all, and can be reassigned to any new (and possibly unrelated) value during its lifetime.
• IntelliSense is not possible with dynamic data.
• It is not mandatorily to use dynamic keyword  (With Dynamic keyword, The intention is to reduce verbose, that’s it)
• It is not statically typed it means that validity of dynamic statement is checked at runtime not at compile time
• Invalid dynamic statement is validated at run time using RuntimeBinderException  available in Microsoft.CSharp.dll
• Cannot make use of lambda /anonymous methods
• Cannot understand any extension method
• It is used in cases where your .Net application interacts with COM libraries (like Microsoft Office products) or VSTO application
• Can be used as parameters, return value or a member of class/structure


Extract Method Refactoring in C#

When to use this:
  1. Length of the method is not an issue
  2. To improve clarity of the code
  3. The name of the new method will reveal the intention of the code in a better way. If you can't come up with a more meaningful name, don't extract the code
       Example : Suppose we have method to use this extract method. by selecting console.WriteLine statement as below

        public void Add(int x, int y)
        {
            int z = x + y;
            Console.WriteLine(z);

        }

We can also right-click the selected code, point to Refactor, and then click Extract Method (Short cut  is CTRL+R, M)  to display the Extract Method dialog box.

The final extracted method is as follows