# When to Write Validation Code?

**URL:** <https://www.ruby-forum.com/t/when-to-write-validation-code/78471>\
**Category:** Rails\
**Created:** [December 6, 2006, 12:42am UTC](https://www.ruby-forum.com/t/when-to-write-validation-code/78471 "2006-12-06T00:42:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![toro04](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@toro04](https://www.ruby-forum.com/u/toro04)\
**Post date:** [December 6, 2006, 12:42am UTC](https://www.ruby-forum.com/t/when-to-write-validation-code/78471/1 "2006-12-06T00:42:50Z")

</div>

I’m writing a blog post on validation in Rails, and I would like to get  
some opinions on when is the best time to write validation code?

My tendency is to save them till last, but I know many people write them  
first thing. Any thoughts on when it is best to do this?

---

<div class="post-metadata">

**Author:** ![toro04](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@toro04](https://www.ruby-forum.com/u/toro04)\
**Post date:** [December 6, 2006, 1:39am UTC](https://www.ruby-forum.com/t/when-to-write-validation-code/78471/2 "2006-12-06T01:39:17Z")

</div>

I don’t like writing validations, it feels like busy work. So if I do  
them first, I drag my feet on the start of a project, if I do them last  
I drag my feet at the end.

I think the proper way is to do them first. That way you have a solid  
model to play with in your controllers and views, and you don’t have to  
worry about the funny errors about nils here and there. It’ll force  
you to do things correctly.

---

<div class="post-metadata">

**Author:** ![toro04](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@toro04](https://www.ruby-forum.com/u/toro04)\
**Post date:** [December 6, 2006, 1:41am UTC](https://www.ruby-forum.com/t/when-to-write-validation-code/78471/3 "2006-12-06T01:41:17Z")

</div>

First thing.

How else will you write the tests for failing and passing conditions?

---

<div class="post-metadata">

**Author:** ![toro04](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@toro04](https://www.ruby-forum.com/u/toro04)\
**Post date:** [December 6, 2006, 1:41am UTC](https://www.ruby-forum.com/t/when-to-write-validation-code/78471/4 "2006-12-06T01:41:18Z")

</div>

On 12/6/06, Adam T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m writing a blog post on validation in Rails, and I would like to get  
> some opinions on when is the best time to write validation code?
> 
> My tendency is to save them till last, but I know many people write them  
> first thing. Any thoughts on when it is best to do this?

I tend to add validations as part of my approach to TDD. Once I have a  
need  
to restrict values I write a test or tests for it, include the  
validation  
and then move on.

This way it stays out of my way until I need it, and I don’t need to do  
all  
the validations up front, or leave them until the end. They just become  
part of the flow.

---

<div class="post-metadata">

**Author:** ![toro04](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@toro04](https://www.ruby-forum.com/u/toro04)\
**Post date:** [December 6, 2006, 2:02am UTC](https://www.ruby-forum.com/t/when-to-write-validation-code/78471/5 "2006-12-06T02:02:09Z")

</div>

Daniel ----- wrote:

> On 12/6/06, Adam T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I’m writing a blog post on validation in Rails, and I would like to get  
> > some opinions on when is the best time to write validation code?
> > 
> > My tendency is to save them till last, but I know many people write them  
> > first thing. Any thoughts on when it is best to do this?

Thanks for your comment.

---

<div class="post-metadata">

**Author:** ![toro04](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@toro04](https://www.ruby-forum.com/u/toro04)\
**Post date:** [December 6, 2006, 2:02am UTC](https://www.ruby-forum.com/t/when-to-write-validation-code/78471/6 "2006-12-06T02:02:35Z")

</div>

Chris Mohr wrote:

> I don’t like writing validations, it feels like busy work. So if I do  
> them first, I drag my feet on the start of a project, if I do them last  
> I drag my feet at the end.
> 
> I think the proper way is to do them first. That way you have a solid  
> model to play with in your controllers and views, and you don’t have to  
> worry about the funny errors about nils here and there. It’ll force  
> you to do things correctly.

Thanks for your comment.

---

<div class="post-metadata">

**Author:** ![toro04](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@toro04](https://www.ruby-forum.com/u/toro04)\
**Post date:** [December 6, 2006, 2:01am UTC](https://www.ruby-forum.com/t/when-to-write-validation-code/78471/7 "2006-12-06T02:01:38Z")

</div>

Adam T. wrote:

> I’m writing a blog post on validation in Rails, and I would like to get  
> some opinions on when is the best time to write validation code?
> 
> My tendency is to save them till last, but I know many people write them  
> first thing. Any thoughts on when it is best to do this?

Thanks for your comment.
