Leaky Abstractions? Try Asp.Net!

First here's a code example:

Single line 'TextBox':

<asp:TextBox ID='dd1' runat='server' TextMode="SingleLine" MaxLength="10"> </asp:TextBox>
                                                                                    ^^^^This 'MaxLength' is respected!

Multi line 'TextBox':

<asp:TextBox ID='dd1' runat='server' TextMode="MultiLine" MaxLength="10"> </asp:TextBox>
                                                                                    ^^^^This 'MaxLength' gets ignored!

Now here it is in pictures:

max length ignored on multi-line TextBox

The single-line TextBox respects the maximum length property. The multi line textbox does not. We see other differences too: these two 'TextBoxen' differ from each other in far more than the number of rows.

The cause here is a classic 'Leaky Abstraction' - and asp.net is riddled with such leaks.

The 'abstraction' of "TextBox" is used to emit either one of three underlying html elements -- "TextArea", "Input type=text" or "input type=password" -- depending on the 'TextMode' property. The three controls are wildly different animals.

"Solutions" to this problem tend to use javascript to stop input once the maxlength is reached.

Edge cases are frequently missed, for example:

  • Once maxlength is reached, ignore input, but don't ignore: Arrow keys, Delete key and Backspace. Others?.
  • Pasting of text should truncate down to maxlength.
  • Disabling of javascript is always possible, so length validation needs to be applied on server side as well.
  • Now that javascript is in play, browser compatability becomes a bigger problem.

That's pretty nasty stuff to have to perform -- and worse, you're given no warning that you need to perform it.

The API tells you that there is a maxlength property you can use... but the API, in this case, is lying.

I'm not really concerned with this problem technically. And I think the asp.net abstraction is so extreme that it's admirable. I'd like to know how to patch this kind of leak.

Here's what I'm wondering:

Is there a way the text box class could have been implemented so that you get a compile error for setting the maxlength property when the textmode is multiline?

That would be enough to make it a safe abstraction in my books. It's more extreme than the static typing afforded by most static type systems.

I expect that such a constraint could be achieved with a language/tool like Spec#.

An alternative would be to throw an exception when setting the maxlength property if the textbox type is multi-line, and vice versa. But that seems like quite a faulty approach.

I'm just not happy about this at all. I think I will need to write a very stern letter to Scott Guthrie.


My book "Choose Your First Product" is available now.

It gives you 4 easy steps to find and validate a humble product idea.

Learn more.

(By the way, I read every comment and often respond.)

Your comment, please?

Your Name
Your Url (optional)
Note: I may edit, reuse or delete your comment. Don't be mean.