views:

339

answers:

9

For example, we have some CSS rules to define our form layout. We use the following markup:

<div class="foo">
    <label class="bar req">Name<em>*</em></label>
    <span>
     <asp:TextBox runat="server"/>
            <label>First</label>
    </span>
    <span>
     <asp:TextBox runat="server"/>
            <label>Last</label>
    </span>
    <div class="clear"></div>
</div>

In my opinion, this is perfectly fine markup. Following these rules is not a burden since you're supposed to worry about clean, correct markup anyway. It's being suggested that we "enforce" these rules with a control that looks something like the following:

<x:FormField runat="server" Label="Name" Required="True">
    <x:TextBox runat="server" hint="First"/>
    <x:TextBox runat="server" hint="Last"/>
</x:FormField>

My instinct tells me that XHTML markup is our friend, not our enemy. Two questions:

  1. Am I completely off base to resist making all these custom controls just to emit markup?
  2. If you agree with me, what reasons would you give?
+17  A: 

I vote use the controls, because if that markup ever changed (say you wanted to add some nice jQueryUI effects to the boxes or add a RequiredFieldValidator control) you can make the change in one place and be done with it. DRY.

Joel Coehoorn
+1 here - I have plenty of controls that only emit markup, because it gives me a much more object-oriented way of controlling it. Much more convenient to access your entire form setup through an object model.
Rex M
+1 because we already see other answers wanting special spacing on the markup
eglasius
What, then, do you keep in the markup? I may be misinterpreting, but it almost sounds like the perfect world you describe would have almost no XTHML markup in your pages.
Larsenal
Certainly not everything should be in a control, but whenever you have code of any type that you find yourself repeating you should abstract that away. Control declarations like you showed are just part of the markup.
Joel Coehoorn
Aye, anything that starts to become a repeatable pattern should be abstracted and variations built on top of that. Makes your code much more modular and much easier to deploy new instances of the same functionality. XHTML should be limited to structural and aesthetic, not repeated functional code.
Rex M
Rex M, would you say the XHTML in my first example is functional? It sure feels structural and aesthetic to me.
Larsenal
A: 

"Enforcing" a look-and-feel through CSS costs a lot less in time, support, maintenance and gray hair. There is nothing inherently more "enforcing" about composite controls (which is what your second snippet looks like.) There is, however, a lot more work involved in custom controls.

Bottom Line: Custom controls are about reuse and redistribution, not layout.

Dave Swersky
+1  A: 

I can side with you on this one based on personal experience with this exact example. It may not apply as much for other scenarios, but label+textbox deserve to stay separate. Usually everything is fine until you get about 2/3 through the project and other people start to critically review it. My experience was that users/managers/ui designers wanted exceptions in many places - different spacing, an extra something between the label and the textbox, etc. Also keep in mind validation routines and one validator does not fit all sizes in ASP.NET. By the time you get a working control like this to handle all the different scenarios, it probably would have been less work to just keep label+textbox on the forms.

flatline
A: 

I do something similar. Take that to the extreme, where you have 30-40 controls like this on a page. Having controls that take care of labels, validators, etc for you make your life a lot easier.

We also have automated form helpers that can load form values onto an object and vice-versa, to alleviate all the

lefthand = righthand code all over the place.

Ben Scheirman
You put the text string in, you take the text string out, you put the text string in and you pass it all about... It's the Attribute Shuffle! :D
Jeffrey Hantin
+7  A: 

Against the controls:

  • Performance numbers. Measure a baseline XHTML page and the same page with 20 controls on it that emit the same markup. This would give you an estimate.
  • Additional cost. You need to factor out the cost of developing, testing and maintaining these controls.

For the controls:

  • Consistency. You now have a single place to control the XHTML structure with all elements you need and it'll stay consistent across all pages.
  • Regression. You can have the proper unit and scenario testing to ensure your XHTML structure is what you require it to be.
  • Reuse. You can reuse the same control in other projects in future.
Franci Penov
+1, but I would label "Additional cost" as "Initial cost", because if you're getting proper reuse from the control then total cost should be lower.
Joel Coehoorn
It's "additional cost" in sense of "on top of what it cost you now to ddevelop your project". But you are right, later reuse would offset that cost, as it could lower future development costs.
Franci Penov
A: 

Using a custom control may be a good choice if you intend to re-use the code, or if you wish to define custome properties or attributes on the code, but it makes little sense otherwise.

Mystere Man
+4  A: 

I would say "Don't become an abstraction astronaut" aka "Architecture Astronaut"

Further reading: http://www.joelonsoftware.com/articles/fog0000000018.html

Neil N
A: 

I agree with Franci... there are good reasons for using controls like that. However, don't take it to the extreme. If you're being told that everything should be a control and compiled in a separate file, then someone is enforcing a policy blindly and without using common sense. Policies are only good if they are applicable. Applying policies (or coding patterns) in situations where it doesn't apply usually results in a nightmare down the road.

Others have said that already, but the reason why I'm bothering to answer here is to point out one special thing. If you are a junior developer and have a senior person telling you to do things a certain way - you do it that way. There is a possibility that they know about requirements you don't know about, or something about the environment that they aren't telling you. There are many times when I've told a junior developer "just do it this way" without a lot of explanation why - because we just need to get it done, and giving them a programming lesson is something I don't have time for. I will usually go back and explain to them later, but when a project is behind schedule (and they always are), you do what you're told and argue about it later.

Jasmine
A: 

I'd say it depends on how much the control gets used and is it just in one project or do you need to distro this amongst other projects? And whats wrong with a user control? You get the reusability + you still have markup

BPAndrew
I guess the TextBox control doesn't feel as bad as the FormField control.
Larsenal