Andrew Theken's C# Blog

A public place for internal thoughts.

Happy Birthday, Zach

Posted by Andrew Theken on July 10, 2010

This is just a bit of a nostalgic rambling about one of my life-long friends and how we get to where we are.

As is probably apparent if you’ve read any of my blog posts. I’ve been fascinated with personal computers for a large part of my life. I’ve had an obsession with them and their power to change my life…

Starting with this:

This is a story of how the Apple IIe changed my life.

One of my best friends, Zach, and I used to out-nerd each other in the computer lab of our grade school. Zach’s parents were newspaper folks. So they (and naturally, Zach) lived, breathed, and loved their Macs. I was solidly on the I-B-M side of the line.

The computer lab consisted of about 25 Apple IIe machines, a Mac Powerbook, and an IBM PC. Zach and I were the only two kids that were big enough dorks to be allowed to touch the PowerBook or the PC – which sucked in middle school, but has paid off handsomely in our “old age.”

So, that was the state of things in 1992, I continued on my path being a PC sympathizer, and Zach stayed in the Mac camp. We had mutual respect for one another. Zach has even used a PC when he had to (although, I cannot say I was so open-minded).

Andrew & Zach, ca. 2000

There were times when we disagreed on things, and I was not always the best friend in the high school years. Looking back, there are any number of paths our lives could have taken. I could have been sick the day that Zach showed up in 4th grade. Zach could have decided that my disdain for Macs was too much to bear. But as it turns out, we’ve been friends for almost 20 years, and outside of my wife, there is no one else on this planet that I trust as much as Zach. I sometimes wonder what other directions our lives might have gone, but I am mostly thankful for the way they’ve turned out so far.

Happy Birthday Zach.

P.S. Zach did eventually win the little Mac battle, but that’s another blog post.

Posted in C# | Leave a Comment »

Why you should write comments before code.

Posted by Andrew Theken on June 12, 2010

I’ve blogged before that I think the way a developer names blocks of their code says a lot about their experience, not only in a particular platform, but also what they know about the Software Development Life Cycle.

But I don’t want to rehash that.

Today, I want to talk about when and why to comment.

When I create a new class definition, I typically try to add a summary comment right then. I also expect this of my team members. It may seem pedantic, but there’s a very good reason for it.

Expressing in English* the intent of the class tends to help me clarify the Single Responsibility that I have for that class. If I find that I have trouble specifying the expected behavior of a class in the summary, that generally means I don’t have the plan for why/how/when I need to use that class.

If I find I need a paragraph to explain the purpose of the class, it might be doing too much.

Doing too much is bad.

So, when I find that this is the case, I look to see which parts of the summary should be broken out — those breakouts typically fall along class definition lines, and before I know it, I can summarize most classes in one or two sentences, and the only do as much as they should.

Only doing as much as you should is “A Good Thing.”

The other benefit is that my colleagues can take a look at my code and understand large blocks without digging though and inspecting each member of the class. Truly, that was the point of OOP. Rather than trying to explain how each block of code works (inline comments), explain why each block of code exists (first-class documentation).

This rule of thumb works the same for members of a class. If you can type out a sentence explaining what you’re trying to do, there’s a much better chance that you will actually do that thing. There’s even a good chance that you’ll be able to identify things that are leaking in and shouldn’t be.

If When your code has gotchas, these should be outlined in the block, or the blocks. This is your chance to explain yourself. Do you need that string to be formatted just so? Do you return something in a particular order? What do you expect from consumers?

While I’m on this subject, please don’t just fill out comments to make the compiler warnings go away. That’s just a waste of your time.. I am aware of a particular tool that provides phantom summaries; summaries that are there, but have no substance. The intentions were good, but it entirely misses the point.

Imagine you bought a dictionary and all of the definitions used the word in the definition.

Definition: A sentence that defines something.

C# and Java both have great documentation support, I believe that other languages/platforms have started to make these summary-type comment sections closer to “first-class citizens,” so your reasons for not commenting code is dwindling.

I’ll leave you with this: Every line of code makes perfect sense while you’re writing it. Really good code will make sense to other people with a little bit of explanation. Great code is that which can be understood by your future self, and the people that follow you. Why not hedge your bets and provide some good documentation to make your good code, great code.

* It doesn’t have to be English, the first language of your team is best for these comments.

Posted in C# | Tagged: , , | Leave a Comment »

