Tiny C# clean-up improves understanding of the code

I’m starting to collect small fragments from everyday life to understand and increase understanding and readability of code I’m coming across.
First piece in this category is gone which I came across while contributing to one of the open source modules.

Original fragment:

{
    IEnumerable btypes = GetBlockTypes();
    List blockproperties = new List();
    int x = 0;

    foreach (BlockType type in btypes)
    {
        x = 0;

        foreach (PropertyDefinition def in type.PropertyDefinitions)
        {
            x++;
            blockproperties.Add(new TypePropertyResultItem {
                TypeName = x == 1 ? type.Name : “”,
                PropertyName = def.Name,
                Description = def.TranslateDescription(),
                DisplayName = def.TranslateDisplayName()
            });
        }
    }

    return blockproperties;
}

Modified code fragment:

{
    var types = GetBlockTypes();
    var blockproperties = new List();

    foreach (var type in types)
    {
        blockproperties.AddRange(type.PropertyDefinitions.Select((def, i) =>
            new TypePropertyResultItem
            {
                TypeName = i == 0 ? type.Name : string.Empty,
                PropertyName = def.Name,
                Description = def.TranslateDescription(),
                DisplayName = def.TranslateDisplayName()
            }));
    }

    return blockproperties;
}



Happy reading not your code! :)

[eof]

Wałdis Iljuczonok

Software architect, lead technologist and Visual Studio ALM & TFS evangelist, technical fellow. Focus on solution research, new technologies analysis and enterprise designing.

http://blog.tech-fellow.net
riga.lv

comments powered by Disqus