Archive for March, 2017
Most software projects I’ve worked on had at least one feature that needed to be dynamic, where certain aspects of the program wouldn’t be known until runtime. Sometimes, those aspects would be known as the application was running on the developer’s machine (data binding in WPF, for instance, is late bound), and other times, they’d only be known at the end-user’s machine (maybe based on the user’s selections, maybe based on the environment).
Before landing in the strongly-typed world of C#, I used to write software in FoxPro, which was a dynamic language. We used to store large blocks of code on tables in the database, and execute that code during run time after all the required pieces of the puzzle were known. That was not something easy to do in the early days of C#.
There are a number of ways to either add or change behavior of an application during runtime, and granted, doing things in C# exactly the same way we did in FoxPro was NOT the way to go! However, when dynamic features where introduced back in C# 4.0, I was glad to have it in my toolbox, and I believe I’ve found good use for it in several situations.
For this post, my main point is just that I think using the dynamic features in C# is fun. I’ll be writing up more posts to talk about some basic stuff around these features, and also giving some examples on how I’ve used it.
I feel miserable pretty much everytime I run into regions when going through C# code. Nine out of ten times, regions are the rug where developers sweep ugly code under.
Many, many years ago, I used to use regions like many people still do:
- to organize my class, grouping fields, properties, methods, etc;
- to group code according to its functionality (methods and properties related to save go here, those related to validate go there, etc.)
Eventually I finally learned that even that way of using regions was pretty bad. If a class is so big that in order to make it easier to read I need to group and hide things under regions, that means the class is doing too much. Also, if there are multiple groups of funcionality within the same class, that probably means each of those groups represent abstractions that belong somewhere else.
Now, what’s even more aggravating is using regions within method bodies. So you have code, code, code, region, code, code, region, code. I’ve seen methods that had switch-blocks where each case block had code hidden behind regions!!. If some code is within a region inside of a method body, than that code should probably be moved to a separate method!
Now, what do we make out of the example below?
The entire body of that method is hidden behind a region. What should expect to find when we expand that region? Just a few lines of the cleanest code possible, right…? Yeah, right!
If you’re using regions like that, please, take a step back and think how you can get rid of that nasty rug. 🙂
I have a personal dislike for code produced by copy-and-paste that forces me to read it over several times so I can spot what’s changed after the block was pasted. Take the code below as an example:
After a while we spot the differences:
There cases where spotting the difference isn’t so easy. Sometimes, it’s a simple “!” somewhere in the code that can be easily overlooked.
Anyway, a couple of posts ago I mentioned how I use dictionary and delegates to get rid of switch-blocks, as well as how I like using ForEach whenever possible. Well, we can clean things up a bit in the code above using the same approach, changing the code to make it look like this:
Much better, I think…
Whenever possible, I like using a ForEach method, instead of a for-each block. Take the code below for example:
The iteration part could be rewritten like so:
Or, even better, since the AddToPizza method takes in one parameter with the same type of the items we’re iterating over…
The iteration could simply look like this:
However, not every thing we iterate has the ForEach method. For example, regular arrays don’t. So, if our items source looked like the one below, we wouldn’t have a ForEach method:
So how do we get a ForEach method that works on Lists, Collections, Arrays, etc? Just create the extension method on IEnumerable<T>, like so:
Make sure to import the namespace for your extension methods (using statement) wherever you’re trying to use them!
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.