Meet your community

community

About a year ago I watched a video made by Scott Hanselman and Rob Conery entitled “Get Involved !”. The purpose of this video is to convince software developers to become active within their community. I really enjoyed this video and it made me want to participate more. This blog is the first example of the things I did toward this goal.

If you want to find more information about this subject and watch the video : Become a Social Developer !

There is a whole chapter related to user groups which allowed me to discover Meetup. This network helps you find user groups in your area in almost any domain you like. This way you can meet people who share common hobbies and interests. And if there is a subject you like without a related meetup in your neighborhood, nothing stop your from creating it !

I recently attended my first meetup and it was an extraordinary experience, I came across a lot of interesting people I could not have been able to meet otherwise. It was all about debating, discussing and sharing in a funny and respectful way. The subject of this meetup was Software Craftsmanship and you can find a summary I wrote on my company’s blog here.

I can only highly recommend Meetup to find communities you might want to socialize with.

See you next time !

Keep control of your entities

Today I want to share tips to improve the control you have over your code and especially over your entities in a Oriented Object language. The example will be in C# .NET.

During my (still short) career I’ve been  working on a certain amount of projects and I often found the same practices related to entities : they are just PODS !

Here’s an example with an Invoice, a Product and a Customer entities.

public class Product
{
    public UInt16 Id { get; set; }
    public string Name { get; set; }
}
 
public class Customer
{
    public string Name { get; set; }
}
 
public class Invoice
{
    public Product Product { get; set; }
    public Customer Customer { get; set; }
 
    public void DisplayInfo()
    {
        Console.WriteLine("{0} ordered :", Customer.Name);
        Console.WriteLine("{0} - {1}", Product.Id, Product.Name);
    }
}

Now, let’s use these classes in a program :

class Program
{
    static void Main(string[] args)
    {
        Invoice invoice = new Invoice();
        invoice.DisplayInfo();
    }
}

And yes, this program throws a NullReferenceException when displaying the invoice because my Invoice.Customer instance is NULL. Let’s fix our Invoice class the following way.

public class Invoice
{
    public Invoice()
    {
        Product = new Product();
        Customer = new Customer();
    }
 
    public Product Product { get; set; }
    public Customer Customer { get; set; }
 
    public void DisplayInfo()
    {
        Console.WriteLine("{0} ordered :", Customer.Name);
        Console.WriteLine("{0} - {1}", Product.Id, Product.Name);
    }
}

But this time the program class looks like this :

class Program
{
    static void Main(string[] args)
    {
        Invoice invoice = new Invoice();
        invoice.Customer = null;
        invoice.DisplayInfo();
    }
}

The NullReferenceException still occurs because the Customer is set to NULL. I know you wonder why I did this… Because the code structure allows me to do it ! It might look stupid this way but sometimes in a more complex application you can set an object’s property to NULL without knowing it, if you’re calling an external service for example or another provider.

Let’s change our Invoice class again to use a bit of encapsulation.

public class Invoice
{
    public Invoice(Product product, Customer customer)
    {
        Product = product;
        Customer = customer;
    }
 
    public Product Product { get; protected set; }
    public Customer Customer { get; protected set; }
 
    public void DisplayInfo()
    {
        Console.WriteLine("{0} ordered :", Customer.Name);
        Console.WriteLine("{0} - {1}", Product.Id, Product.Name);
    }
}

With this version the Customer and the Product have to be set when constructing the Invoice. So ? Do you think we’re done ? Let’s find out with a new program.

class Program
{
    static void Main(string[] args)
    {
        Invoice invoice = new Invoice(null, null);
        invoice.DisplayInfo();
    }
}

Still not running correctly, still the same exception for the exact same reason. You can add control to your entities properties but you should add control during initialization as well to avoid manipulating an invalid instance of your class.

This lead us to our final version of the Invoice class (at least in this post).

public class Invoice
{
    public Invoice(Product product, Customer customer)
    {
        if (product == null)
            throw new ArgumentNullException("product");
        if (customer == null)
            throw new ArgumentNullException("customer");
 
        Product = product;
        Customer = customer;
    }
 
    public Product Product { get; protected set; }
    public Customer Customer { get; protected set; }
 
    public void DisplayInfo()
    {
        Console.WriteLine("{0} ordered :", Customer.Name);
        Console.WriteLine("{0} - {1}", Product.Id, Product.Name);
    }
}

I choose to use the ArgumentNullException to prevent the construction of an invalid object. This allow me to have a complete control on my Invoice entity. With these kind of practices I try to prevent incorrect behavior from misuse. I transformed a PODS into an entity with “intelligence”.

I hope these tips will help you and do not hesitate to share yours and/or to comment about these tips.

See you next time for more software development related topics !