Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Refactoring First Steps

2.00/5 (1 vote)
13 Jun 2011CPOL2 min read 6K  
Refactoring is to improve the design of a code without altering the product.

Refactoring is to improve the design of a code without altering the product.

I started with a short definition because I suspect that you, like me before, are having trouble understanding that specific term or to implement it easily in your schedule. Refactoring can happen whenever and wherever it suits you. You can think of ways to improve your code design or it's readability while you are taking a shower or eating lunch.

Here are some small steps for mankind, but some huge leaps for your code:

I will start with the best thing a development team can do for its code readability: start a development blog for your company.

I've created this Form.cs code:

C#
private void Form1_Load(object sender, EventArgs e)
{
    List<Robot> robots = (List<Robot>)Session["list"];

    foreach (Robot robot in robots)
    {
        if (robot.awake)
        {
            MessageBox.Show("my name is: " + robot.Name + "and I love: " + robot.Hobby);
            robot.eat();
            robot.sleep();
        }
        else
        {
            MessageBox.Show(robot.Name + " is asleep");
            robot.wakeup();
            robot.eat();
        }
    }
}

This is a small piece of code that can have many improvements in code design and in code readability. The point of your R&D blog is to share knowledge and accepted design issues with your fellow developers.

Refactoring Example 1:

Parameters naming.

The smallest and easiest thing that can change your code readability. In this case, I placed an object in the HttpContext session and called it "list". This can create a whole variety of problems. Overriding your object is one, and not understanding what it's for is next.

Therefore, naming it listRobots could be more efficient.

Refactoring Example 2

Let’s say Session["list"] is null.

Sure, I can write this line:

C#
List<Robot> robots = Session["listRobots"] == null ? 
            null : (List<Robot>)Session["listRobots"];

Refactoring Example 3

But what if the object inside that session isn't a list of robots? In this case, an exception will occur. Obviously, I can create a try-catch block, but a much more efficient solution will be this:

C#
List<Robot> robots = Session["listRobots"] == null ? 
            null : Session["listRobots"] as List<Robots>;

So our code looks like this now:

C#
private void Form1_Load(object sender, EventArgs e)
{
    List<Robot> robots = Session["listRobots"] == null ? 
                null : Session["listRobots"] as List<Robots>;

    foreach (Robot robot in robots)
    {
        if (robot.awake)
        {
            MessageBox.Show("my name is: " + robot.Name + "and I love: " + robot.Hobby);
            robot.eat();
            robot.sleep();
        }
        else
        {
            MessageBox.Show(robot.Name + " is asleep");
            robot.wakeup();
            robot.eat();
        }
    }
}

But still so many improvements are left...

Refactoring Example 4

Instead of MessageBox.Show(...), you can create a static method inside the Robot object. I would call it robot.Talk("...").

I would also place the whole string inside a string.Format method so it will be easier to understand.

Now we have this as the code:

C#
private void Form1_Load(object sender, EventArgs e)
{
    List<Robot> robots = Session["listRobots"] == null ? 
                null : Session["listRobots"] as List<Robots>;

    foreach (Robot robot in robots)
    {
        if (robot.awake)
        {
            robot.Talk(
                string.Format(
                                "My name is: {0} and I love: {1}", 
                                robot.Name, 
                                robot.Hobby)
                                );
                                            
            if (robot.isHungry())
            {
                robot.eat();
            }
            robot.sleep();
        }
        else
        {
            robot.Talk(
                string.Format(
                                "{0} is asleep",
                                robot.Name)
                                );
                                            
            robot.wakeup();
            if (robot.isHungry())
            {
                robot.eat();
            }
        }
    }
}

Refactoring Example 5

You can also take out blocks of code and make them a method or several methods.

So, in the end, I have this code:

C#
private void Form1_Load(object sender, EventArgs e)
{
   List<Robot> robots = Session["listRobots"] == null ? null : 
                             Session["listRobots"] as List<Robots>;
   foreach (Robot robot in robots)
   {
      if (robot.awake)
            TalkEatSleep(robot);
      else
            AnnounceWakeupEat(robot);
   }
}

There are a lot of refactoring ideas, methods, and patterns; the main idea is to keep your mind open for changes and improvements and to understand that a minute you spend on refactoring now is you saving someone else's 10 minutes trying to understand your code.

Good luck!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)