I hate looking at dense code. You know, that kind of code that has blocks that look like bricks left randomly around the room, making it very easy for anyone to walk around the place.

Serioulsy, dense code is hard to read and understand. Let’s take this little bit of code as an example:

 

When I run into code that looks like that, this is sort of what I see:

It’s so easy to put that garbage out, like this:

The same happens with collections…

I like this much better:

I always favor using object/collection initializers whenever possible, if that makes the code more compact and easier, more calm to read.

4 responses to “Fun with C#: Object and Collection initializers”

  1. Ant. Rui Nogueira Avatar
    Ant. Rui Nogueira

    Good explanation, about collection.

  2. […] of characters in code and I try to get rid of it whenever possible, as I mentioned in this other post. The keyword private in C# is another one that bugs me. Take the following […]

  3. […] I’ve mentioned before (here, here, and here, at least), I’m very specific as to how much stuff I want to see on my screen as I’m working […]

  4. […] Fun with C#: Object and Collection initializers […]

Leave a Reply to Toggling Visual Studio CodeLens On/Off | Claudio Lassala's BlogCancel reply

Trending

Discover more from Claudio Lassala's Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading