Code ReviewWho Can Help Me
Originally posted at 3/22/2011
This time, taking on Who Can Help Me, I found some really interesting things there.
For one thing, we got this little guy:
I was all ready to discover INewsRepository –<> NewsRepository : Repository<NewsItem>, etc. What I found, instead was a real service:
That is how it is supposed to be, to be frank. There is a need to abstract something, and we write just enough code to make it work. I would argue with the implementation of this, however, because the approach for multi threading is wrong headed. We shouldn’t spin out a new thread pool work item just to execute the request when the FluentTwitter API already contains async version that can do quite well for us, but the overall concept is sound. And I was relieved not to find nested repositories in my first few steps there.
Of course, I then discovered that this nice service has some friends from the left side of the blanket:
I think that I expressed my opinion about code such as this:
A waste of keystrokes, and highly inefficient to boot. You don’t make queries by Id in NHibernate, you use Get or Load instead.
Overloading the infrastructure – After reviewing the code, I was surprised to see so much of it dedicated for caching:
What surprised me more is that in the entire application there were exactly two locations where a cache was used. In both cases, it led to the same service. Implementing a much simpler solution in that service would have chopped quite a bit of code out of this project.
And then there was this:
Luckily, this is dead code, but I was quite amused by this code, in a “shake you head in disbelief” fashion.
First, for a code that is obviously meant to be used in a multi threaded fashion, it is not thread safe. Second, it is actually a memory leak waiting to happen, more than anything else. If you call that method, your items will never freed.
The next is a personal opinion, but I can’t help feeling that this is pretty heavy weight:
Well, that is true whenever you are looking at an XSD, but in this case, we just need to expose two properties, and I think that it would have been perfectly fine to stick that into the <appSettings/> section. There are actually several places where similar approach has been tried, but I don’t see this of any value if you aren’t writing a library that might require special configuration. If you are writing an app, using the default modes is usually more than enough.
Reading the controllers code wasn’t a real surprise. One thing that did bother me is the amount of mapping that is going on there, and how much of that I was simply unable to follow. For example:
Which is then calling;
Which then goes into additional custom implementations and convention based ones.
The reason that this is important is that this is a prime location for Select N+1 issues, and indeed, we have several such occurrences of the problem just in this piece of code.