Best practices in developing ASP.NET applications…

來源:互聯網
上載者:User
  • Remove unused private fields and functions.
  • Do not cast unnecessarily. Avoid duplicate casts where possible, since there is a cost associated with them.
  • Properties that return arrays are prone to code inefficiencies. Consider using a collection or making this a method.
  • 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 stringlength. Replace these with checks for someString.Length == 0.
  • 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.
  • Use stringbuilder instead of string types for string manipulation. 
  • Use String.Format instead of concatenating and appending strings.
  • Use Type.TryParse rather than Convert.ToDestinationType(). For example, use int.TryParse() rather than Convert.ToInt32() which might throw an exception.
  • Override Equals() method wherever applicable in your classes.
  • 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 ofFileStream as a parameter when only calling Stream.Read(), this makes the method work on all kind of streams instead of just File streams.
  • 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.
  • Use properties instead of visible instance fields.
  • Follow the same naming conventions accross the solution. 
  • Remove unwanted commented code, indent code properly.
  • Use curly braces with in an if statement, even if there is a single statement in the if block. This will provide better readability.
  • Make sure to refactor your code to move the duplicated code to common reusable functions.
  • Move one time control settings into the .aspx page rather than having them in the code behind in if(!IsPostback)block.
  • Use inheritance wherever possible, which enables code reuse and also reduces the amount of code we have to write and test.
  • Move the reusable JavaScript functions to an external .js file instead of having them on the page.
  • 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.
  • Make sure to check for nulls when using any type retrieved from a session, querystring or a database to avoidNullReferenceExceptions.
  • Use foreach loop instead of using for loop which may lead to out of boundary run time exceptions.
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.