1. Recursive traversing of Pocos

    Tossed together some recursive reflection on dtos/models/pocos, that I thought I could share

    Enables for all kinds of magic behaviour by adding custom attributes that can be looked out for and acted upon.

    One example of implementation is bulk translation of ViewModels.

    It currently supports the following IEnumerable types:
    Dictionary<ValueType, CustomDto>
    List<CustomDto>

    Code and readme can be found on github: https://github.com/Dashue/Recursive-Reflections

    Have fun

  2. A few caveats when using Strings in .Net

    Strings in .Net are class objects and unlike value types they are stored on the heap.

    Garbage collection occurs when one of the following conditions is true:

    • The system has low physical memory.

    • The memory that is used by allocated objects on the managed heap surpasses an acceptable threshold. This means that a threshold of acceptable memory usage has been exceeded on the managed heap. This threshold is continuously adjusted as the process runs.

    • The GC.Collect method is called. In almost all cases, you do not have to call this method, because the garbage collector runs continuously. This method is primarily used for unique situations and testing.

    Due to the second condition creating a string object can trigger garbage collection, which is costly in terms of performance. As developers we want to keep the string creation to a minimum. Since strings in .Net are immutable (cannot be edited) means that combining two strings “string1” + “string2” results in the creation of three strings and editing a string results in creating a new one with the modification.
    I think it’s good to have the knowledge about how things work and why even though it’s considered an micro optimization.

  3. Why you’d want to place a shared library in its own solution

    A situation occurred on work where we decided to refactor duplicated funtionality from two project (A and B) into a new shared library (X) DRYing things up. During this process the question of where to put X came up. I quickly answered “It’s own solution” whereupon my colleague countered with the very simple yet valid question “Why” and I answered ”Because” thinking it was really obvious and at the time couldn’t understand why he’d even ask.

    After work I realized this was a childish and not a very helpful answer caused by me just knowing it was a good thing to do and have never reflected upon the reason for it. So I decided to gather my thoughts and write this blog giving a better answer to his valid question.

    Presumptions

    We will use internal Nuget feed for the library.

    X could be placed in either A or B or separate library.

    Consuming the Library

    Placing it in its own solution forces all projects consume it the same way, otherwise some projects would use nuget and one could use project reference. 

    Future Changes

    If placed in A it would be easy to change according to requirement or quick fix for A and not think about B or any future consumers. Placing it in its own solution forces it to behave more like an independent library and less like a project reference. It makes more sense to open the solution for the library when change is needed than to open an unrelated solution containing the library.

    Unrelated Functionality

    Since the functionality is general and not related to neither A nor B, placing it in the same solution just because it’s consumed by them is not a valid argument.

    Versioning

    The library has different release/deploy cycles than the consumers and using Nuget allows for choosing what version and if/when to upgrade. Different consumers can easily consume different versions of the library. 

    YAGNI

    YAGNI was mentioned as an argument against placing X in its own library. I think developers are to fast to throw abbreviations as arguments thinking they are valid without actually considering the situation at hand, but this is material for a future post.

  4. PhotoUtil v1.0.0.1 aka Speeding up a c# project

    I was investigating why my hobby project “PhotoUtil” (can be found here: http://johanilsson.com/blog/projects/photoutil/) was being slow. Following is a short recap of that procedure.

    Find out what’s taking time 1: Analyze / Launch Performance Wizard

    The first thing that popped out at me was a DateTime creation which took 100% of the CPU time (according to the performance report). I grab the DateTimeOriginal value from exif metadata of a photo and convert it into datetime, only because it allowed for cleaner code when accessing the date and time values later on in the code. Apparently parsing strings into datetime is slow, so instead I got it to work with some string manipulation.

    Find out what’s taking time 2: Analyze / Launch Performance Wizard

    When reading exif data using GDI+ from a file the whole image is read into memory using new Bitmap(Path); then the desired properties are fetched using GetProperty. This made me switch focus to increasing concurrent files being worked on (read parallelization) instead of minimizing process time of each file. With this in mind I rewrote my foreach loop into using the parallel library.

    from foreach(var filepath in filepaths){ 
    into  Parallel.ForEach(filePaths, oldFilePath => {

    Taking on step further:

    After finding out that the whole image is read into memory I set out to find an alternate approach of extracting EXIF data from a photo. It wasn’t long before I found a sweet project called ExifLib which only reads the Exif data portion of an image. ExifLib is a wonderful lib created by Simon McKenzie only available as source code from codeproject.com but I’m hoping to convince him to put it up on NuGet so more people can find and make use of it.

    Results

    100 images processed on an Intel Core 2 duo

    Before optimizations: 00:00:20.8059121 

    After optimizations:   00:00:11:4034511

    Using ExifLib:            00:00:01.0714248

    References:

    ExifLib - http://www.codeproject.com/Articles/36342/ExifLib-A-Fast-Exif-Data-Extractor-for-NET-2-0

  5. Code that is hard to be covered by tests is bug-prone and need to be redesigned to be easily coverable by easy tests.

    Writing a complete test suite naturally leads to good design, enforcing low coupling and high cohesion.

    — http://codebetter.com/patricksmacchia/2012/01/10/non-trivial-and-real-world-feedbacks-on-writing-unit-tests

  6. Review TFS 11: The things I look forward to

    With TFS 11 around the corner (with in-place upgrade from 2010), having been rewritten with the promise of a faster and smarter experience I felt like compiling a list of the features I look forward to the most. Ordered by pain factor solving desc.

    Automerge content changes (finally)
    Finally TFS has come to it’s senses and allows for automerge of non-conflicting content changes. 

    My work pane and Search work items
    No need for custom work item queries, work related to you will be easily available in the work item pane. Searching for items is also available here.

    Easier to transition between work items states
    No more open work item and changing state to in progress, just right-click item in my work pane and click start.

    Suspending
    Easier task switching by using suspend/resume. Saves visual studio state, open files and position in currently open file. Suspended tasks are listed in you work pane.

    New Pending changes page
    Shows tree of pending changes. Allows or leaving files in excluded state without having to exclude changes every check-in (good for personalized web.config etc.).

    New built-in diff tool
    TFS team has finally gotten around to fix the built in diff tool, which now supports more view modes and reflects live changes. Which means diff will update if you make changes to your version when showing base/yours. The merge tool has also gotten some love.

    Local workspaces
    Removes offline mode, and the time out method require to go offline. Doesn’t handle modified files by reading the read-only bit on the file (Took them until 2012 to fix it). File system watcher watch for change made outside of VS, and lists them nicely in the pending changes pane.

    Shelving
    Shelveset merge (with current and baseless) and searching of shelvesets.

    Code Review workflow
    Request code-review workflow on suspended changeset or checked in changeset as well as code reviewing functionality with the ability to comment on specific changes and even alter the changeset.

    Rollback changesets

    Async operations
    Modality and blocking operations has now been removed, no more  waiting on visual studio to complete retrieving things (search items etc.).

    New Builds page
    Shows builds associated to me (started by me etc.). Extensible

    Summary:

    I really like the new way that TFS 11 shows data; putting the concept of “My work” in focus, showing my work items and my builds etc. This combined with the new functionalities provided, working in TFS 11 will be much more smother and faster than it’s predecessors and I am really looking forward until it’s released and part of development environment.

    For more info:
    Developer collaboration with Team Foundation Server 11
    Build 2011 Day 4 TOOL-811T
    Speakers: Jamie Cool 
    http://channel9.msdn.com/Events/BUILD/BUILD2011/TOOL-811T

  7. Enumerable.Zip

    I just stumbled upon Enumerable.Zip which is a newly added Linq operator since .NET Framework 4.

    Signature is as follows:

    public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> func);

    Much like a zipper it “zips” together two IEnumerables applying a specified function over each pair of elements.

    I’ve seen examples which print the greater number from two list of ints, which constructs addresses from four lists of city/street/number/flatnumber and one that prints name and age from name/age lists.

    I have as of yet found a good real world example, and am incapable of thinking one up. Anyone out there who have made good use of this added functionality?

  8. Improving blog Part 1: Installing Wordpress

    So I’m in the process of moving my blog from tumblr to my own hosting environment. I installed php, mysql and wordpress on my readynas and everything went smooth, until I tried to browse the blog. The php file was downloaded instead of parsed. After some digging around I found out that the php module wasn’t being loaded by apache and after a quick edit of the conf file.

    LoadModule php5_module /path/to/apache/modules/libphp5.so

    A new error was presented to me:

    Your server is running PHP version 5.2.0-8+etch16 but WordPress 3.3.1 requires at least 5.2.4.

    This is due to the readynas being locked to debian etch version for stability. To resolve this I had to uninstall the libapache2-mod-php5 and php5-mysql that I had apt-get’d (using apt-get remove —purge package) and install a community released php addon containing a higher version. After installing the addon I was presented with yet another error:

    Your PHP installation appears to be missing the MySQL extension which is required by WordPress.

    This is caused by a quirk in the addon, install it again did the trick.

    So now I have a running instance of wordpress on my readynas, and am off to do some configuration.

    The next part will contain information about importing old tumblr articles into wordpress. Check back!

  9. My take on Ref

    A good friend of mine asked me to share my thoughts on the ref keyword so here goes.

    To give it some context: To me a good method is a function taking a variable amount of parameters, making some processing and returning the result. Furthermore the method should do one thing and one thing only and never change input parameter values.

    Now the ref keyword allow developers to stray from this “ideal” method by allowing it to change the values of the input parameters. Obfuscating the self-documentation of the method and by this diffusing what the method does and what it’s responsibility is. This creates a required understanding by your fellow colleagues about the intricates of the method, and in turn requires every developer to know how every method in the system is implemented.

    The most common reason for using ref parameters is simply that your method is doing to much, rendering you unable to return a single value/collection to account for everything that is being done, ending up with sending and object as input parameter and changing a lot of values. This may be improved by returning part of the data as an out parameter and part of it as the return value if possible.

    The reason why I think that using out is better than ref is because out doesn’t obfuscate the api and the intention of the method. Using Ref conveys that “This method might(?) change values of the parameter or even the parameter itself” while Out conveys that “This method will change the parameter”.

    Consider this test example:
    Assert.AreEqual(4, Square(2));

    To:
    float a = 2;
    Square(a);
    Assert.AreEqual(4, a);

      To sum up with one of my favourite quotes regarding ref and out

      You rarely need to use Out, and you need a really good reason for using Ref

    1. Learning steps for design patterns, from dnrtv 194

      Learning steps for design patterns, from dnrtv 194