Archive for February 2011

20 Software Development Best Practices




Below are a compilation of 20 software development best practices:

  1. Always use source control system even if the project has only one developer. By doing that, you don't lose some or whole code immediately, can share same source file by multiple person and can take the whole advantage of coding histories. 
  2. Follow coding standards and check that standard with automized tools.
  3. Be consistent. If you do operations in a specific way, do that kind of operations in the same way (e.g. defining variable/method/class names, paranthesis usage etc.).
  4. More code does not mean better code. Keep it simple and reduce complexity.
  5. Don't use magic numbers and strings directly in the code. Use constants. This method provides more modularity and understandability.
  6. Don't use comment lines to delete code, just delete. Version controling system will help you if deleted code is required.
  7. Delete unused methods and classes. Version controling system will help you if deleted code is required.
  8. Catch specific exceptions instead of highest level class 'Exception'. This will provide understandability and more performance.
  9. Use understandable and long names for variables. Loop variable names can be i, j, k, index etc., local variable names must be longer than loop variables, parameter names must be longer than local variables and static variable names must be longer than parameters;  proportional with scope size.
  10. Package related classes (that changed together and/or used together) together.
  11. Use understandable comments. Bad comment is worse than no comment.
  12. Use positive conditionals. Readability of positive conditionals are better than negative ones.
  13. Use dependency injection to manage too many singletons.
  14. Use exceptions only for catching exceptions, not for control flow. Think as required and perform control flow with control statements/conditionals.
  15. Don't use so many arguments with methods. Keep the number at most 8-10. If more is required, review your design.
  16. Don't use method alternatives with boolean flag variables (public void someMethod(bool flag)). Write more than one method for each flag condition.  
  17. Method names must include "what is done by this method" information.
  18. Think twice before defining a method as static and be sure if you really need to. Static methods are harder to manage.
  19. Avoid using methods with reference parameters. Use multi attributed object parameters instead.
  20. Number of interface methods must be minimized to decrease coupling/dependency.

Posted in | 50 Comments

A Brief Explanation of HttpModule and HttpHandler




An HTTP request from client to server can be explained as a chain of IIS, AspNET_ISAPI, Asp.NET Work Processor, HttpModules and HttpHandlers. The last part of the chain which is HttpHandler, creates the HTML and sends the result to HttpModule. AspNet_IASPI.dll is used for Asp.NET pages. And chain completes in reverse order this time, as shown below:

Http Request Lifecycle


HttpModules and Usage:

We can use several predefined methods of the Global.asax file to perform actions on start/end of the application, on start/end of the request etc., and an HttpModule  do the same thing with more modularity. So, an HttpModule can be thought as a modular alternative of Global.asax file and its usage is recommended.

For creating an HttpModule, you should implement IHttpModule interface, and then Init and Dispose methods. We can use HttpApplication object:

using System;
using System.Web;
namespace CodeBalance.Web
{
    public class ExampleHttpModule : IHttpModule
    {
        public void Init(HttpApplication app)
        {
            app.BeginRequest += new EventHandler(newBeginRequest);
        }
        public void Dispose()
        {
            // dispose operations, if necessary
        }
        void newBeginRequest(object sender, EventArgs e)
        {
            HttpApplication context = (HttpApplication)sender;
            string Url = context.Context.Request.RawUrl;
            if (!Url.Contains("SystemMonitoring.aspx"))
            {
                // show error page
            }
        }
    }
}

You can easily add/remove HttpModule via web.config file:


HttpHandlers and Usage:

HttpHandlers define actions for different type of requested files. They also can perform actions even if that file does not exist. An HTML page can perform all of the operations of an HttpHandler. But an HttpHandler has better performance and has more modularity than pages.

For creating an HttpHandler, you should implement IHttpHandler interface and then IsReusable and ProcessRequest methods. We can use HttpContext's Request and Response objects:

using System;
using System.Web;
namespace CodeBalance.Web
{
    public class ExampleHttpHandler : IHttpHandler
    {
        public void IsReusable(HttpApplication app)
        {
            // readonly.determines if this handler can be used in another requests
        }
        public void ProcessRequest(HttpContext context)
        {
            context.Response.Write("Hello World!");
        }
    }
}

You can easily add/remove HttpHandler via web.config file:

Posted in | 7 Comments