I am a .NET developer and this is my world

Microsoft .NET

“What do you do ?”

This simple question can be a real pain to answer for people like me. It is not because I’m shy or anything like this. It’s because I work in a complex domain, software development, and it can be really hard to explain especially to non technical people. Yet some of them were/are interested by the topic. And this is why I’m creating this blog post. I want to give them an overview of what I do without being too technical.

The short answer to the previous question is : “I am a .NET developer”. A what ?! First of all, I am a developer and thus I create software/programs for computers and to do so I use a technology called .NET (pronounced “dotnet”).

Software development

As a developer/programmer I spend a lot of my time writing code. This code is used to do specifics actions on a computer in an automated way, this is what we called a program or a software. People use software everyday and in a lot of things. A video game is a software, a website is a software, the web browser you use to visit these websites is also a software. Where there are electronic components it is likely that there are also some programs written by developers. The code of these programs are written using programming languages and there are a lot of them and I mostly use the one called CSharp (C#) which leads me to the next topic.

Microsoft .NET

.NET is a framework developed by Microsoft. A framewhat ?! You can check the Wikipedia page to have a better understanding (or not) of a software framework but let’s say that is the kind of toolbox that help a programmer to build software.

So is C# a framework ? No, C# and .NET are two different things. C# is a programming language which is compatible with the .NET framework, it means that C# can use the functionalities present in .NET. It is possible to use .NET with a different language than C# (a list of compatible languages can be found here), for example I made an introduction for F# in my last article which can work with .NET. It is also possible to use C# without .NET even if it is not recommended since this language has been created for the framework. The blog post you are currently reading can be an analogy to this concept : I used several tools (~ a framework) to help me write it and I chose to use english (language) to express my thoughts, I could have done the same in french with the same tools to express the same thoughts.

So what can people do with the .NET framework and a compatible language ?

There is a part of the framework called ASP .NET that allows developers to build websites and other web-based functionnalities. I use the features provided by this part of the framework in my everyday job, especially the MVC extension and a few others.

I also use a feature called WCF (Windows Communication Foundation) in order to create services that provide information across the Internet. For example the Facebook application on a smartphone will use these kind of services to retrieve your personal informations and the updates related to your account.

The .NET framework offers features to develop applications (on a computer or on a smartphone) with WPF (Windows Presentation Foundation). Back in school I used this technology to build a media player like the one available in Windows.

There are also some tools available with .NET to ease the development process. The .NET developer best friend is Visual Studio, this software allows a programmer to write code in C# (or in another language) and to test it, debug it and organize it. We also use a tool called TFS (Team Foundation Server) which provides team management functionnalities. TFS also provides source control management that allow a team to create backups of its work.

I won’t go into the details but the .NET framework also provides functionnalities and tools for video game creation (on PC, Xbox, Kinect), for cloud management (Azure) and much more. Microsoft is behind this framework so it can be used with almost every product this company has to offer. This is what I like about this technology : it’s wide and offers a lot of interesting features.

I hope this will help you have a better understanding of my profession and passion. Feel free to comment if some parts are still confusing or too technical.

See you next time !

FSharp : Welcome into the Functional Programming

FSharp logo
FSharp logo

I am a CSharp (C#) developer for several years now and before working with this programming language I made a few projects in C++ : I am an Object Oriented Programmer (OOP). But OOP is not the only way to develop software and sometimes it is not the easiest way to create a specific functionality.

As a software journeyman I want to sharpen my skills and learn new ones. Given that I am familiar with the OOP paradigm it could be interesting to learn a new programming paradigm. So I decided to look toward Functional Programming with FSharp (F#).

Why FSharp ?

Short answer : pressure from my peers !

Joke aside, I was hesitating between Python and FSharp to learn functional programming. Some of my colleagues advise me to pick FSharp and I also had the chance to participate to a BBL (Brown Bag Lunch) on FSharp (summary of this event here). But this is not the only reason I chose this language.

FSharp is not only a functional language, it is also possible to do OOP with it : it is a multi-paradigm language. FSharp has been designed by Microsoft Research thus it can be used with the .NET framework which is perfect for a C# developer like me. And of course it can be used inside Visual Studio.

What does it look like ?

Enough with the talking ! Let’s see some code !

For this first FSharp example, I resolved a simple problem of the Project Euler in C# and F#. This problem is called : “Multiples of 3 and 5”.

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.

Solution in C# (one of many) :

using System;
 
namespace CSharp.Euler
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("The result is : {0}", Multiple());
        }
 
        static int Multiple()
        {
            var result = 0;
            for (int i = 0; i <= 999; i++)
            {
                if (i % 3 == 0 || i % 5 == 0)
                    result += i;
            }
            return result;
        }
    }
}

Solution in F# (one of many) :

let multiple =
    [0..999]
    |> List.filter (fun x -> x % 3 = 0 || x % 5 = 0)
    |> List.sum

printfn "The result is : %d" multiple

The purpose of this example is not to compare these two languages, it is to show a bit of the FSharp syntax.

If you want to learn more or even try FSharp, I highly recommend visiting www.tryfsharp.org to check their well-crafted and interactive tutorials.

I’m just starting learning FSharp and for now I really like it and I wanted to share this enjoyment with a quick presentation of this multi-paradigm functional focused programming language. I think that FSharp will come back in future articles shortly.

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 !