# Check for Nil Everytime?

**URL:** <https://www.ruby-forum.com/t/check-for-nil-everytime/141247>\
**Category:** Rails\
**Created:** [June 24, 2008, 1:39am UTC](https://www.ruby-forum.com/t/check-for-nil-everytime/141247 "2008-06-24T01:39:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 24, 2008, 1:39am UTC](https://www.ruby-forum.com/t/check-for-nil-everytime/141247/1 "2008-06-24T01:39:05Z")

</div>

So I have a view where I take a date and I run a strftime("%b %d") date  
format a string, right?

Ok, now if the date can be nil ; if that is the case I dont want an  
error message being thrown if I call strftime on it. I would ideally  
check for nil when I am displaying the value, right? But do I really  
have to? If the value is not required then there ought to be a way of  
just rendering a warning so we can avoid those nil errors (whiny\_nils?)  
Do I have to check for nils everytime and everywhere and all over the  
place? Checking for nils should be optional if there is a nil the  
compiler should just generate a warning and move on… I mean does  
anyone remember the java.nullpointerexception horrors?

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 24, 2008, 1:56am UTC](https://www.ruby-forum.com/t/check-for-nil-everytime/141247/2 "2008-06-24T01:56:21Z")

</div>

On Jun 23, 2008, at 4:39 PM, Ather S. wrote:

> (whiny\_nils?)  
> Do I have to check for nils everytime and everywhere and all over the  
> place? Checking for nils should be optional if there is a nil the  
> compiler should just generate a warning and move on… I mean does  
> anyone remember the java.nullpointerexception horrors?

What’s wrong with?

mydate.strftime(…) unless mydate.nil?

Makes it very clear what you’re doing…

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 24, 2008, 2:00am UTC](https://www.ruby-forum.com/t/check-for-nil-everytime/141247/3 "2008-06-24T02:00:17Z")

</div>

Philip H. wrote:

> On Jun 23, 2008, at 4:39 PM, Ather S. wrote:
> 
> > (whiny\_nils?)  
> > Do I have to check for nils everytime and everywhere and all over the  
> > place? Checking for nils should be optional if there is a nil the  
> > compiler should just generate a warning and move on… I mean does  
> > anyone remember the java.nullpointerexception horrors?
> 
> What’s wrong with?
> 
> mydate.strftime(…) unless mydate.nil?
> 
> Makes it very clear what you’re doing…

That sounds fine but it is just all this extra code that is unnecessary  
I feel. Why would you want to throw an error message crippling the  
entire application if some field is nil?

There should be a more graceful method where we can avoid even checking  
for nil because most of the time we forget to check for nil.

You see all the required fields have presence of validation in the  
models but there are these optional fields which can throw these nil  
errors; we should be able to avoid peppering our scriptlets with .nil?s  
; does that make sense? Is it just me?

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 24, 2008, 2:20am UTC](https://www.ruby-forum.com/t/check-for-nil-everytime/141247/4 "2008-06-24T02:20:59Z")

</div>

One more thing what if a relationship for example user.profile.name has  
one link in the middle that is nil then again the entire application is  
littered with nil:nilclass errors! ; we should be able to tackle such  
errors in data with small tags rather than crippling the entire  
application.

Philip H. wrote:

> On Jun 23, 2008, at 4:39 PM, Ather S. wrote:
> 
> > (whiny\_nils?)  
> > Do I have to check for nils everytime and everywhere and all over the  
> > place? Checking for nils should be optional if there is a nil the  
> > compiler should just generate a warning and move on… I mean does  
> > anyone remember the java.nullpointerexception horrors?
> 
> What’s wrong with?
> 
> mydate.strftime(…) unless mydate.nil?
> 
> Makes it very clear what you’re doing…

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 24, 2008, 3:19am UTC](https://www.ruby-forum.com/t/check-for-nil-everytime/141247/5 "2008-06-24T03:19:26Z")

</div>

On Jun 24, 1:20 am, Ather S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> One more thing what if a relationship for example user.profile.name has  
> one link in the middle that is nil then again the entire application is  
> littered with nil:nilclass errors! ; we should be able to tackle such  
> errors in data with small tags rather than crippling the entire  
> application.

It’s a delicate path to tread. In a system where errors are swallowed  
silently it’s a bloody nightmare to work out what is happening when  
things don’t work.

Fred

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 24, 2008, 3:41am UTC](https://www.ruby-forum.com/t/check-for-nil-everytime/141247/6 "2008-06-24T03:41:13Z")

</div>

That is fine during “debugging” and testing phase we can have the errors  
blow up; but in production these errors should not annoy users with  
glaring ugly error messages; in production we should have the ability to  
have silent warnings or separate error logs which post these errors but  
on the viewer screen.

Frederick C. wrote:

> On Jun 24, 1:20ï¿½am, Ather S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > One more thing what if a relationship for example user.profile.name has  
> > one link in the middle that is nil ï¿½then again the entire application is  
> > littered with nil:nilclass errors! ; we should be able to tackle such  
> > errors in data with small tags rather than crippling the entire  
> > application.
> 
> It’s a delicate path to tread. In a system where errors are swallowed  
> silently it’s a bloody nightmare to work out what is happening when  
> things don’t work.
> 
> Fred

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 24, 2008, 4:10pm UTC](https://www.ruby-forum.com/t/check-for-nil-everytime/141247/7 "2008-06-24T16:10:14Z")

</div>

Craig W. wrote:

> not wishing to get into a philosophical debate with you but it wouldn’t  
> make any sense to have production mode swallow errors that development  
> mode or testing mode would toss an error…

I am not advocating swallowing the error just making the error less  
verbose or less glaring and less of a show stopper; We already have  
verbose and --trace related error magnification tools. We have  
whiny\_nils=true already in there so that is precisely what I am talking  
about; However perhaps I am requesting a little more than that.

> that would defeat the whole  
> design process. I actually count on what works in development or testing  
> to work in production.

In development one does not go down every imaginable path ; even in  
testing one cannot assure that what works in testing/development will  
work in production; usually in production the end user modifies the data  
or an anomaly arises which causes the entire application to start  
throwing these nil.nilclass errors all over the place.

> you can trap for nil entries in validations and validations are simple

what if one forgets these validations ? (nils can still arise when  
related records are deleted and dependents are left dangling) what if  
someone deletes a dependent and a dangling pointer remains to that  
record? it would be cool if that dangling pointers just printed  
something innocuous like “points to nil” and the application moved on  
instead of throwing a nil.nilclass error.  
Again this app. is in development mode in production machine so I dont  
know precisely how it will react in prod mode.

> you can always test it with .blank?

test every relationship with a .blank? that is an easy candidate for  
abstracting .blank and putting it in a lower layer, dont you think?

> you can always convert nil values to empty strings by simply  
> adding .to\_s  
> i.e.  
> test = User.new  
> test.name  
> =\> nil  
> test.name.to\_s  
> =\> “”

Thanks will try this one out but this is an example of what I mean.  
Maybe this is all I need to do, wrap everything in .to\_s

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 24, 2008, 5:09pm UTC](https://www.ruby-forum.com/t/check-for-nil-everytime/141247/8 "2008-06-24T17:09:49Z")

</div>

On 24 Jun 2008, at 15:10, Ather S. wrote:

> Again this app. is in development mode in production machine so I dont  
> know precisely how it will react in prod mode.

If I were you I would worry about preventing bogus data rather than  
trying to deal with it (eg add some foreign key constraints)

Fred

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 24, 2008, 4:27am UTC](https://www.ruby-forum.com/t/check-for-nil-everytime/141247/9 "2008-06-24T04:27:25Z")

</div>

On Tue, 2008-06-24 at 03:41 +0200, Ather S. wrote:

> > > one link in the middle that is nil ï¿½then again the entire application is  
> > > littered with nil:nilclass errors! ; we should be able to tackle such  
> > > errors in data with small tags rather than crippling the entire  
> > > application.
> > 
> > It’s a delicate path to tread. In a system where errors are swallowed  
> > silently it’s a bloody nightmare to work out what is happening when  
> > things don’t work.

* * *

not wishing to get into a philosophical debate with you but it wouldn’t  
make any sense to have production mode swallow errors that development  
mode or testing mode would toss an error…that would defeat the whole  
design process. I actually count on what works in development or testing  
to work in production.

Consider…

you can trap for nil entries in validations and validations are simple  
you can always test it with .blank?  
you can always convert nil values to empty strings by simply  
adding .to\_s  
i.e.  
test = User.new  
test.name  
=\> nil  
test.name.to\_s  
=\> “”

Craig
