Monday, July 26, 2010

The Basic of Unit Testing

Do you test your program before releasing? Probably your answer is yes. I have never met a developer who doesn't test his/her code. But, this testing is usually individual and it's not repeatable. Just filling a form in user interface, click submit button and see the result. Sometimes, there are many cases which are bad and good you should test. While good case is usually one, bad cases can be more than one such as not filling required fields, invalid numeric value or date value you enter. You can test all the cases once and you say, it's done and test is approved.

Consider this, you need to implement a new feature into the current application. You should test all the cases again "manually, individually". At this time, you may forget one bad case.

Needles to say, there are a few problems with your testing;
  • Takes time to test all cases
  • It's manually tested
  • It's not repeatable
  • It's not reusable
  • Nobody know what you test
In order to test your application you should be familiar unit testing approaches. For instance what is "unit" in test term? Let's explain some terms you need to understand clearly.

Unit : A unit is a piece of code of your application which is testable. 
Unit Test: A unit test is a method which invokes another piece of code and test to determine whether they are correct according to some assumptions.
Integration Testing : Testing one or more than one modules as a group. It occurs after unit testing.
Refactoring : You will face this term if you use unit testing. Refactoring means changing code without changing its functionality. If you separate any method into smaller methods or change existing method name you did refactoring. The main aim of refactoring is to make the code become easier to maintain, read, change and test.

In order to do successful unit testing you should take into account these rules : 
  • Your unit testing should be easy to implement
  • Your unit testing should run quickly. When you start your unit testing it should be finish as soon as possible
  • Anyone in your team can run unit testing you write is able to run
  • It should be reusable
  • You should design your test code to be run automatically
  • When you write your test code, it should remain for future use
Actually, before writing unit test, your software application should be designed having regard to test driven development. In order to write good unit testing, you should know software design principles which led you to create useful unit testing. Beside this, you need to know some design patterns such as dependency injection pattern or  factory design pattern.

Friday, July 9, 2010

Software Documentation

Documentation is not only writing stuff, but it's managing information.

Before starting documentation advices, please read the sentence written red above. Documentation is the most ignored stage of software development. Most of people think that "writing documentation" is waste of time, they say, "i know nobody will read it". Sometimes, they tend to find excuses not to write documentation and sometimes it's understandable reasons not to write documentation. For the successful documentation, i made a list you should put into practice :

  • Documentation begins as soon as software development project starts : Requirements of software application is the first documentation paper. If you start to write documents after having completed software applications, you are on wrong way.
  • Documentation never ends. It's a ongoing process until the project is stopped or canceled : As i mention above, documentation begins with requirements and continue with maintenance information even if all software is completed. As long as you implement new feature, remove current feature and change the current business logic, you should keep sharing information.
  • Documentation is not a writing stuff, it's a sharing knowledge across the whole team. : It's the motto of documentation. While writing documents, you should always think this information you write for everybody not only for you. It should consist of useful information.
  • Documentation cannot be assigned to only one person, it's task in which everybody in the team involves. : Documentation is team work. There is no one who knows all information of the products. People in the team has different skills such as database administrator, network administrator or software developer. Each one should be responsible for own field.
  • Documentation consists of sensitive data. Therefore, It has to be protected and some permission level has to be created. : You expose all your information and knowledge to everybody in your team. But some information which has sensitive data shouldn't be reachable. You should create permission groups and assigned them to people in your team.
  • Information in documentation should be easily reachable and findable by everyone in the team if they have enough permission to see it : Main idea of documentation is sharing information. You can write some documents but if nobody can find and read them, they will be thrash. You should take into account to create a documentation environment where everybody know how to reach information. 
  • Content of information should be separated. Some documents include advanced technical information and this is not understandable for people in business level. If information is not useful, it's nothing and has no value. So, you should make categorization before starting to write.
  • Some rules have to be applied in order that information keeps clean, tidy, useful : Rules always required for each project otherwise it would be chaos. People in the team should arrive at a consensus about rules of documentation. But rules shouldn't be many, otherwise it's really difficult to compose documentation. People can be easily tired of it. If there is anyone who has an experience on documentation, he/she can lead the documentation process.
  • All people in the team should control and give feedback each other what they write : This is useful to increase quality of documentation and avoid making mistakes.
  • Don't forget to backup your documents : It's always a risk to lose your documents. To prevent undesired problem, you should backup all records daily to external hard disk or another PC in your network. 
