Wednesday, 26 November 2014

The When and Why: Static

The static modifier, much like the access modifiers, is one of the first keywords that new developers learn and, much like the public access modifier, static is often misused by new developers because it provides a seemingly easy shortcut around learning how to write proper Object Oriented code.

Excessive use of the static modifier can lead to messy, overly complex code. On the other hand, careful use of the static modifier can lead to functionality that is elegant, scalable and performs well. The aim of this article is to help new developers strike the right balance. First I'll describe when to use static, then I'll explain why.

When?

Ideally, static should only be used for private or protected members when you need to share a single value between all instances of the class to which those members belong. You should avoid using public static wherever possible.

There are a couple of occasions where a public static is required, however.

Providing Common Values for structs

If you've ever used string.Empty or int.MinValue then you've invoked a public static property. Here's an example of using the same technique in a Point struct.

public struct Point
{
    public int X;
    public int Y;

    // Static property that returns an instance of point
    public static Point Zero 
    {
        return new Point { X = 0, Y = 0 };
    }
}

// This...
var point = Point.Zero; 

// ...is cleaner and more descriptive than writing this...
var point = new Point { X = 0, Y = 0 };

// ...every time we want a zero value point

Extension Methods

Extension methods are a way to add new functionality to classes that you don't have access to the code for. The design pattern for extension methods requires that you use the static modifier on a public method.

public static class StringExtensions
{
    public static string Left(this string str, int count)
    {
        return str.Length <= count 
               ? str 
               : str.SubString(0, count);
    }
}

// We use the extension method just like any normal method
var example = "Hello world".Left(5);

Why?

Over-use of the static modifier is an anti-pattern because it increases and actually encourages class coupling. Code that features classes that reference each other excessively is often referred to as spaghetti code because of how difficult it is to untangle and understand.

When all of your classes know about each other in this way they become less reusable and they also become harder to maintain. After all, it's much easier for you as a programmer to understand and reuse a class that only knows about itself than it is to understand one that accesses eight or nine other classes, each of which accesses another eight, and so on.

Further Reading

I encourage you to read up on all of the below topics as even a loose understanding in the back of your mind will have you start asking the right questions when writing code.

SOLID, Class Coupling, Cohesion, Interfaces, Factory Pattern, Dependency Injection

Understanding programming theory really is just as important as understanding syntax. If you have any questions at all about these subjects, feel free to drop me an e-mail or ask straight in the comments.

Tuesday, 25 November 2014

When Worlds Collide: IronPython

In the post A Brief Encounter with Python I mentioned that I quite like Python's syntax and script-like feel. I also make no secret of my passion for the .NET framework... So imagine my delight when I discovered an open-source fusion of the two!

With the syntax of Python and the vast wealth of utility found in the .NET framework, IronPython is an exciting prospect. C# still has the edge when it comes to cool language features (async/await, anyone?) but I can't deny that IronPython interests me from the perspective of combining something new with rapid application development.

Since you can try IronPython without having to download it there's nothing to lose, so why not have a go with it?

Sunday, 23 November 2014

The Problem With a Language That Grows

The Problem

There is no denying that C# is a very powerful language. A large contributor toward that power is that the C# language definition is regularly expanded and built upon to grow alongside the ever-changing landscape of information technology. Each iteration of the language has opened up new, powerful capabilities that either did not exist within the language prior to that release or that were only possible through the writing of many, many lines of brittle code.

The inherent problem with this kind of growth is that for programmers who didn't join the C# party early on, but rather are just arriving now, there are almost too many tools available for problem solving. Worse still, some of those tools are incredibly powerful and can be easily and accidentally abused when placed in the hands of a novice. This is an unfortunate side effect because those same tools in the hands of an experienced programmer make for compelling and powerful solutions.

An Example

One such modern language feature to which I am referring is the Type dynamic. As a C# advocate one of the features that I laud loudest is type safety. In C# if the Type you're referencing doesn't have a DoSomething() method then you can't write myObj.DoSomething(). Well you can... but it won't compile!

The Type dynamic removes that powerful precaution. It says "whatever you write, I'll trust you, but if I later try to run code that I can't then I'll crash". That's quite the deal with the Devil and should only be used by those who really know what they're signing up for and only when there is truly no other option.

In my entire career I've used the Type dynamic exactly once.

