Click here to Skip to main content
16,017,788 members
Articles / Web Development / ASP.NET
Article

Best practices in ASP Dot NET

Rate me:
Please Sign up or sign in to vote.
1.00/5 (19 votes)
18 May 20053 min read 45K   19   7
A quick reference on the "Best practices in ASP Dot NET"

Best practices in ASP.NET<o:p>

                                                - by Raghunandan S<o:p>

<o:p> 

ASPX file<o:p>

  • The labels / hard-coded values in the .aspx file should be fetched from the properties files / .xml files. <o:p>
  • All the exceptions should be propagated to the end-user with a proper message. <o:p>
  • Code should be modular and read-able. <o:p>
  • Unwanted html tags should be removed from the .aspx files. <o:p>
  • Unwanted breaks, blank spaces should be removed as they increase the data bandwidth. <o:p>
  • While using commenting style for JavaScript functions use server side commenting instead of the usual HTML comments, This reduces the data bandwidth
      Server side comments : <code><%   %></code>
      Client side comments  : <code> //<code> or <code><!--  --></code> <o:p>
  • Code should be indented with 2 spaces only (more spaces means more data bandwidth). <o:p>
  • Use page-caching for frequently visited screens. <o:p>
  • Use cascaded style sheets instead of having inline styles. <o:p>
  • Avoid unwanted inclusions of .js files; include only the required .js files. <o:p>
  • Avoid html frames as they are considered style of web designing. Instead of html frames, use include-files. <o:p>
  • Avoid many hidden fields in the files, this adds on to the data bandwidth. <o:p>
  • Always get the absolute path of the images or the include files. <o:p>
  • Reduce the usage of Custom Tag (server controls) until and unless you are sure that the Custom Tag you are using are optimized.<o:p>

<o:p> 

 Code behind / class file<o:p>

  • Module level variables: Module level variables (global to the class) should be avoided as they not thread-safe. <o:p>
  • String concatenation: Avoid String concatenation, use StringBuilder instead. Always initialize the StringBuilder with an approximate value. This will give a better performance since the memory extension calls will be reduced.
      <code>stringBuilder = New StringBuilder (500)</code> <o:p>
  • Variable initialization: Do not initialize any variable unless you reach a condition where the object is necessary.
    <code>
      String strName = Nothing

      if(intId == 1)
      {
        strName = "Raghu"
      }
    </code><o:p>
  • Disposing the Objects: Manually dispose the objects after use. Though .NET framework supports garbage collection, manual disposal increases the performance of the application. <o:p>
  • Hard-coded constants: Do not hard-code any string constant value or numeric constant value. Assign the value to a constant (give a meaningful name which represents the constant value) and use. <o:p>
  • Using Interfaces: Use Interface rather than implementation. For Eg: Use List instead of ArrayList, This will reduce the Typecasting of Objects. <o:p>
  • <st1:place><st1:placename>View <st1:placetype>State: There are lots of drawbacks to the use of view state, however. First of all, it increases the data bandwidth of the page both when served and when requested. There is also an additional overhead incurred when serializing or de-serializing view state data that is posted back to the server. Lastly, view state increases the memory allocations on the server.
         Several server controls, the most well known of which is the DataGrid, tend to make excessive use of view state, even in cases where it is not needed. The default behavior of the ViewState property is enabled, but if you don't need it, you can turn it off at the control or page level. Within a control, you simply set the EnableViewState property to false, or you can set it globally within the page using this setting:
      <code><%@ Page EnableViewState="false" %></code>

    If you are not doing postbacks in a page or are always regenerating the controls on a page on each request, you should disable view state at the page level.<o:p>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Team Leader LnT Emsys
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Aoi Karasu 16-Nov-09 0:36
professional Aoi Karasu 16-Nov-09 0:36 
GeneralGood article Pin
rabashani27-Jan-06 23:53
rabashani27-Jan-06 23:53 
GeneralStringBuilder query Pin
NeverHeardOfMe19-May-05 12:43
NeverHeardOfMe19-May-05 12:43 
Questionwhere is the rule #1 ? Pin
DavidNohejl18-May-05 12:19
DavidNohejl18-May-05 12:19 
GeneralMore explanation and justification needed Pin
David Wulff18-May-05 6:26
David Wulff18-May-05 6:26 
Your article would be useful if you actually went on to explain why you consider them to be best practises. Also, it is ASP.NET, not ASP Dot Net or ASP.Net.

RaghuOnDotNet wrote:
Reduce the usage of Custom Tag until and unless you are sure that the Custom Tag you are using are optimized.

What does this mean?

RaghuOnDotNet wrote:
Avoid page-caching

Why?

RaghuOnDotNet wrote:
Reduce the usage of hidden fields in the JSP, this adds on to the bandwidth

I thought you were talking about ASP.NET, not JSP, and even so... what do you mean?

RaghuOnDotNet wrote:
Always get the absolute path of the images or the include files.

Why?

RaghuOnDotNet wrote:
Module level variables (global to the class) should be avoided as they not thread-safe.

So where do you define your class's fields/attributes? And if you're using codebehind, how do you define your client-side server controls?

RaghuOnDotNet wrote:
Do not initialize any variable unless you reach a condition where the object is necessary.

I thought the .NET data types were initialised to a default value anyway, so what are you saving?

RaghuOnDotNet wrote:
Use Interface rather than implementation. For Eg: Use List instead of ArrayList, This will reduce the Typecasting of Objects

Can you give an example of this in use, I'm not certain how you mean it to be read.


About the only things that seemed reasonable to me was the bit about not using ViewState if you don't need it, StringBuilder when appropriate, and the basic web design rules.




Die Freiheit spielt auf allen Geigen
GeneralRe: More explanation and justification needed Pin
DavidNohejl18-May-05 11:47
DavidNohejl18-May-05 11:47 
GeneralRe: More explanation and justification needed Pin
David Wulff18-May-05 13:46
David Wulff18-May-05 13:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.