Click here to Skip to main content
Click here to Skip to main content

Best practices in developing ASP.NET applications

By , 19 Jan 2011
 
Best practices in developing ASP.NET applications...
  1. Remove unused private fields and functions.
  2. Do not cast unnecessarily. Avoid duplicate casts where possible, since there is a cost associated with them.
  3. Properties that return arrays are prone to code inefficiencies. Consider using a collection or making this a method.
  4. To test for empty strings, check if String.Length is equal to zero. Constructs such as "".Equals(someString) and String.Empty.Equals(someString) are less efficient than testing the string length. Replace these with checks for someString.Length == 0.
  5. Methods in the same type that differ only by return type can be difficult for developers and tools to properly recognize. When extending a type, be sure not to define new methods that differ from base type methods only by type.
  6. Use stringbuilder instead of string types for string manipulation.
  7. Use String.Format instead of concatenating and appending strings.
  8. Use Type.TryParse rather than Convert.ToDestinationType(). For example, use int.TryParse() rather than Convert.ToInt32() which might throw an exception.
  9. Override Equals() method wherever applicable in your classes.
  10. Consider passing base types as parameters - Using base types as parameters to methods improves re-use of these methods if you only use methods & properties from the parameter's base class. E.g. use Stream instead of FileStream as a parameter when only calling Stream.Read(), this makes the method work on all kind of streams instead of just File streams.
  11. Do not catch general exception types - You should not catch Exception or SystemException. Catching generic exception types can hide run-time problems from the library user, and can complicate debugging. You should catch only those exceptions that you can handle gracefully.
  12. Use properties instead of visible instance fields.
  13. Follow the same naming conventions accross the solution.
  14. Remove unwanted commented code, indent code properly.
  15. Use curly braces with in an if statement, even if there is a single statement in the if block. This will provide better readability.
  16. Make sure to refactor your code to move the duplicated code to common reusable functions.
  17. Move one time control settings into the .aspx page rather than having them in the code behind in if(!IsPostback) block.
  18. Use inheritance wherever possible, which enables code reuse and also reduces the amount of code we have to write and test.
  19. Move the reusable JavaScript functions to an external .js file instead of having them on the page.
  20. For controls that are declaratively specified on the page, tie the event handlers to the controls events on the aspx page rather than initializing them in the codebehind. If the controls are built dynamically, then we do not have a choice.
  21. Make sure to check for nulls when using any type retrieved from a session, querystring or a database to avoid NullReferenceExceptions.
  22. Use foreach loop instead of using for loop which may lead to out of boundary run time exceptions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mahendra Vishwakarma
Software Developer (Senior)
India India
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralReason for my vote of 5 good one!!!!membersagar viradiya1 Feb '12 - 23:49 
Reason for my vote of 5
good one!!!!
GeneralReason for my vote of 4 Great articlememberajaykulkarnir14 Feb '11 - 16:37 
Reason for my vote of 4
Great article
GeneralReason for my vote of 5 Good summary of tips for beginner pr...memberDuffmanLight7 Feb '11 - 3:22 
Reason for my vote of 5
Good summary of tips for beginner programmer.
GeneralThis is rather a confused list - most of the points are abou...memberMember 25604727 Feb '11 - 2:02 
This is rather a confused list - most of the points are about coding practices and conventions in C# (or C, C++) or relate to .NET applications in general, so they are not about "developing ASP.NET applications".
GeneralReason for my vote of 1 Not as described by title. Mostly a ...memberMember 25604727 Feb '11 - 1:13 
Reason for my vote of 1
Not as described by title. Mostly a random list of coding suggestions
GeneralReason for my vote of 5 nice onememberPranay Rana2 Feb '11 - 0:03 
Reason for my vote of 5
nice one
GeneralReason My vote of 5 all point is sounds like greatmvpRaviRanjankr1 Feb '11 - 0:01 
Reason My vote of 5
all point is sounds like great
GeneralReason for my vote of 5 Nice Tricks! My vote of 5mvpRaviRanjankr1 Feb '11 - 0:00 
Reason for my vote of 5
Nice Tricks! My vote of 5
GeneralReason for my vote of 4 I like the points.membersendtodilip24 Jan '11 - 18:43 
Reason for my vote of 4
I like the points.
General@Murrali...thanks for suggestion, I'll try to do it asap...membermahendra_moni17 Jan '11 - 23:09 
@Murrali...thanks for suggestion, I'll try to do it asap...
GeneralRe: Hi, correct the typo from the title. 'pactices' instead of '...mvpthatraja18 Jan '11 - 5:03 
Hi, correct the typo from the title. 'pactices' instead of 'practices'
General@Chris Hey....thanks for such comment... Here we see how you...membermahendra_moni17 Jan '11 - 22:59 
@Chris Hey....thanks for such comment...
Here we see how you can check for empty strings in the C# programming language and compare the performance of each approach.
Equality operator ==: 796 ms
string.Equals: 811 ms
string.IsNullOrEmpty: 312 ms
Length: 140 ms [fastest]
Instance Equals: 1077 ms
GeneralRe: but if string is null then the check on string.length will g...membersamrules7 Feb '11 - 17:32 
but if string is null then the check on string.length will give us null reference exception isn't it..so to avoid this I would check null for string before checking length, so we have two steps now.
 
i would rather go for string.isnullorempty in that case or the new introduced string.isnullorwhitespace
GeneralThanks for this post. But it will be great and useful if we ...memberMurrali from Madras17 Jan '11 - 22:40 
Thanks for this post.
But it will be great and useful if we know the reason / explanation for each suggessions.
 
So that we do not just know the best practices and we can also know why ? it is suggested.
General"To test for empty strings, check if String.Length is equal ...memberChris Hey17 Jan '11 - 22:01 
"To test for empty strings, check if String.Length is equal to zero. Constructs such as "".Equals(someString) and String.Empty.Equals(someString) are less efficient than testing the string length. Replace these with checks for someString.Length == 0."
 
Why would you not use string.IsNullOrEmpty?
GeneralReason for my vote of 2 Seems to be code review comments fro...memberBheemareddy Srinivas17 Jan '11 - 1:29 
Reason for my vote of 2
Seems to be code review comments from some project , anys good consolidation
GeneralReason for my vote of 5 thanks for sharingmemberPranay Rana17 Jan '11 - 0:41 
Reason for my vote of 5
thanks for sharing
GeneralThis is already posted in several other places. Do we really...subeditorIndivara13 Jan '11 - 21:50 
This is already posted in several other places. Do we really need another copy?
 
http://venkataspinterview.blogspot.com/2010/01/best-pactices-in-developing-aspnet.html
http://dotnetkeeda.blogspot.com/2010/12/aspnet-best-coding-practices.html
http://chinnisanti.blogspot.com/2010/12/best-pactices-in-developing-aspnet.html
http://chinnisanti.blogspot.com/

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 19 Jan 2011
Article Copyright 2011 by Mahendra Vishwakarma
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid