Quick Tips To Create Good Applications In .NET Framework

For the days and years to come, we will always be working on or wanting to improve code quality and get things done in better ways. It is always hard at first, but things come with experience. Great applications certainly have great architectures. Great architectures have great programmers behind. Those great programmers certainly have a high level of intellect. The least thing we can do is to learn. I am a beginner programmer and here are my few observations on how the great programmers out there are creating great applications

1. Follow SOLID Principles.

SOLID principles help us to stick to Object Oriented Programming principles. They make the code more flexible and understandable and help us in maintaining code.

S - Single Responsibility
O - Open for extension, Closed for modification.
L - Liskov Substitution
I - Interface Segregation
D - Dependency Inversion

Here is a detailed tutorial on Solid Principles in C#.

2. Follow Design Patterns.

They are essentially important aspects to solve programming problems. If followed correctly, they are blessings. If they go wrong, they can cause goosebumps for developers. We need to understand which pattern is created for which reason.

Three major categories:
    1. Creational
    2. Structural
    3. Behavioral
There are dozens of design patterns in .NET. Here is a detailed tutorial: Design Patterns In .NET.

3. Handle Exceptions

Always follow the try-catch block. Never keep catch block empty and always log the exception to understand what went wrong.
  1. try {} catch (Exception ex) {  
  2.     //Don’t keep me empty!! Log my ex !!  
  3. }  
Here is a detailed tutorial: Exception Handling in C#

4. Dispose unused resources

Dispose of the unused resources using Finally block always or implement IDisposable interface and create resources inside the using() block.
  1. try{  
  2.    }catch(Exception){  
  3.    }finally{  
  4.       //Dispose the unmanaged  
  5.       }  
  6.    using(resource){  
  7. }  
Here is a detailed tutorial: Dispose vs Finalize in C#

5. Optimize conversions

Understand the usage of Convert.To[Type](param) vs [Type].Parse(param)

Convert.ToInt32() and Int32.Parse() which to use when? One will initialize with a default value and the other will throw an exception. I guess now we understand which one is better at which situation.
Here is a detailed tutorial: Type Conversions in C# 

6. Use of Not Null

Understand the usage of Not Null conditional access, null-coalescing operator, and null checks,
  • Whenever we are trying to access any property always use null check before accessing it.
  • ?. stands for not null conditional access, it is shorthand to access property only if it is not null
  • ?? is useful when we have null values and we want to assign some other value in case we have null values. It saves a lot of decision making if used correctly.
Learn more here: Nullable types in C#


Remember, it is easier to write spaghetti code. As soon as we start creating an application, we may think that getting a final product in less time is the only requirement. But, when we fall into this pitfall, there is no going back. Then, we have to invest a lot of time in mass code re-factoring to scale the application or to add new features. Getting things done can be done easily; however, getting them done correctly is important. The process we follow leads us to the final outcome. The application that we create and leave for others to work upon leads to bigger problems if it is not made correctly. Remember, no one likes to work on buggy code. Make your code learning for someone who watches it. Don’t make it a lesson that someone should remember for how things were messed up.