views:

137

answers:

11

When coding up, say, a registration form from scratch, does it make sense to get it functioning with the expected inputs first, and then go back and catch/handle the unexpected inputs and deal with errors?

The alternative would be to process the input, checking any constraints and insuring that they are handled properly, and then dealing with getting the typical use case functioning correctly.

Is one way preferable to the other, and if so why? Also, is there an alternate way to go about this sort of 2-part task?

To clarify, by validity, I mean more than just data validation, including business rules, such as "No more than X people can register for this event"

+3  A: 

One way would be to take a TDD approach (assuming you write unit tests). Start off by writing your unit tests, then work on getting those unit tests to pass.

In my opinion UI work should be done last since you probably have plenty to do on the back-end functionality.

Miyagi Coder
Spoken like a true back-end developer :)
willcodejavaforfood
+2  A: 
  1. Write the test for a method of the validation object
  2. Write the method of the validation object
  3. Test until pass

Repeat 1-3 until all methods are tested.

  1. Write the form and feed the data into the tested and working validation object methods.

Use the same procedure for the business object handling the data post validation.

Johan Öbrink
+2  A: 

I always like coding up the functionality first and then adding validation later. Seems fine to me seeing as most coding is done in a development environment and no one will be submitting data while you're working on it.

But whichever way you feel comfortable with is best.

The downside to my method is that, if you're disorganized, you may forget to add a validity or sanitation check somewhere. And it does happen :-)

T Pops
Ok thats fine i guess
Greg
+2  A: 

If it's not TOO much work, I'd start with basic input validation. Especially for dates or identifiers, like order numbers. It's easier to loosen validation than to tighten it. Basic input validation can save a lot of debugging time further down in the backend.

On the other hand, say you're talking good-looking Javascript enabled validation that supports multiple languages. In that case you'd be better off writing a simple first version, and develop the backend based on that. You can polish up the input form when it's beginning to approach a final version.

Andomar
+6  A: 

The best thing to do, in my opinion, is to get a decent first version working which may not deal with all of the unexpected cases completely, but is made thoughtfully and modularly. You can then go back and perfect the logic so that your tests pass.

In the real world, this method pays off because you're more likely to be productive and interested when there's something working with a few kinks, rather than just being stuck trying to figure out all the edge cases in your head and deal with them at the start.

mandaleeka
+1 for explaining why going for a version that works end-to-end is often good (I'm reminded of "tracer bullet" coding, from The Pragmatic Programmer; http://www.artima.com/intv/tracer.html)
Jonik
Jonik, thanks for reminding me of that passage from TPP. Yes, dealing with uncertainty by trying to specify things to death upfront is unwieldy and gets exponentially harder as the size of the task increases.
mandaleeka
+2  A: 

Once you get the functionality working, will you REALLY go back and add validation code?

Most of us would like to, but many of us run out of time at the end of the project.

Robert
A: 

If you're breaking the code up into "model" and "UI", then some of this is easier to figure, but basically requires a design choice: are your model objects going to assume correct inputs and put the onus on the UI, or test for correct inputs.

Now, me, being a belt and suspenders guy, I tend to answer that question "yes": the model will check the domain even though the inputs should be checked anyway. But in any case, once you make that decision, you have your answer: if domain checking is part of the definition, you should build it and test it with all the other parts. If you separate the domain checking from the functionality, build and test them separately.

Charlie Martin
+1  A: 

I like to put up a couple of fields worth of functionality, then do the validation checks for that functionality, then the validation for the rest, and finally fill in the rest of the functionality.

Adam Jaskiewicz
+3  A: 

I'm strongly in favor of writing the validators first. Once you have something that seems like it's working it will be much harder to go back and add the validators. By writing the validators first (but not necessarily handling the exceptions they raise) you make sure that you get them done. It also gives you a good chance to think about exactly what you're expecting - this can help you think of special cases you'll need to cover later.

Tracer bullets are nice, but the principle doesn't need to be applied to every aspect of a project.

edebill
A: 

Having seen too many databases where people neglected to do the validation methods, I vote for doing it first.

HLGEM
+2  A: 

I'm not sure that designing one way or the other is superior... While Mr. Leekman says that cranking out the functional part and then going back and doing the tedious work of edge-cases is more productive, I'm not sure I see how.

Having the functional code is not useful (and even dangerous) if you're allowing non-permissible values (i.e. values your functional code didn't expect) creep in.

Conversely great boundary checks are useless if the values that make it through aren't processed in some way.

In a production environment, you really have nothing to show if you are not covering both validity and functionality. The order in which you do these things really should be an issue of personal preference. If you're not feeling creative and just want something tedious to do, write the validity-checking portion of the code. If you've just figured out the perfect algo to do exactly what you need during your coffee break, sit down and write the functional part and go back to bounds-check later on.

ParoXoN