Search This Blog

Friday, April 1, 2016

Code cleanup and tests

Code is somewhat odd, it should stay as is, it's digital afterward, right? However it seems like a fruit, it rotten over time. With that I mean that if your don't spend time on your code it will slowly but constantly degrade, specially if you continue to add features on it.

The only parade to this is to continue to cleanup your code, improve things, and have the courage to refactor / change pieces even if that was always working.

The drawback is that the more you work on a code the more bugs you may introduce, which means you will for sure introduce bugs while doing so. The cure for it is automated tests, to ensure that at least a good chunk of your code base still work after the face lift you did.

After the Entity Framework conversion, I wrote a small piece of code which would check that at least all my DB tables can be accessed. It doesn't mean the data is correct but it means the ORM doesn't have troubles accessing them (for example wrong table name).

using (var ctx = new IV4.Backend.Model.InventoryConnection())
{
    foreach (var prop in ctx.GetType().GetProperties().Where(p => typeof(IQueryable).IsAssignableFrom(p.GetGetMethod().ReturnType)))
    {
        IQueryable table = (IQueryable)prop.GetValue(ctx, null);
        var rowNb = 0;
        // Read the first 10 rows (just to see if we get data)        foreach (var row in table)
        {
            rowNb++;
            if (rowNb >= 10)
                break;
        }
    }
}
My next step is re-organize the way the code is stored, instead of a directory "template" and a directory "typescript" I will try to put all the things together in "modules", with template, LESS files and typescript all in one directory. The advantage is that when you need to edit one feature you will find all the files inside a single directory instead of browsing 3 different directories.

For the end user again this has no impact (in the best case), but for the developer like me it saves time, which means I can develop faster what the customer want.

No comments:

Post a Comment