Being A Better Programmer

Tuesday, August 6, 2013

Iris and I put together “Being a Better Programmer” with tips and strategies for everything from writing code to time management. We’ll list our favorite books and tell you a bit about how we got started in the industry, as well as talk about how we learn the business domain and where to go to find some good code to read.

Learning To Program - Being A Better Programmer

One of the techniques we talk about for being a better programmer is writing code – lots of code. Sometimes the code is a hobby project you work on for personal fun, sometimes the code is a kata or a koan, and sometimes the code is something you deliberately implement to see how something works in isolation. I have many little projects checked into source control that only exist to understand how something works.

For example, the behavior of a List<T>. If you want to figure out the default capacity of a List<T>, as well as the algorithm it uses to increase capacity, then you can use a decompiler or create a small experiment project to test a List<T> under different scenarios. While the decompiler is a useful exercise on its own, a small project with code is something that can give you hours of enjoyment as you run the program under different framework versions and try different parameters.

Here’s one I wrote recently that provides some interesting results.

static void Main(string[] args)
{            
    var list = new List<int>();            
    var newCapacity = list.Capacity;
    var oldCapacity = newCapacity ^ Int32.MaxValue;

    while (true)
    {
        if (oldCapacity != newCapacity)
        {
            Console.WriteLine(newCapacity);
            oldCapacity = newCapacity;              
        }
        list.Add(1);
        newCapacity = list.Capacity;
    }
}

For those who watch, I hope you enjoy the course!


Comments
gravatar Chris Tuesday, August 6, 2013
So I checked out the code sample in LINQPad - very interesting. I'm straining my brain to determine purpose of the XOR though.... why not simply set oldCapacity to Int32.MaxValue? I'm missing something interesting, I think.
gravatar Jesse C. Slicer Tuesday, August 6, 2013
Replace the generic type parameter int with bool and consequently replace list.Add(1); with list.Add(false); and see the interesting difference (did this in 64-bit LINQPad).
gravatar Scott Tuesday, August 6, 2013
@Chris - the original idea was that I just wanted to make sure the number was different. Int32.MaxValue would be a good safe choice though, no List should start with that as a default capacity! @Jesse: cool :)
gravatar Smeagol Friday, August 9, 2013
But we all already know it doubles the capacity each time it increases because that gives better amortised time complexity?
gravatar Scott Friday, August 9, 2013
@Smeagol - Yes, I believe that's documented. But you can try to see how far it gets on 32bit and 64bit systems, for example.
gravatar Chris Friday, August 9, 2013
One of the techniques we talk about for being a better programmer is writing code – lots of code. I think it's very easy to fall into the trap of copy/pasting code and tweaking it until it works. This is a great way to learn almost nothing!
gravatar Greg Sunday, September 8, 2013
Chris, it depends. E.g. in the mechanical world, there are people who machine their own parts ... and then there are people who "just" assemble already extant parts. Some pretty useful things are made by people who "just" assemble parts.
Comments are closed.

My Pluralsight Courses

K.Scott Allen OdeToCode by K. Scott Allen
What JavaScript Developers Should Know About ECMAScript 2015
The Podcast!