Dot Net Coding Standards Part3
01:35
1.
Always
use multi layer (N-Tier) architecture.
2.
Never
access database from the UI pages. Always have a data layer class which
performs all the database related tasks. This will help you support or migrate
to another database back end easily.
3.
Use
try-catch in your data layer to catch all database exceptions. This exception
handler should record all exceptions from the database. The details recorded
should include the name of the command being executed, stored proc name,
parameters, connection string used etc. After recording the exception, it could
be re thrown so that another layer in the application can catch it and take
appropriate action.
4.
Separate
your application into multiple assemblies. Group all independent utility
classes into a separate class library. All your database related files can be
in another class library.
1.
Do not use session variables throughout the code. Use
session variables only within the classes and expose methods to access the
value stored in the session variables. A class can access the session using System.Web.HttpCOntext.Current.Session
2.
Do
not store large objects in session. Storing large objects in session may
consume lot of server memory depending on the number of users.
3.
Always
use style sheet to control the look and feel of the pages. Never specify font
name and font size in any of the pages. Use appropriate style class. This will
help you to change the UI of your application easily in future. Also, if you
like to support customizing the UI for each customer, it is just a matter of
developing another style sheet for them
Good
and meaningful comments make code more maintainable. However,
1.
Do
not write comments for every line of code and every variable declared.
2.
Use
// or /// for comments. Avoid
using /*
… */
3.
Write
comments wherever required. But good readable code will require very less
comments. If all variables and method names are meaningful, that would make the
code very readable and will not need many comments.
4.
Do
not write comments if the code is easily understandable without comment. The
drawback of having lot of comments is, if you change the code and forget to
change the comment, it will lead to more confusion.
5.
Fewer
lines of comments will make the code more elegant. But if the code is not
clean/readable and there are less comments, that is worse.
6.
If
you have to use some complex or weird logic for any reason, document it very
well with sufficient comments.
7.
If
you initialize a numeric variable to a special number other than 0, -1 etc,
document the reason for choosing that value.
8.
The
bottom line is, write clean, readable code such a way that it doesn't need any
comments to understand.
9.
Perform
spelling
check on comments and also make sure proper grammar and punctuation is used.
1.
Never
do a 'catch exception and do nothing'. If you hide an exception, you will never
know if the exception happened or not. Lot of
developers uses this handy method to ignore non significant errors. You should
always try to avoid exceptions by checking all the error conditions
programmatically. In any case, catching an exception and doing nothing is not
allowed. In the worst case, you should log the exception and proceed.
2.
In
case of exceptions, give a friendly message to the user, but log the actual
error with all possible details about the error, including the time it
occurred, method and class name etc.
3.
Always
catch only the specific exception, not generic exception.
Good:
void ReadFromFile ( string fileName )
{
try
{
// read from file.
}
catch (FileIOException ex)
{
// log error.
//
re-throw exception depending on your case.
throw;
}
}
Not
Good:
void ReadFromFile ( string
fileName )
{
try
{
// read from file.
}
catch (Exception ex)
{
// Catching general exception is bad...
we will never know whether
// it was a file error or some other
error.
// Here you are hiding an exception.
// In this case no one will ever know
that an exception happened.
return "";
}
}
4.
No
need to catch the general exception in all your methods. Leave it open and let
the application crash. This will help you find most of the errors during
development cycle. You can have an application level (thread level) error
handler where you can handle all general exceptions. In case of an 'unexpected
general error', this error handler should catch the exception and should log
the error in addition to giving a friendly message to the user before closing
the application, or allowing the user to 'ignore and proceed'.
5.
When
you re throw an exception, use the throw statement without specifying the original
exception. This way, the original call stack is preserved.
Good:
catch
{
// do
whatever you want to handle the exception
throw;
}
Not Good:
catch (Exception ex)
{
// do
whatever you want to handle the exception
throw ex;
}
6.
Do
not write try-catch in all your methods. Use it only if there is a possibility
that a specific exception may occur and it cannot be prevented by any other
means. For example, if you want to insert a record if it does not already
exists in database, you should try to select record using the key. Some
developers try to insert a record without checking if it already exists. If an
exception occurs, they will assume that the record already exists. This is
strictly not allowed. You should always explicitly check for errors rather than
waiting for exceptions to occur. On the other hand, you should always use
exception handlers while you communicate with external systems like network,
hardware devices etc. Such systems are subject to failure anytime and error
checking is not usually reliable. In those cases, you should use exception
handlers and try to recover from error.
7.
Do
not write very large try-catch blocks. If required, write separate try-catch
for each task you perform and enclose only the specific piece of code inside
the try-catch. This will help you find which piece of code generated the
exception and you can give specific error message to the user.
8. Write your own custom
exception classes if required in your application. Do not derive your custom
exceptions from the base class SystemException. Instead, inherit from
ApplicationException.
0 comments :