Monday, June 28, 2010

Dependency Injection

Hey guys what are your views of DI? It does seem partly ritual to make sure things are testable, but I suppose it also is to ensure you can easily do context sensitive configurations or the ability to replace functionality. Of course if most of your interfaces end up only have a single implementation does it seem like too much overhead? Or too convoluted?

I'm personally a fan of the pattern and think that the (although somewhat annoying but also minor) overhead of maintaining interfaces is worth the cost of having a consistent scheme of how things get plugged together. Also I don't think it's too convoluted to follow now that I have experience with the pattern and workflow.

Anyway what do you guys think about the practice? What do you find are the pros and cons? Do you have any run ins with people new to it? How do they feel about it? etc..etc

Saturday, June 26, 2010

Area 51 Proposal

In case anybody's interested, I have a proposal up at Area 51 for a stack exchange site regarding development methodologies.  If you're interested, show your support by following and contributing!

http://area51.stackexchange.com/proposals/9543/development-methodologies

Saturday, June 19, 2010

Tiny DSLs In The Code

I wanted to explore a little on the LINQ vs. lambda that Dave mentioned. I really don't have a direction for this post but wanted to get some thoughts down and see what others were thinking. Also it should be noted that I'm talking mostly about the IQueryable abilities and the two methods of using them (LINQ: from x in list where x.valid select x, and lambda: list.Where(x => x.valid))

The lambda syntax he mentions is just strictly straight up C#. You chain together the methods giving them the lambda expressions for the various actions to perform on the queryable you are working with. You get to build up what you want on a lazy evaluated data structure and it flows fairly cleanly inside C#.

There are some advantages to this, someone familiar with the basic idea of method chaining and the language idea of lambda expressions can easily understand the flow of the code. This sort of internal DSL gives us the ability to take advantage of language features and try to produce more expressive code. Several OSS libraries in the .NET world have certainly picked up on the fluent interface idea which is an internal DSL practice.

Then we have the LINQ syntax. It allows us to also build up what we want and have it lazily evaluated but now the you are working in a language that isn't really a part of C#. Although LINQ is a language feature of C# it still is more of an external DSL. It's a small language implemented on its own. Thankfully the compiler fully understands this DSL and can convert it to the expressions to form a fully valid C# program. You can still chain actions off of it and pass it around, but you are given a new language to express the idea with and not only relying on C#.

Now someone needs to know this new language in order to understand your code and not just C#. Perhaps LINQ is a bit of a simple example, but this new language can provide create expressiveness and higher levels of thinking. These are generally great but again now someone needs to know two languages. This second language though is purposefully designed to be smaller, stricter, more focused, and more expressive in its domain. Naturally I'm speaking about DSLs and their advantages and disadvantages.

I wonder if DSLs will generally catch on. Like I mentioned in a comment on Dave's post the Ruby world seems like enjoy some DSLs. They are still really internal DSLs since they are implemented in and used in Ruby which is a general programming language but Ruby's flexibility in syntax allows for them to produce some pretty interesting results. I suppose the most famous is RSpec and how it can help a developer very expressively and cleanly say what his/her code is suppose to do. This does mean that you now have to learn how to write this new DSL but in return it offers a more natural and less ambiguous way to write what you mean.

.NET does have Boo which is pretty cool language. It's a general statically typed programming language on the CLR. Python inspired syntax but it has a very extensible compiler and a built in idea of macros. You can actually define new syntax in Boo with Boo. So you could actually write the core of the system in C# and then have areas that you write in a DSL in Boo that when compiled gets tied into the system (such as maybe a rules engine on customer pricing). Or you write the whole thing in Boo.

Now all this DSL stuff certainly brings more overhead and higher level of entry for a new developer so I'm curious what everyone's thoughts are. Do you think they are worth it? Has promise? Some people feel that proper use of DSLs or just in general polyglot environments are going to be what allows us to manage complexity better and improve our disciple. Instead of hitting everything with a hammer, you could add in a saw, coat hanger, and duct tape.

