nil.blank?

C2 Wiki discusses how nil.to_s.empty? is available in rails and nil.blank? is provided for Ruby On Rails,

Ruby
[source:ruby]
nil.to_s.empty?
>> true
[/source]

Rails
[source:ruby]
nil.blank?
>> true
[/source]

Java
[source:Java]
String s = null;
return s == null;
>> true
[/source]

Let’s say we want to check that s is non-blank string. The danger is that s could be null. We will use random to seed s (Or just think of it as the invisible hand typing a form value). In ruby we can do:
[source:ruby]
s = rand < 0.5 ? “” : nil
!s.blank?
>> true
[/source]

And in java?
[source:Java]
String s = Math.random() < 0.5? “” : null;
return !s.equals(“”); //Playing roulette
>> NullPointerException?!

if (s!=null) {
return !s.equals(“”);
} else {
return false;
}
>> true
[/source]

Verbosity, or simplicity? I feel that in web development where checking for blanks occur on a frequent basis, ruby has the better syntax. Not just in length, but it fits with my mental context as well.

8 Responses to “nil.blank?”

  1. Jonathan Giles Says:

    You are mistaken in the roulette statement regarding Java – the solution is to simply reverse the equals statement. You therefore should show:
    final String s = Math.random()

  2. Jonathan Giles Says:

    Good to see my whole post come through there 🙂

    To clarify (and hopefully this one shows):
    You are mistaken in the roulette statement regarding Java – the solution is to simply reverse the equals statement. You therefore should show:

    final String s = Math.random() < 0.5 ? “” : null;
    System.out.println(!””.equals(s)); // No longer playing roulette

    Granted, it is not as clean as the ruby example, but to claim that the alternative is the best that Java can do is incorrect.

  3. Thong Kuah Says:

    @Jonathan: You are most certainly correct. I have done that myself too (should have remembered 😉 ) It is a testament to programming languages that anything is (almost) possible. However, the question of how the language construct will fit with your expectations will arise.

    The example I gave is clearer for a Java developer (That’s how I would have coded), and with an IDE, I will just crank that out quickly with curly brackets and all. The problem arises (sometimes) when I look back at the code, and find it hard to navigate. Hence the comparison.

  4. Jonathan Giles Says:

    So your defence is that by writing crappy Java code you’re going to struggle later on to understand it? I don’t think Ruby will solve this problem for you. This is universal, and should be mitigated by good variable names, documentation where documentation is necessary, and most importantly, not caring about succinctness.

    My point is, I get the feeling that sometimes Ruby programmers try to fit too much on a line. Java can do the same, but it’s less prevalent. My case in point, lets rewrite your ‘bad’ Java example in what I would deem ‘Ruby-esque’:

    return s == null ? false : !s.equals(“”);

    I think you’d agree that is no better than the ‘proper’ answer that I provided, but it’s a hell of a lot tougher to understand.

    So, to close – what exactly is ‘wrong’ with the ‘bad’ Java example you pose – it’s instantly readable. My personal opinion on much Ruby code is that it isn’t always immediately readable like this, but that it is (in the worst case) just as decipherable as my Java example above.

  5. Looking for a fight… at Jonathan Giles Says:

    […] a teeny bit of a discussion on another blog last night, and I think I should provide a bit more of a discussion on it here. Basically, the premise of the […]

  6. Rob Says:

    Obligatory pythoners view… 🙂

    In Python, “” and None both evaluate to false in a boolean comparison… this makes things quite simple in most cases (and you can always check more explicitly for None with s is None)


    # Explicit casting to booleans...
    bool('')
    >> False
    bool(None)
    >> False
    bool('banana')
    >> True

  7. Thong Kuah Says:

    @Jonathan: To explain further about “how the language construct will fit with your expectations “, I personally don’t think that “”.equals(s) is a good idea, as “”.equals(s) is the opposite intent of what I set out to accomplish, which is s.equals(“”).

    As with the example that I posed, it is not good because I do not care if s is null. Looking back at the code, I will wonder what was I trying to do
    here? Was I looking for nulls as well? Why should I?

    As you stated s == null ? false: !s.equals(“”) is no better and I agree. That was the point of the post, s.blank? ( and “”.equals(s) ) is more understandable.

    @Rob: thanks for the pov of python. It is interesting how casting is used is determine truth. Is that a standard way in Python? just curious.

  8. Stephen Judd Says:

    Just thinking about the common case, web forms, and Python, something I do frequently is take advantage of Python’s idiom for dealing with dictionary keys that may or may not exist.

    >>> form = {} #empty dictionary
    >>> form[‘name’] #blows up because there’s no key ‘name’
    Traceback (most recent call last):
    File “”, line 1, in
    KeyError: ‘name’
    >>> form.get(‘name’) # returns None
    >>> form.get(‘name’, ‘anonymous’) # returns specified default value
    ‘anonymous’
    >>> form.setdefault(‘name’, ‘bozo the clown’) # returns default value AND creates new entry
    ‘bozo the clown’
    >>> form
    {‘name’: ‘bozo the clown’}
    >>>

Leave a Reply


http://www.canakkaleruhu.org http://www.vergimevzuati.org http://www.finansaldenetci.com http://www.securityweb.org http://www.siyamiozkan.org http://www.fatmaozkan.com http://www.sgk.biz.tr http://www.denetci.gen.tr http://www.bagimsizdenetim.biz.tr http://www.mevzuat.biz.tr http://www.security.biz.tr http://www.sorgulatr.com http://www.kanunlar.biz http://www.prsorgu.net http://www.sirabul.com http://www.emekliol.org http://www.coklupagerank.com http://www.coklupagerank.net http://www.coklupagerank.org http://www.prsorgu.org http://www.scriptencode.com http://www.sirabul.net http://www.sirabul.org http://www.sitenizanaliz.com http://www.seoisko.com http://www.seomavi.com http://www.scriptencode.net http://www.scriptencode.org