In Outsorcery when your serialized object of Type IWorkItem<T> gets to the server, unable to infer the Type T and not needing to know it because the resulting object of that Type will just be serialized and sent back to the client immediately, I use the Type dynamic to allow me to call the method DoWorkAsync() that I know exists but can't prove to the compiler without reams and reams of infrastructure code. It is an incredibly rare scenario and one that I have locked down to only a couple of lines of code, wrapped in a try/catch, in the lowest levels of my library.

More and more I've started to see novice developers diving straight onto using the Type dynamic in scenarios where other language features would give them both the type safety and the flexibility that are available within C#. I've seen people use dynamic when they should be using Interfaces, when they should be using Generics, when they should be using Type Casting and even in cases when they should just be using the Type object.

A Solution?

So the question inevitably becomes a take on the same question we see time and time again in sport, in online gaming and even in general life.

How to protect new C# developers from being overwhelmed,
without holding experienced C# developers back?

The solution that I propose is one that aims to not be overly invasive, but instead informative. Drawing on the experience of the world of online gaming, I suggest that when you write code that uses a heavy-handed feature for the first time, Visual Studio should present you with a tutorial window. Something like this:

dynamic myObj = otherObj.Operation();

Of course, veteran programmers will be able to disable this feature via the pop-up either on a per tutorial basis or as a global setting.

Having these tutorials built into the IDE shouldn't get in anybody's way, it just means that for the programmer who's just starting out, the relevant information is right next to the code they're writing. In effect, it'll be like pair programming between the novice and the collective knowledge of veteran C# developers.

What do you think?

Friday, 21 November 2014

The When and Why: For and Foreach

I've often seen it asked which type of loop out of for and foreach is more appropriate under a given set of circumstances. I'll explain when to use each type of loop, then I'll explain the why, much in the same way that I did for access modifiers.

Definitions

The keywords for and foreach have such broad applications that it can be a bit befuddling trying to understand what all the related terms mean. Below is a quick summary of the terms I'll be using.

  • Enumerable: An object that can be enumerated (it implements IEnumerable). Don't worry if that's too technical, just know that you can use foreach with any object that is enumerable (i.e. all arrays and collections).
  • Enumerate: To go through each item in an Enumerable in turn.
  • Index: An item's zero-indexed position in the collection/array. e.g. items[3] is the 4th item in the array named 'items'.

Note: Not all enumerables have an index, but anything with an index is enumerable.

When

Always start with foreach, you can swap to for later if the need arises. You can't add or remove items in a collection while enumerating it, but you can call methods and get/set properties on the item, which is the most common reason for enumerating.

foreach (var item in items)
{
    item.DoWork();
    item.Property = "Some other value";
}

