Jump to content
  • Advertisement
  • entries
  • comments
  • views

Refactoring, Part 4

Sign in to follow this  


Alright, so far most of our refactorings have been done to simplify the code, or to remove duplication. However these next two refactorings will target some of our customer's requirements.

The first refactoring we'll do involves the Protocol enumeration. We will be replacing this enumeration with a type code. We wish to do this for the simple reason that by having the Protocol as an object it will be able to have methods, and these methods will be able to encapsulate functionality that belongs with the protocol.

public class Protocol {
public static readonly Protocol Tcp = new Protocol();
public static readonly Protocol Udp = new Protocol();
public static readonly Protocol Ip = new Protocol();
The first step in the refactoring is to replace the enumeration with an actual type (as you can see on the right). However, if you run your tests, assuming you have one that tests the output of the AccessListEntry's ToString() method, you will find that they fail. They fail because this object no longer returns the expected strings for the different instances. We can fix this by adding a private field to store the name, and adding a constructor that takes a string. We then adjust the static instances to call this constructor. Running our tests we see that they pass. The next step will be to add support for ports. First we add a second parameter to our constructor, a boolean argument which specifies if the protocol supports ports, we then add a field to store the passed value. Finally we add a property (I called it SupportsPorts) to the Protocol class which exposes the private field.

public class Protocol {
public Protocol(string name, bool supportsPorts) {
this.name = name;
this.supportsPorts = supportsPorts;

public override string ToString() {
return name;

public bool SupportsPorts { get { return supportsPorts; } }
At this point we should have something very much like the code on the class on the right (although I removed the static instances, and the private fields). The next thing we should do is to refactor the constructor to change the if (protocol == Protocol.Ip) line to now be if (!protocol.SupportsPorts). We also change the exception no longer say "IP does not support ports.", instead having it give the name of the protocol in place of IP. Next we refactor that bit of code, including the port range checking out to another method called CheckPortValidity. We now can apply a move method refactoring to the CheckPortValidity method, and move it to the Protocol class. We then replace the call in the constructor with a call to the method in the protocol.

I would also suggest refactoring the ComparePort method to make sure that the port is within the valid range of ports for the protocol. You can do this by having the protocol class expose it's minimum port and maximum port values and comparing against these in the greater than and less than tests.
Sign in to follow this  


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!