Exclusively for you!! Trial version of Google Apps, Apply Coupon-CNHPGX6T6QRWGV!!
Tip : Code Structure C#
Feb03

Tip : Code Structure C#

Welcome to CodeSpread! 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 searching for some particular items. Below is the sample structure and example: using statements Constants / Read-only fields Private Members Enumerations Public members Protected members Constructors Properties Public methods Private methods //using statements Using System Using System.Web namespace Controllers { public class SampleController {   //Constants / Read-only fields public const int numofitems=10; //Private Members private string _samplestring = string.Empty; //Enumerations public enum months { January = 1, February, March, April, May, June, July, August, September, October, November, December };   //Public members public  int StartDate =string.Empty;   //Protected members protected int EndDate ==string.Empty; //Constructors public SampleController () { }   //Properties public string Date { get; set; } //Public methods public string GetStartDate() { } //Private methods private int numOfItems() { }     Related articles across the web C# Coding Standards CLS Compliance?...

Read More
24/12/2013: 10 Best Practices of programming from CodeSpread(31-40)
Dec24

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 best practices from CodeSpread. Best Practice #31, Naming Conventions are important, Use Pascal Casing for class, interface, constants and methods names. For example: interface INewClass { void NewMethod(); } public class NewClass : INewClass { public const int NewConstant=10; public void NewMethod() { } } Best Practice #32,Use camel casing for local variable and parameters. public class NewClass { public int countEmployee; public void NewMethod(int customerId) { } } Best Practice #33, Do not use abbreviated language. For ex: do not use single alphabets as it does not convey any useful meaning. int i=1; int j=2; Best Practice #34, Always use descriptive language for naming. int count=10; void GetEmployeeCount(); Best Practice #35, Use #region to summate the block of codes together and have a clean separation. #region Get Customer Details public class Customer { public int customerCount; public void GetCustomerDetails(int customerId) { } } #endregion Best Practice #36, Always maintain updated comments. Use // or ///. //Get Employee Details Public Employee GetDetails(int employeeId) { } Best Practice #37, Follow Solid principles. Best Practice #38, Instead of using hardcoded values, use constants or read only properties. Best Practice #39, Always trim the reference values used for comparison. Best Practice #40,Use string.Empty instead of “”. We would love to hear from our users about the best practices used by them. Please write to...

Read More
18/12/2013: 10 Best Practices of programming from CodeSpread(21-30)
Dec18

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 practices from CodeSpread. Best Practice #21, A single huge EDMX file may hit the performance. So split the EDMX into multiple EDMXs. Best Practice #22, Perform minimum operations possible within one sql transaction Best Practice #23, Use more than one DataContext where each one can represent a single unit of work. This can remove the database objects that are not used frequently. Best Practice #24, SQL Query plans should be reused whenever possible as this can avoid unnecessary query compilation costs. Best Practice #25, Prefer DateTime.TryParse method over try/catch blocks. if (DateTime.TryParse(text, out result)) { // code } else { // throw error } Best Practice #26, While using select LINQ Query, return IQueryable and not a ‘var’ object. Best Practice #27, Do not use the unnecessary demo code which is available by default, while creating a new application. Best Practice #28, Place all the Scripts to the end of webpage. This will get downloaded once browser has downloaded all the other content and will help in progressive rendering of the page. Best Practice #29, ORDER BY,DISTINCT, TOP etc. operations may degrade query performance as they are performed only after the resultset is retrieved, so use them only when there is clear requirement. Best Practice #30, Use namespaces while defining contracts in WCF service. This will help with versioning and interaction of the application with other services. We would love to hear from our users about the best practices used by them. Please write to...

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

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

Read More
16/12/2013: 10 Best Practices of programming from CodeSpread
Dec16

16/12/2013: 10 Best Practices of programming from CodeSpread

We 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 instead of for loop wherever required.* Best Practice#2, To make your file compact, right click on namespaces and select remove unused strings. It will remove all the unused namespaces, which are not used in your code anytime. Best Practice#3, Create a separate config for connection strings and include it in web.config. Best Practice#4 , Dispose unmanaged objects explicitly for ex: COM Objects Best Practice #5, Create automatic properties and use either get or set, try to avoid both get and set together, if not required.* Best Practice #6, Never append your stored procedure with ‘sp_’ as it might be treated as a system stored procedure and will lose its scope. Best Practice #7, Please write each SQL verb on a new line as shown in the image.   Best Practice #8, During Exception Handling, Always use finally block to clear the objects while using try catch blocks. Best Practice #9, Streaming interfaces, like XmlTextReader, give better performance and scalability, compared to loading large XML documents into the XmlDocument or XPathDocument classes and then using DOM manipulation Best Practice #10, Keep EnableViewState Property as false for the page and enable it only for the required controls. It will minimize the page size. *Conditions...

Read More