Use for only when you need access to the current index and are working with an array or array-based collection (e.g. a list). One scenario that you might need this is when you need to remove items from a collection while going through it (something you can't easily achieve with foreach). Here's an example.

// go through the list backward so that removing 
// items doesn't cause us to skip the item that follows.
for (int i = myList.Count - 1; i >= 0; --i) 
{
    if (myList[i].ShouldBeRemovedFromList)
    {
        myList.RemoveAt(i);
    }
}

Why

There are many technical reasons for favouring foreach over for, but my main reason isn't technical at all. foreach is fewer lines of code, uses cleaner syntax and also expresses the intent of your code. That is, 'go through this collection without changing which items are in the collection'.

As luck would have it, foreach isn't just better looking, it often performs better too!

As mentioned in the definitions section, not all enumerables have an index, but all objects with an index can be enumerated. Similarly, arrays have length and some collections have Count, but some implementations of enumerable have neither. You'd be tempted to cheat and use the extension method Count(), but that'd be a mistake. Why? Let's take a look at a comparison.

// foreach on an enumerable
foreach (var item in items)
{
    if (item.GiveUp)
        break;
}

// for loop on enumerable
// Count() extension method is not the same as List<T>'s Count
for (int i = 0; i < items.Count(); ++i) 
{
    if (items[i].GiveUp)
        break;
}

You'd be forgiven for thinking that the two examples do the same thing and, from the perspective that they achieve the same result, they do. However, the second example is actually a performance issue waiting to happen.

Let's say that the enumerable items has 10,000 items in it and that it is the 5th item of those 10,000 that gives up.

  • First example: We enumerate the first five items, reach the item that indicates we should give up and exit the loop (by using the break keyword).
  • Second example: We do the same thing, except that the call to the extension method Count() actually goes through the whole enumerable and counts all the items before we start.

So while the foreach example makes 5 visits, the for example makes 10,005... That's a pretty massive difference!

This is just one example and there are many more where foreach is the smarter choice, but this article is quite long already, so if you have any specific scenarios you're not sure about then let me know in the comments and I'll add them to the article.

Further Reading

There are occasions when you'll want to run a loop until a particular condition is met, rather than running a loop for every item in an array or collection. When such an occasion arises, you'll be glad to know about the while statement.

If you're interested in the fine-grained difference between an array and a collection then check out this MSDN article on Arrays and Collections.

foreach and the yield keyword go hand in hand. It's the yield keyword that lets you enumerate a collection or array once, instead of once per method that does work on it.

I've saved the best for last, as once you have a handle on the basics of foreach and yield it's well worth getting to grips with using LINQ. It can be quite a big topic with lots of confusing and varied applications, but hopefully these two hints will help make things clearer.

  1. LINQ does different things depending on what you use it on. For example, using LINQ on an in memory list will run code against the list, using it on a LINQ to SQL table object will generate and execute SQL against the database!
  2. LINQ makes use of a concept known as Lazy Evaluation, which means that the aforementioned generation and code running doesn't happen until you actually enumerate the collection.
If you'd like to see a beginner's guide to LINQ, let me know in the comments, as I'd love to write one!

Thursday, 20 November 2014

C# 6.0 Hype - Null-Propagating Operator

I don't think that it's much of a secret that I am a total code nerd! No more so is this true than when it comes to efficient, readable, bug-free, well-structured object oriented goodness - especially in the oh-so elegant form of C#. With that in mind, there are many incredibly cool features coming in C# 6.0 that Sean Sexton has already done a brilliant job of blogging about in bitesize fashion here.

Since Sean really has done such a fantastic job already, I will only be delving into one feature in this post. If the title didn't give it away, it's the Null-propagating operator!

You'll probably be used to seeing question mark used in the following ways:

Well, in C# 6.0, you'll be seeing it used like this too:

Any programmer that has experienced a Null Reference Exception will know that a fair few lines of code go into the frequent act of null-checking your references before making member calls on them. Here's an example of calling ToString() on a child's child's child when any one of them could be Null. It's a contrived example, but there are a lot of real world scenarios that are just as bad.

public string ChildChildChildToString(Parent parent)
{
    if (parent == null)
        return null;

    if (parent.Child == null)
        return null;

    if (parent.Child.Child == null)
        return null;

    if (parent.Child.Child.Child == null)
        return null;

    return parent.Child.Child.Child.ToString();
}

Phew! That's 11 lines, without curly braces, just to say return null if this step is null. For completeness sake, here's the same example, this time using the already available conditional operator to try and thin it down a bit.

public string ChildChildChildToString(Parent parent)
{
    return parent == null ? null
           : (parent.Child == null ? null
              : (parent.Child.Child == null ? null
                 : (parent.Child.Child.Child == null ? null
                    : parent.Child.Child.Child.ToString())));
}

Argh, my fingers! And my eyes! Ok, so it may not be that bad, but there's a lot of repeated typing there just to check for null and those five lines of code are not exactly easy to read. Let's not forget either that we're having to do this absolutely everywhere that there's the potential for a reference to be null (which is pretty much everywhere anything remotely complex is going on).

And now, for the moment you've been waiting for. Here's the same example, but this time using the planned null-propagating operator.

public string ChildChildChildToString(Parent parent)
{
    return parent?.Child?.Child?.Child?.ToString();
}

Even just typing that made me want the feature right now! It's easy to read, quick to type, doesn't spam up my screen with null-checking and just plain makes sense. Love it.

Tuesday, 18 November 2014

A Brief Encounter with Python

Before C# I worked with C++ and before that I worked with VB6, I've also toyed with PHP and F# in the past but can't claim to have used them in any kind of detail. The one big lesson that I've learnt from these forays into other languages is that another language's perspective on problem solving can be so radically different to your current language of choice as to teach you useful lessons about programming in general.

With this in mind and fancying something a bit new, I thought it'd be cool to take a look at developing with Python. I wasn't wrong, Python is cool! I'll dive straight into my first impressions with a code comparison.

C#

/* This is a 
    multi-line comment */
// This is a single line comment
public class Example : BaseClass
{
    private readonly string _name = "unknown";

    // Constructor
    public Example(string name)
    {
        _name = name;
    }

    // Example method
    public string ExampleMethod()
    {
        var result = "hello ";
        resltu = result + _name; // Intentional typo
        return result;
    }
}

Python

""" This is a 
    multi-line comment """
# This is a single line comment
class Example(BaseClass):
    __name = 'unknown'

    # Constructor
    def __init__(self, name):
        self.__name = name

    # Example method
    def exampleMethod(self):
        result = 'hello '
        resltu = result + __name # Intentional typo
        return result;

There are some familiar elements here like classes, variables, methods, assigning values, returning values, constructors, comments, etc. However, the first thing that a C# programmer will likely notice when looking at the Python example is that there are no curly braces, so how does Python define scope?

Python defines scope by the indentation and because of this, indentation is non-optional. If you indent the class members incorrectly then you'll get an Indentation Error. I like this feature! We're all indenting our code anyway and fewer lines spent on managing scope means more lines of code on screen at any one time. Looking at the two samples, Python looks much cleaner to me.

Python also feels very quick to get up and running and working with the command line and text editor combo gives a nostalgic feeling of being closer to the code, though granted you can work this way with any language.

It's not all praise for Python, though. My first concern can be seen in the samples above. The C# sample won't compile, because resltu is a variable that hasn't been defined. Visual Studio will point you to the line and say "Did you mean to do this?" You can then declare the variable or correct your mistake. To Python, what we've said here is create a variable named result if it doesn't exist and assign "hello ". Now create another variable named resltu and assign it the value of result and our field __name. Now return the original variable result. In the sample, this typo is a bug, but Python will accept it.

My second concern so far is that there is no direct equivalent to C#'s access modifiers in Python. In the C# sample above, consumers of the class Example cannot access _name because it is marked as private. In fact, we can't modify it after construction either, because it's readonly, too. In Python, however, the most we can do is indicate to consumers of the class that the field __name is private by preceding it with a double underscore as we have done. Consumers of the class can still access it if they want via _Example__name.

As a developer in the C# headspace, this is alarming to me. The response from the Python community is that "we're all consenting adults here", but anybody who's worked on an Enterprise level application will know that not all programmers in a large team are of the same skill level and that this feature opens the door for potential, ill-considered misuse.

Over the coming months I will no doubt uncover a lot of Python's interesting gems and, when I do, I'll be blogging about those and their C# equivalents as well. If you happen to know of useful features available in Python that don't exist in .NET then I'd love to hear about them in the comments below. I'd also be really interested to hear how real my concerns are. If you've been working with Python at an Enterprise scale, do you run into these issues very often or is everyone better behaved because the reins are loosened?

Saturday, 15 November 2014

The When and Why: Access Modifiers

Today I'm going to talk about access modifiers. If you don't know what they are or how to use them, check out the first section of this MSDN topic.

Read it? Understood it enough to get a program to compile? Great, then lets get to it. I'll start with the when, then explain the why.

When?

Use private first for anything that other classes don't need to see. You can always make something protected or public later but, when you do, ask yourself if there's a way you can keep it private and expose the work you want to do with the private fields as a public method instead of exposing the fields directly.

Public should be used only when absolutely necessary. For example, if you find yourself writing something like this:

public class PersonDetails
{
    public int Age { get; set; }
    public DateTime Birthday { get; set; }
    public bool IsFemale { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime WeddingDate { get; set; }
}
 
static void Application_Main()
{
    var person = new PersonDetails();
    person.FirstName = "Jane";
    person.LastName = "Bloggs";
    person.IsFemale = true;
    person.Birthday = new DateTime(1984, 10, 24);
    person.Age = 
        (DateTime.Now - new DateTime(1984, 10, 24)).Years;
    
    if (justMarried)
    {
        if (person.IsFemale)
        {
            person.LastName = "Sanders";
        }

        person.WeddingDate = DateTime.Now;
    }

    Console.WriteLine(
            person.FirstName 
            + " " 
            + person.LastName 
            + " is " 
            + person.Age);
}

Then you should get in the habit of thinking "that's a lot of person-dot-somethings, this would be better off as private fields and a public method" and rearrange it to be a bit more like this:

public class PersonDetails
{
    public int Age 
    {
        get 
        { 
            return (DateTime.Now - _birthday).Years; 
        } 
    }

    public string FullName 
    { 
        get 
        { 
            return _firstName + " " + _lastName; 
        } 
    }

    private DateTime _birthday;
    private string _firstName;
    private string _lastName;
    private bool _isFemale;
    private DateTime _weddingDate;

    public PersonDetails(
                string firstName, 
                string lastName, 
                DateTime birthday, 
                bool isFemale)
    {
        _firstName = firstName;
        _lastName = lastName;
        _birthday = birthday;
        _isFemale = isFemale;
    }

    public void JustMarried(string newLastName)
    {
        if (_isFemale)
        {
            _lastName = newLastName;
        }

        _weddingDate = DateTime.Now;
    }
}
 
static void Application_Main()
{
    var person = new PersonDetails(
                        "Jane", 
                        "Bloggs", 
                        new DateTime(1984, 10, 24), 
                        true);

    if (justMarried)
    {
        person.JustMarried("Sanders");
    }

    Console.WriteLine(person.FullName + " is " person.Age);
}

You probably won't stop there, because once everything is sorted, it'll be easier to see other improvements you can make. This process of improvement is called refactoring and it is an important and iterative process - meaning you do it over and over. Your classes will end up looking a lot more descriptive, useful and less error prone if you get into this habit.

Use protected when you're inheriting from your class and need access in the child class to methods and properties that exist in the parent class but aren't public.

You probably won't need internal for now as it's mostly used in third party libraries to create classes that are public to their code, but hidden from yours.

Why?

Good Object Oriented Programming is all about encapsulation. Encapsulation just means that your objects (that's the classes and structs you write) are self-contained. A good object hides how it does something and only publicly exposes enough methods and properties to cover what it does.

Exposing the bare minimum functionality publicly to other classes promotes readability, it also promotes re-usability (the code is in a neat little package that doesn't rely on dozens of other classes) and it promotes scalability. It's also less likely to have errors in it because being all packaged nicely means it's easier for you to keep track of and - if you do make a mistake - it'll be easier to figure out what's going wrong, because there are fewer elements to your code that could be affecting a private field or property compared to a public one.

Let's take a side by side look at the public members of our example before and after we refactored it.

BeforeAfter
Age (get / set)Age (get only)
Birthday (get / set)FullName (get only)
IsFemale (get / set)JustMarried()
FirstName (get / set)
LastName (get / set)
WeddingDate (get / set)

As you can see, the list on the right is a lot shorter and tells us how we're expected to use the class without having to dig around inside of it or find out how everything else is using it. In big projects, well designed classes that let you understand the what without the how become the difference between it taking 10 minutes to change something and 10 hours. You'll come to find that well structured code also helps you feel more confident that your change isn't secretly breaking something somewhere else!

Further Reading

If this topic has you chomping at the bit for more, then you should check out the readonly keyword. In the same way that private protects from public misuse, readonly protects from after-construction misuse.

You should also definitely check out Interfaces. Interfaces don't have any implementation (implementation is what we call the how in programming). Their purpose is to define the contract (the what) that a class must fulfill publicly in order to be considered an implementation of that interface. If you'd like to see a W&W article on interfaces, let me know in the comments!

Friday, 14 November 2014

The When and Why: An Introduction

Welcome to the first post in my When and Why series!

Its my opinion that when learning to code there's a huge emphasis in the resources available on the how of programming. They'll start by describing general ideas that you'll be tackling, then they'll list out and describe syntax you'll be using and what that syntax means to .NET and then they usually end with a small working example demonstrating the functionality of the syntax. To my mind, knowing syntax on its own is a lot like knowing words without understanding good grammar. Programming is as much theoretical as it is practical and taking fifteen minutes to understand the contextual implications of the syntax you've just learned will, inevitably, save you hours and even days in the future because you won't spend all that time trying to use the right tool for the wrong job.

As you probably have already guessed, my When and Why series is an attempt to start filling that void. In each W&W post I'll start with a link to a how resource and then I'll go into explaining when we use that feature and also why we chose to use that feature for those scenarios.

And remember, as with all things programming, the When and Why are not meant as absolutes, they are meant as a guide until you feel confident enough to decide for yourself what the most appropriate tool for a job is.

Sunday, 9 November 2014

And so it begins...

Hi there and welcome to my blog!

I'm not sure how often I'll be posting here just yet but, when I do, I'll be mostly talking about C# development. Not just language features, but well patterned usage of those features, too.

It seems to me that so many sources of information about programming, especially those targeted at beginners, completely omit one of the most important lessons, how to structure the code to be easy to manage and to scale well. That just seems crazy to me! Well-written code is far more than just syntax and just because you can write something a certain way doesn't mean you shouldwrite it that way. So that's why, when I do come to blog about something, I won't just be telling you what it is but how it fits into the modern world of software design.

As with all blogs, these will just be my opinion, so I invite you to come and open a healthy debate if you disagree!

Until next time,

Steve L