Monday, 6 March 2017

7 Habits of highly effective C# Programmer(3 Series) - Part 1

text3.png
Every Programmer dream is to write a few lines of code. It should look sleek and it solves a very complex problem. If only few lines of code can solve a very complex, there is nothing better for a programmer that made his day. So becoming effective on your programming skills should be task that should be performed every day.
In this blog we will look into the set of tips and tricks that will make you efficient C# programmer. Basic takeaway from this article should be that you understand the framework. Remember the days, we have spent days even weeks on single issue just because we did not understand what our compiler actually does.
Any programming language or framework you choose, you must learn the internal architecture on how it works and how it has been designed and for what purpose. This will reduce lots of stress and avoid spending nights, trying to solve some stupid issue which would have been solved easier if we would have known the framework better.


We will look into 7 important features that makes you effective and code efficient

1.Understanding JIT and MS-IL

2. Understanding Generics

3. Use `Yield` Keyword in Statement

4. Understanding Closure

5. Use nameof

6. Use IQueryable extension


7.Consider Weak Reference for large Objects

So we will take the below tips of understanding some of the basics principles and also some tips so you can code effectively. You can skip Understanding JIT and IL, if you are experienced .Net programmer, it is explained to make sure the .Net developers understand the basics of .Net.
1.Understanding JIT and MS-IL
If you are starting your career in .Net framework you need to know what CLR means, how MSIL (Microsoft Intermediate language) is created and how JIT (Just In Time) compiler converts MSIL to native code on demand. And you need to understand JIT Compilation Vs. NGen Compiler. Understand how NGen improves performance.
In primitive level, .Net Managed execution process consists of four steps.
  • Compiler chosen based on your file extension.
  • Compile your code to MSIL
  • Compile MSIL to Native Code
  • Run the code
Above steps should be properly understood, I am leaving it as it is now, as for this blog is concerned this knowledge as trivial for any good .Net developer. But it is very important to understand and make it your second nature, so you understand what it means, when you get CLS Complaint error.
2. Understanding Generics
Generics provides compile time error rather than runtime error, so it is easy to develop an efficient code when you know ahead that your code might fail. Earlier in 1.x .Net framework had only System.Object which we can code against and add appropriate runtime checks to ensure runtime object was what you expected.
With the addition of generics we have more control of code at compile time and lots of type checks and plumbing code can be totally removed. Generics substitutes your often called T  in the source code on runtime will help you better use the generics and where not to use it.
Before going in details you need to understand
  • Open generic type
  • closed generic type.
When the specific instance of a generic type, in which all the type parameters are specified, is called a Closed generic type For eg:- Dictionary<int, string>.
If only some or none of the parameters are specified then it’s called Open generic type, For eg:- Dictionarystring> or eg:- Dictionary
C# compiler takes our source code and creates MS-Intermediate language (IL) definition for the type. When the code with generics gets compiled to IL, the IL contains the placeholder for an instantiation of specific completed generic type. The JIT compiler completes the definition when it creates the machine code to instantiate a closed generic type at runtime. This practise introduces a trade-off between increased code and time and space required to store data.
When the generic class is JIT compiled, the JIT compiler examines the type parameters and emits specific instruction depending on the type parameter.
Why should I need to know how generic works?
Logic for creating Machine code for reference generic type is different from the Value and Reference generic type.
All the below instantiation (Reference type) share the same code at runtime:
           List<string> tokenList = new List<string>();
           List<Stream> fileStreamList = new List<Stream>();
           List<CustomClass> classList = new List<CustomClass>();
All the below instantiation (Value type) does not share the same code at runtime
           List<int> tokenList = new List<int>();
           List<double> price = new List<double>();
           List<CustomStruct> structList = new List<CustomStruct>();


This may be interesting but why should I care? Generic type that will be used with multiple different reference types do not affect the memory footprint. However when closed generic type contains value types as parameters, that JIT compiled code is not shared. So choosing carefully the generics based on value and reference type is important, so if possible we can avoid value type reference in the generics and save the compiler, code generation headache. We have explained closure below and it will explain the generics and how the class is generated for closure, will let you understand the importance of the generics code substitution.
So basic understanding of the generics compilation in IL and JIT process will help us write more effective code, when using generics when we understand the underlying design on more details about the generic compilation refer here.

7 Habits of highly effective C# Programmer(3 Series) - Part 2

7 Habits of highly effective C# Programmer(3 Series) - Part 3

No comments:

Post a Comment

Understand and Implement Roles Vs Claims Based authentication in MVC Web API

In this blog, you will learn on how to Implement Roles and claims based authentication. You will also understand what is the difference b...