17/12/2013: 10 Best Practices of programming from CodeSpread(11-20)

bestpractice
bestpractice

17/12/2013: 10 Best Practices of programming from CodeSpread(11-20)

We love programming and having a knowledge of best practices always helps in writing beautiful code. We have shared first 10 random best practice in our previous article here, now we are sharing few more best practices.

Best Practice #11, Use StringBuilder instead of String, as any concat operation on String will create unnecessary successive string objects. In case of StringBuilder, any append operation will mutate the same object and will not create any successive objects.

Best Practice #12, Use Integrated Mode in IIS 7.0 and above. This will avoid two separate application pipelines to handle native and managed components

Best Practice #13, Use app_Offline.htm file, it is a user friendly way of keeping the application offline while doing some modification to the application. It also forces an application restart.

Best Practice #14, Indexed views can increase performance of certain common queries without having to make temporary tables.

Best Practice #15, Create a Custom Error page to show user friendly page while handling exceptions. The redirection logic can be written at the Page_Error event or the Application_Error event in Global.asax. Or use the below logic of web.config.

error-config

error-config

Best Practice #16, Catch specific Exceptions in a try catch block. Create multiple catch block in a try-catch, and each catch block can be used to handle a specific exception type.  Last Catch should be base Exception.

Best Practice #17, Use Try Catch Block in SQL Server to catch exceptions. Refer the screenshot.

try-catch

try-catch

Best Practice #18, Use Regex class, if validation is required for controls other than server controls.

Best Practice #19, While using Linq queries to retrieve a single item from a data collection, go with SingleOrDefault() instead of Single() to provide a fail-safe option.

Best Practice #20, Avoid ‘using’ block in WCF and replace it with try/catch block. ‘using’ block can mask the faulted state of Service. In catch block, use close() or abort().

Although , all points above are debatable and it should be like this way, as only discussion can help to evolve best practices every time.

One of our friend and fellow writer Rajaraman R have shared one of the best practice used by them,

“I would also go for using capitalized letters in SQL Statements. That is a good practice.”

Please refer comment section of our last article.

Author: hershey

A passion for knowledge drives me to do programming, A passion for programming drives me to create something different, A passion for creation drives me to spread the knowledge.

Share This Post On

0 Comments

  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

More from CodeSpread:

  • 18/12/2013: 10 Best Practices of programming from CodeSpread(21-30)18/12/2013: 10 Best Practices of programming from CodeSpread(21-30)We love programming and having a knowledge of best practices always helps in writing beautiful code. We have shared first 20 random best practice in our previous articles 1-10, 11-20. More best pra...
  • 16/12/2013: 10 Best Practices of programming from CodeSpread16/12/2013: 10 Best Practices of programming from CodeSpreadWe love programming and having a knowledge of best practices always helps in writing beautiful code. Here, we have few best practices shared by professionals. Best Practice#1, Use foreach loop i...
  • 24/12/2013: 10 Best Practices of programming from CodeSpread(31-40)24/12/2013: 10 Best Practices of programming from CodeSpread(31-40)We love programming and having a knowledge of best practices always helps in writing beautiful code. We have shared first 20 random best practice in our previous articles 1-10, 11-20, 21-30. More b...
  • Tip : Code Structure C#Tip : Code Structure C#As a developer, I always stress on developing a habit of following the best practices to structure the code. It helps me to easily navigate through the code and also saves a lot of effort in search...
  • Concepts: S O L I DConcepts: S O L I DSOLID - Object oriented programming principles. May be its easy to forget these principles  but for creating a reusable code or maintainable code, people will always have to come back and check ...
  • Agile Encounters UnpredictabilityAgile Encounters UnpredictabilityWhat is Agile? Wikipedia says “Agile software development is a group of software development methods based on iterative and incremental development, where requirements and solutions evolve through...
  • C# Code to send a MailC# Code to send a MailWhile working in a project, we always neglect the importance of the code which we have written many times and now end up in copy pasting the code from old projects. Lately, I saw a request in fo...
  • SQL:My first SSIS PackageSQL:My first SSIS PackageI would like to share my experience about a situation, where I was given a task to export a file(say XML) from FTP location and get the data from this file populated into our database tables. Moreo...