Code Snippets Vs DRY


2010.10.24: Update: Umm, yea, so turns out Haxe does have a much simpler syntax if you just need a default getter/setter. Didn't realize that when I wrote this. But I do still think people should be careful not to let code snippet support become a substitute for proper DRY.

Originally posted: March 25th, 2009

Code snippet support in an editor can be very useful, but it does seem to carry a certain risk of discouraging advancements in DRY practices. For example, I was just working on a Haxe class (I refuse to use the goofy "haXe" capitalization) that has 14 (and that number is growing) accessors like this:

private var _assetBaseUrl:String;
public var assetBaseUrl(get_assetBaseUrl, null):String;
private function get_assetBaseUrl():String
{
    return _assetBaseUrl;
}

Might not seem like much to someone who's accustomed to Haxe, but that's an enormous amount of code just for a single privately-writable, publically-read-only member variable (For the record, Haxe has the ugliest, most verbose accessor definition syntax I've ever seen). Considering I have 14 of these (so far), just in this one class alone, that's a lot of mess.

Of course, this is where the code snippet users come in and say "Just use a code snippet!". I don't know for certain, of course, but I can't help suspect that the existence of code snippets is part of why Haxe's creators allowed the accessor syntax to be so verbose in the first place. And considering that most of those differ only by name and type, well shit, so much for the idea of "Don't Repeat Yourself"!

Contrast that with the same code in C#:

private String _assetBaseUrl;
public String assetBaseUrl()
{
    get { return _assetBaseUrl; }
}

Or better yet, D programming language:

// getter is defined in a utility module I wrote
mixin(getter!(char[], "assetBaseUrl"));

And holy crap, all of a sudden we have actual DRY!

Problem is, code snippets tend to get used as an excuse to ignore DRY (it's really only about one small step above outright copy-and-paste coding). If the early programmers used editors with code snippet support, we probably wouldn't even have functions today.

Of course, I'm not suggesting that we get rid of code snippets, or stop using them. We just shouldn't be considering them a proper substitute for real DRY.

0 comments for "Code Snippets Vs DRY"

There are currently no comments.

Leave a comment

Captcha