The “Named Generic” Anti-pattern

Posted by Andrew Theken on June 6, 2010

WARNING: This is me standing on my soapbox, and if you don’t think I’m right, leave a comment. (Also, if you’re familiar with SOLID, this may seem obvious to you).

When it comes to code, I’m pretty particular about how things are named. To me, naming both classes and class members is one of the fine arts of software development. I actually think that this can demonstrate quite a bit about someone’s experience, what they understand about the software development life cycle, and ultimately, they conceptual understanding of Object-oriented design. That’s why this particular “code smell” bothers me so much.

I’ll call it the “Named Generic” pattern.

we’ve all seen it. A developer wanted to specify a typed parameter, but missed the boat.

For example:

public RecallList GetRecalls(AutoMakersList automakers)
{
// do something useful with each automaker and yield the recalls associated with them.
}

So far, this code isn’t bad. We have a decent method name, the parameter name is ok, but what are the two type definitions here:

public class AutoMakersList : List{ /*no body*/ }
public class RecallList : List{ /*no body*/ }

Uh oh.

With two classes I have simultaneously reduced flexibility, and increased complexity. From the perspective of the consumer of this method, I now have to marshal a set of strings, then add them to a new class called AutoMakersList, which I had to search out and attempt to understand.

From the API designer’s perspective, I’ve placed some requirement on what is legal to pass in. Except I haven’t. The list is still of string, and the last time I checked, there were no validation methods on string that validate they are auto maker’s names (C# 5.0, maybe?). So I’ve really just obfuscated what I wanted to happen, which was this:

“hand me an enumerable of validated automakers”

The same could be done with this method signature:

///<summary>This will produce the recalls associated with the specified automakers.</summary>
///<param name="automakers">This is a pre-validated set of automakers for which recalls
/// will be retrieved. Valid automakers follow these rules: .....
///</param>
public RecallList GetRecalls(IEnumerable automakers)
{
//iterate over each automaker, and do something interesting to yield out their recalls
}

In Visual Studio (and perhaps MonoDevelop?), I’ve now told the API consumer what I expect, they will get it intellisense when they’re constructing the call. Whereas, if I just told you to hand me an AutoMakersList, there’s ambiguity in what is required. A side note is that Code Contracts are very helpful, and you should consider using them anyway, but that’s not the point of this post. The other benefit of this approach is that I’ve reduced the calling requirements on this method. The above example is actually not done yet, and here’s where it will *seem* like I’m contradicting my point, but I’m not, really.. really.

public RecallList GetRecalls(IEnumerable automakers)
{
//do something interesting.
}

public class Automaker
{
public String Name{get; set;}
public bool IsValid(){return valid;}
}

Instead of passing in just a a list of string, why not pass in “Automaker?” On the surface, it seems very much like just passing a simple String, the difference is that I have attached the context explicitly to the Name property, instead of implicitly from the name of the collection in which the object was stored. Let that marinate in your brain for a minute. They’re actually radically different concepts, one of them works, and IMHO, one of them doesn’t.

Part two…

“RecallList”

Here, the API designer got it half right. The the context for each recall is explicitly attached to the object that cares — “Recall”, but RecallList doesn’t actually add any value, it essentially says “this is a list of Recall”, which is the same thing as what “List” says, in a much more concise way. Although I think Classes are *cheap* *cheap* *cheap* and people are too often reluctant to add them, in this case “RecallList” is just *redundant* *redundant*.

Finally, passing a list in or out adds extra overhead that neither the API designer or the caller needs. In both cases, List places extra requirements on either side of the call, when really everyone meant to say, “here’s a set of something that you can read through. (IEnumerable)” When we apply all of these suggestions together, this is the method we’re left with:

///<summary>This will produce the recalls associated with the specified automakers.</summary>
///<param name="automakers">This is a set of automakers for which recalls will be retrieved.</param>
public IEnumerable GetRecalls(IEnumerable automakers)
{

// iterate over each automaker, use the "IsValid()" method to determine if it should be processed,
// and do something interesting to yield out the recalls
}

As I said at the beginning of the post, if you’re familiar with SOLID, most of this post will make good sense to you, but I wanted to try to put it in a context of code you’ve probably seen. Let me know what you think in the comments.

Posted in C# | 3 Comments »

 
Follow

Get every new post delivered to your Inbox.