Code from your fellow developers

It's almost a law: whenever you take over code from another developer, you start to moan and complain about the state of that code. It's awful, terrible, the worst piece of code you've ever seen. I am no exception. However, when I took over some code from a colleague who left the company I found this gem. I think I am right in complaining about this piece of code. (FYI this is almost an exact copy of what I found in the codebase, except for it being an console application and the classnames…)

namespace ConsoleApplication9

{

    class Program

    {

        static void Main(string[] args)

        {

            Customer customer = new Customer();

            customer.Id = 42;

 

            Order order = new Order();

            order.Id = 64;

 

            customer.Save(order);

        }

    }

 

    public class SaveableEntity

    {

        public int Id { getset; }

 

        public void Save(SaveableEntity entity)

        {

            entity.Id = Id;

            SaveService.Save(entity);

            this.Id = entity.Id;

        }

    }

    public class Customer : SaveableEntity

    {

        //...

    }

 

    public class Order : SaveableEntity

    {

        //...

    }

}

 

Now, please tell me what you think of this…

Print | posted @ Monday, September 29, 2014 3:14 PM

Comments on this entry:

Gravatar # re: Code from your fellow developers
by Willem Meints at 9/29/2014 3:22 PM

There's a bug in that code. Why assign the ID of a child entity to the ID property of the current entity?

To be more specific: Why assign the ID of the order as the ID of the customer? And why save the order through the customer?

I don't think the relation is customer owns many orders. Orders are most likely something you want to handle as a root entity in the application.

I could go on, but this smells ;)
Gravatar # re: Code from your fellow developers
by bageg at 9/30/2014 8:53 AM

Why assign the ID of the order as the ID of the customer? And why save the order through the customer?
obat panas dalam untuk ibu hamil
Post A Comment
Title:
Name:
Email:
Comment:
Verification: