Top 10 way to optimize C# code



Code optimization is used for improving application performance and reduces execution time.
Optimization of code is done by Code Alteration and Minimizing Code Size

The following are the ways by which code can be optimized:

  

1] Alter Complex operation by a simple one. In other words reduce the strength of the operation. For e.g.  x ^ 3 can be written as x * x * x.


2] Remove unused code:

    i) Remove code that never gets executed.
ii) Remove variables whose values are not used.


3] Delete white Space: White space is required for better     understanding of code or better organization of code for   developers. White space should be removed carefully without   any error on the page. White space elimination will result in   code size reduction and also in space requirements.

 

4] Unnecessary comments should be removed from the source code because it will increase the download time. Aside from the once widely used META "keywords" tag, most META tag information is just glorified commenting.


5] Minimize large function and Variable Names of functions and variables should be clear as defined by good programming practice but names that are too lengthy should be avoided. The user does not require the source code of the application. only the developers are concerned with the function and variable names. Thus the name should be small and as far as possible should disclose the meaning.


6] If there is some code like a=a+1, instead we write a++. Both lines accomplish the same operation


7] In C#, variable declaration is not compulsory but there are some good reasons to declare the variable.

    i) Variable declaration increases performance: Variables that are used without declaration cause extra processing.

    ii) Variable declaration prevents accidental misuse of them: By declaring variables we can avoid a situation of giving the same name to various variables meant for various purposes.


8] Use Inheritance if we write two classes with similar data   member i.e. Variables and member functions due to this code   duplication program consume more memory


9] Use List whenever require. working with Array List for the same work can make the working of code slower. This is especially if you are storing multiple types of objects within the same list.


10] Small code takes less memory try to get work done using operator to concise the code and make it work in a single line.Use operators like && that would allow you to mention all the conditions in a single line.

 

No comments:

Post a Comment