Until now, i haven't talked about any documentation tool. Because tool which you use to carry out documentation comes later. First of all you should think about the advices written above then choose the tool which helps you to put into action.

Speaking about tool, i can recommend web based tool called ScrewTurn wiki. ScrewTurn wiki is a opensource application built by .NET. The reason to choose this tool apart from advises above is convenient to our system which is Windows based. If you use Linux environment you can find and select any wiki tools by searching on internet.

As summary, all people in your team has to understand why documentation is needed and what is the main idea of documentation. Remember this : Documentation is not only writing stuff, but it's managing information.

Thursday, June 24, 2010

Safely Type Casting in .NET-CSharp

In software programming sometimes safely type casting is ignored. This causes unavoidable exception in run-time. I explain how you can cast object to another custom defined type "safely".

Let's take bad example to analyse :
In this example, i defined two different classes which are Student and User.

   1:  public class User
   2:  {
   3:      public int Id { get; set; }
   4:      public string FirstName { get; set; }
   5:      public string LastName { get; set; }
   6:  }
   7:   
   8:  public class Student
   9:  {
  10:      public int Id { get; set; }
  11:      public string Name { get; set; }
  12:      public string Grade { get; set; }
  13:  }

Here, you can find the Foo() method that i try to obj parameter which it's type is Object.

   1:  static void Foo(object obj)
   2:  {
   3:       User user = (User)obj;
   4:       Console.WriteLine(user.FirstName);
   5:  }
In this Foo() method, there are two mistakes :
  1. After casting user variables needs to be checked if it's null or not.
  2. obj parameter can be different type from User. In this example we have two different types of class Student and User. In Foo() method, User type is expected as a parameter. 
If you send Student parameter to Foo() method instead of User type, System.InvalidCastException is thrown.

Let's see the best example of Foo() method :

   1:  static void Foo(object obj)
   2:  {
   3:      User user = obj as User;
   4:      if(user != null)
   5:          Console.WriteLine(user.FirstName);
   6:  }