The .NET ecosystem of languages has certainly expanded with F# and IronPython being supported languages, let alone all the others that are out there. The Java world seems to embrace their ecosystem of languages and although many feel Java the language has become fairly stale the JVM has still been continuing to explode with ideas. Perhaps I should write a post about my thoughts on the big differences I feel between the .NET and Java world (mostly about open mindedness and OSS).

Anyway again, What do you guys think about polyglot environments and DSLs both internal and external?

Thursday, June 17, 2010

Natural Language Constructs

Yesterday I casually mentioned to Sean that in my code I seem to vastly prefer lambda over LINQ syntax. He translated that to mean that I probably prefer chaining my actions from one object to the next, or something like that. And, you know what? He was right. I thought about it a bit more today and what I discovered was the reason why I've never liked certain static method calls in C# (actually, I've never liked static methods in general and only grudgingly accept them where needed).

Taking this a bit further, I narrowed down a few key examples of static method calls I've never liked. Let's take a look at what are probably the two most common for me:

string.IsNullOrEmpty(input);
string.Format(input, args);

I can't tell you how many times I've begun typing a variable to check if it's null, only to remember that it's a string and have to back-track for a moment to make the method call. Or how many times I've begun typing a string only to later remember that it needs to be formatted and to back-track for a moment, etc. It's just a little thing, but it irks me a little bit every time.

(In fact, this is probably the reason why it took so long for me to catch on to using string.Format() instead of just concatenating strings together, which I've always done until recently. I hope all the places I've ever worked will forgive that.)

But no longer must I accept these things. For now I can write extension methods! Behold:

public static string FormatWith(this string s, params object[] args)
{
  return string.Format(s, args);
}

public static string IsNullOrEmpty(this string s)
{
  return string.IsNullOrEmpty(s);
}

You may be wondering if that second one will break if it's called on a truly null variable. It doesn't. Man, I love C#.

I can't speak for everyone else, but to me this makes the code so much easier to write. I feel like a weight has been lifted. It's all in how we look at what we're writing, I guess. For me it's about the subject at hand. In this case, the variable. I prefer to start with the variable and chain my commands to it accordingly. (This is also why I hate output parameters and things like Array.Reverse(), which by the way I've also replaced with an extension method). I mean, compare for yourself:

if (string.IsNullOrEmpty(s)) return false;
vs.
if (s.IsNullOrEmpty()) return false;

return string.Format("The value is: {0}", s);
vs.
return "The value is: {0}".FormatWith(s);

At the very least, it's less typing. But, more to the point, it starts with the subject and then proceeds with the actions on that subject. That's how we learned to speak English, that's how some of us want to write our code. It just seems like a more natural language construct, doesn't it?

Either way, I've been making a lot of these extensions, and getting ideas for more from various sources online. StackOverflow has a great thread on the topic.

[Edit: I renamed IsEmptyOrNull() to IsNullOrEmpty() because, unlike Format(), the compiler doesn't seem to complain that it's a name already in use. It can tell the difference between the two.]

Thursday, June 10, 2010

Domain Sketch

I think this is the rough sketch that I wanted the architect to do back at my old job.  But now, as I've been diving deeper into this stuff myself (I'll admit, there was just no motivation to improve anything back there), I've drawn this for my teammates here on my cubicle's white board.
 
It's a basic diagram of the domain layout I'm working towards here, and if you were at my last job then you may notice it borrows heavily from that design.  I've changed a good bit in the interaction between the handlers and the processors to accommodate the different application ecosystem at my new job, but there wasn't enough room on this little board to go into much detail there.

One thing that may help in pushing this idea at my current employer (being a financial institution) is the auditing at the single entry point.  If the client applications are as dumb as possible and all of the business logic is in the Processors and all of the actual data persistence/interaction is in the Repositories, then having that single entry point is a convenient place to keep a detailed audit trail of all requests that come in to the domain.