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.
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.
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.