VS.Net #Region

I’m reviewing some code for a client. I’ve decided that I don’t like the #Region functionality from the VS.Net IDE… It seems to be another ‘sticking plaster’ to help make things ’easier’ for people to structure code in inappropriate ways…

I guess I’m not a fan of ‘outlining editors’; perhaps I’m just not used to them. Anyway, I can see why #Region might have been considered a good thing to design into the IDE given the way Microsoft love to push wizard generated code into places that you’re forced to also put user generated code (I know, it’ll be fixed in Whidby, ZZZzzz…). You can just see the dev meeting:

Manager: That ‘forms thing’ demo you just did looked crap. The nice draggy drop stuff worked well but then when you went in to the code to add the single line of business logic code; you know, that “Hello World” thing, well, it was scary. There was all that other code you hadn’t written in there, how did that get in there? Why is it there? That will scare people.

Dev: That’s all generated for us to make things easier. We could structure the code we generate so that the user never needs to edit the same code that we generate, but that’s very hard to get right…

Manager: But it’s scary to see all of that code here in this demo. The people we demo to will hate us, can’t we put all that code in another file…

Dev: Er, no, that’s on the list for the future, it’s one of the things we’re making crap so that we can crow about how we’re making the next version soooo much better by simply providing functionality that other languages have had since the beginning and touting it as a wonderful new invention…

Manager: I see, good.

Dev: We could add a hack to the editor…

Manager: How long?

Dev: 5 mins… We can add a #HereBeDragons directive. We could then enclose the generated code in a #HereBeDragons block and that will hide the scary code and just display “Here be dragons” in a pretty colour and nice font.

Manager: Make the font user definable, and give them a pretty clicky thing to make it show the code…

And then you let users define their own #Regions …

I always used to think that there must be some element of guilt felt by the programmer who adds the 4001st line to an already stupidly huge function, after all, it’s clear to him how bad the function already is and yet here he is adding more crap to it… Lately though I’m not so sure, the whole premise is based on projecting my values onto someone else (never a good thing); some people probably just don’t see anything wrong with a 4001 line function called DoStuff() that takes and returns a single blob of XML… Anyway, I digress…

I think #Regions can be used to soothe the kind of pain that someone might feel at seeing a large file of code that’s inappropriately structured. There’s too much going on in the file to make it feel comfortable to read so we can throw in a few regions to make it look like it’s better… Give the regions nice names and we dont need to add block comments to explain the functionality… Nest the regions to make it smaller and smaller and smaller until…

Oooh, pretty clicky button and no scary code…

This whole rant is because IMHO this is one of those cases where by solving the wrong problem the users are actually done a disservice; what looks like a simple ‘fix’ is actually a way to hide a million sins… This rant is also because I don’t like mornings and because I always have a bit of a tantrum when I start to review a new client’s code - it’s just the disconnect between two programmers views on what’s right… I’ll get over it.