Wednesday, 15 January 2014

How your web browser requests and receives a resource from a Web Server?

Your Web browser, in fact any web client, goes through the following cycle when it communicates with a Web server:

  1. Obtain an IP address from the IP/domain name of the site (the site URL without the leading 'http://'). This lookup (conversion of IP name to IP address) is provided by domain name servers (DNSs).
  2. Open an IP socket connection to that IP address.
  3. Write an HTTP data stream through that socket.
  4. Receive an HTTP data stream back from the Web server in response. This data stream contains status codes whose values are determined by the HTTP protocol. Parse this data stream for status codes and other useful information.

Click here to read more about HTTP status codes

Tuesday, 17 December 2013

Variable scope in ASPX page

When you declare a variable in a Web Forms .aspx file, you’re actually declaring a local variable inside an auto-generated rendering method. ASP.NET generates separate rendering methods for all tags marked runat="server". So for example if you have a variable declared in <head> with runat=server its scope is limited to the <head>.

Read more>>

Tuesday, 3 December 2013

Dealing with Bugs in Test Driven Development(TDD)

Although defect rates can be lower when using TDD, you will still create bugs. These should be corrected with the same three-phase iterations, Red, Green and Refactor. First you create one or more tests that demonstrate the bug by validating the correct behaviour. These tests should fail. Next you fix the code to make the test pass, performing regression testing by executing all of the tests. Once the bug is fixed, you should perform any appropriate refactoring to keep the code clean and readable.

Related articles

What is TDD?

TDD Process

Dealing with bugs in TDD

Test Driven Development (TDD) Process

The TDD process uses very short iterations, each adding a tiny amount of functionality to your software. Each iteration has three phases, described by the TDD mantra, "Red, Green, Refactor". The three phases are:

  • Red. The colour red refers to the colour shown in many automated test runners when a test fails. The idea is that before you write any production code you should write a failing test for that functionality. This often means writing test code that exercises classes or methods that have yet to be created. It is very important that every new test fails. New tests that pass immediately suggest that either the code they test is already present or that the test itself is defective. New tests should be small, aiming to test only a single aspect of the software being developed.
  • Green. The second phase of each iteration is writing code to make the new test pass. The smallest amount of code possible to make the test pass, without causing any existing tests to fail, is added to the project. This may mean creating dummy return values. For example, when creating a method that adds two values, the first test may check that adding two and three returns five. To make this test pass the method could return the fixed value of five, rather than performing an addition. Later tests will ensure that the method works correctly in all situations.
  • Refactor. A very important, though often overlooked, phase is refactoring. After creating a new test and making it pass you should consider the design of the code. Sometimes no refactoring is required. Other times you might change method or property names, or extract methods for a cleaner design. In some cases you may perform major refactoring of the code for an improved design. This is made easier because the existing tests can be run to ensure no bugs are introduced during the changes.

On completion of an iteration you simply start the process again. Usually an iteration is over in minutes, so most of the time you have working, if incomplete, code.

Related articles

What is TDD?

TDD Process

Dealing with bugs in TDD

What is Test Driven Development (TDD)?

  • Test-driven development (TDD) is an iterative development process.
  • The process relies on writing unit tests before creating the code that they validate.
  • The TDD process uses very short iterations, each adding a tiny amount of functionality to your software.
  • Each iteration has three phases, described by the TDD mantra, "Red, Green, Refactor".
  • TDD assists in creating high quality, well designed, loosely coupled and maintainable code that can be refactored with confidence.

Some development practices and methodologies lead to code that grows in complexity over time, with a corresponding decrease in maintainability. You may find that some projects are hard to modify, as adding new features might break existing functionality or cause subtle bugs that are difficult to rectify without introducing further defects. When you encounter such projects it may be difficult to incorporate small changes. Large refactoring operations can be near impossible.

If a project includes a high level of unit test coverage, meaning that most or all of the source code has tests to validate their functionality, it is likely to be easier to maintain. The key reason for this is that, in order to perform high quality testing, the code must be loosely coupled and is more likely to have a better design. In addition, you can confidently make changes to code that has a full set of tests, knowing that those tests will fail if you introduce errors.

Test-driven development (TDD) is an iterative development process that aims to achieve high standards of design, excellent testability of source code and high test coverage for your projects. As the name suggests, the process is driven by the creation of automated unit tests.

Related articles

What is TDD?

TDD Process

Dealing with bugs in TDD

Thursday, 28 November 2013

Accessibililty of "Protected Internal" members of a class

When a class member is declared as Protected Internal it means Protected OR Internal NOT Protected and Internal.

The behaviour of the internal access modifier for class members can be changed by using protected internal. Methods and properties marked as protected internal are hidden from other assemblies except where a class is derived from the class in question. For derived classes in other assemblies, internal methods are still hidden but protected internal methods are visible.

What is the use of a private constructor?

A private constructor is a special instance constructor. It is generally used in classes that contain static members only. If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class.

Private constructors are used


When you have a class that should only be created through factory methods. 

When you have overloads of the constructor, and some of them should only be used by the other constructors. 

When you want to prevent the users of your class from instantiating the class directly, Singletons for example

When you want to use it from a static factory method inside the same class

When you want to prevent the runtime from adding an empty constructor automatically.
  public class Complex
    {
        public double real;
        public double imaginary;
 
        public static Complex FromCartesianFactory(double real, double imaginary)
        {
            return new Complex(real, imaginary);
        }
 
        public static Complex FromPolarFactory(double modulus, double angle)
        {
            return new Complex(modulus * Math.Cos(angle), modulus * Math.Sin(angle));
        }
 
        private Complex(double real, double imaginary)
        {
            this.real = real;
            this.imaginary = imaginary;
        }
    }
 
Complex product = Complex.FromPolarFactory(1, Math.PI);
What is the need of private constructor in C#?