In this Foo() method written above, as operator is used to cast the obj parameter to User type to prevent invalid cast exception. Although the parameter's object type is not compatible with the type that we want to cast, it returns null. For instance, if student is passed to Foo() method, user variable in Foo() method will be null. It's suggested to use as operator then check whether the variable (here it's user) is null.

For testing Foo() methods you can use Main method written below.

   1:  static void Main(string[] args)
   2:  {
   3:      User user = new User();
   4:      user.Id = 1;
   5:      user.FirstName = "Serkan";
   6:      user.LastName = "Karaarslan";
   7:   
   8:      // If you send user parameter to Foo() method, 
   9:      // even if it's in bad example
  10:      // it is casted correctly.
  11:      Foo(user);
  12:   
  13:      // if you send Student parameter to 
  14:      // Foo method in bad example
  15:      // you get unable to cast object 
  16:      // (System.InvalidCastException)
  17:      student.Id = 2;
  18:      student.Name = "Serkan";
  19:      student.Grade = "B+";
  20:   
  21:      Foo(student);
  22:   
  23:      Foo(null);
  24:   
  25:      Console.Read();
  26:  }

Thursday, April 29, 2010

How to Find the Serial Number of Bios in Windows

Before, i mentioned the subject about how to find bios serial number of windows using c# in this entry.

Here, i will write another solution of finding bios serial number of your pc in windows operating system by using command prompt. In order to check the serial number, follow the instructions written below :

  1. Open Start menu, click Run
  2. Type "wmic". (wmic stands for; Windows Management Instrumentation Command-line)
  3. Type "bios get serialnumber"
  4. Bingo! You got it. 
Here is the screenshot of my command prompt's output.

Monday, April 19, 2010

Ping a Hostname with C#

With C# you can detect a remote server whether is accessible using Ping class in .NET. Send and SendAsync methods are used to send Internet Control Message Protocol (ICMP) request message to a remote server and wait reply message from that server. For details of ICMP you can read on IETF web site, http://datatracker.ietf.org/doc/rfc792/. The internet protocol is not designed to be absolutely reliable. I mean, there is no guarantee that response from server is returned. Usually, Success or Timeout messages are sent as a response from remote server.

To ping an host name, System.Net.NetworkInformation namespace has to be imported in your class application.
   1:  static void Main(string[] args)
   2:  {
   3:       List list = new List();
   4:       list.Add("www.technolatte.net");
   5:       list.Add("www.amazon.com");
   6:       list.Add("www.thereisnosuchdomain.com");
   7:       PingIt(list);  
   8:       Console.ReadKey();
   9:  }
After creating Ping object, you should call Send method with host name and timeout parameters, which is set to 100ms in this example, to determine whether you can reach a computer across the network. Beside that, Send method has 8 different overload methods.

Send method returns PingReply object which contains Status property which its type is IPStatus. It's easy to read Status message to retrieve whether sending ping is successful. If you want to know detailed status enumeration properties, check this link.

As i mentioned above, there is no guarantee that you receive response from remote server because of firewall settings of remote server. If you run this code excerpt you can see www.amazon.com returns Timeout status while www.technolatte.net returns success.

For more information check msdn library, http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx

Tuesday, April 6, 2010

Host Header Name to Host Multiple Web Sites

For some reasons, you may want to host more than one web sites on same IIS using one public ip address. In order to do this, you can use different port numbers to distinguish web sites in IIS.  As you know, as default, port 80 is used to host web sites in IIS. But servicing your web sites from different ports, it's not handy to use port number with domain name like www.technolatte.net:85. You can avoid port number by using Host Header Name

Before configuring this you should aware that your computer or network must be using a name resolution system, such as DNS to use Host Header Name.

Case  : 
You have IIS installed on your Web Server.
Web Server's public IP address is : 212.212.212.212
You have 2 Web Sites on the IIS. Web Site A, Web Site B.
You have two different domain name, www.websiteA.com, www.websiteB.com
DNS was configured and 212.212.212.212 was pointed to your web server.

When user types www.websiteA.com on any browser, Web Site A on IIS should receive request and send response.
When user types www.websiteB.com on any browser, Web Site B on IIS should receive request and send response.

First, you need to point your web sites on IIS to same ports.
Let's say :
Web Site A is pointed to port number 80
Web Site B is pointed to port number 80

Below, you can find steps how to add host header name for a web site already made;

Solution :
  • Open your IIS
  • Right click on web site you already made
  • Click Properties.
  • On Web Site Identification field, click Advanced button.
  • On this form, click Edit button and check the following screenshot about the case written above.

Host Header Name for Web Site A

Host Header Name for Web Site B



Friday, February 26, 2010

How to Check DNS with CSharp?

If you want to know any domain name is matched with certain ip address, you can use System.Net namespace to retrieve IPAddresses collection which is related to domain name. Please check the code snippet written below :
   1:  public static bool CheckDNS(string domain)
   2:  {
   3:      bool result = false;
   4:      try
   5:      {
   6:         IPAddress[] addressList = Dns.GetHostAddresses(domain);
   7:   
   8:         result = addressList.length > 0 ? true : false;
   9:      }
  10:      catch
  11:      {
  12:          result = false;
  13:      }
  14:  }
If  CheckDNS method returns true, it means domain name is related to at least one ip address. If return false, the domain name is not associated to any ip address.

For testing you can pass an existed domain name such as www.technolatte.net and pass a domain name which doesn't exist.

CheckDNS("www.technolatte.net"); //output is true
CheckDNS("www.thisistestdomainname.com"); //output is false