But was it intuitive ??

yesterday I asked people what does the code 2 < 1 < 5 produce. 

Much to my amazement everyone seemed to get it right !!  I must admit when the syntax was pointed out to me just the other day I really didn’t expect to see what I saw. Admittedly, the syntax I used was low < x < high, still it wasn’t until I fed it values for x above high or below low that I realized the syntax was doing other than I what I expected.  Yes it was always True, regardless of the value of x.


But by my recent informal blog survey everyone got it right, so that possilby means either (1) it was intuitive, (2) people knew to expect the unexpected due to the way I asked the question (3) the sample set was too small to tell, and/or (4) frequent readers of my blog are the most intelligent coders there are so just because they could understand the code doesn’t mean the average coder could ๐Ÿ˜‰

So I’m thinking it’s a combination of (2) , (3) and (4), and that the syntax is NOT intuitive <g>


So perhaps a follow question(s) should be:
(a) would you expect that result when you see If low < x < high Then, or would you expect that to test if x is in the range from low to high ?
(b) If you found code like this in existing VB production code would you leave it there ?  If not, what would you say/do to the coder who wrote it ? If so,why would you use that syntax ?



7 Comments so far

  1.   Ayende Rahien on June 29th, 2006          

    No language that I know of can support this syntax, so it was obvious to me that it was something like ( (2< 1) < 5 ), and then I just resolved those on my head. The Option Strict Off was the tip for me, though. It made me realize that there was implicit type conversion there. I showed the code to another guy, and he didn't figure it out. Beside, I would assume that anyone interested enough to read blogs is someone who care about development, so you get higher average. And no, it is not intiutive.

  2.   Eddie de Bear on June 29th, 2006          

    lol, is this where we drop the “C# doesn’t have this problem” line??

    On a seious note, if I saw code like this in code, then I would definately be spending a minute or two with the developer who wrote it. Code should be easy to read, for everybody, regardless of their experience.. Becuase of this, try and limit code to 1 STATEMENT PER LINE!!!! If you can’t do this, refactor, make the statement a new method (function/sub), named appropriately..

    Sorry about the rant…

  3.   bill on June 29th, 2006          

    Hi Ayende,

    Well just to prove that the readers of my blog are the smartest, you answered my next question before I even asked it ๐Ÿ˜‰

    the preferred syntax for that code (if you were actually going to use such a statement) would be:
    If (2< 1) < 5 Then The ()'s make a world of difference in whether or not it's intuitive. as such the IDE should ensure they are there.

  4.   bill on June 29th, 2006          

    hey Eddie,

    LOL, your too nice. I think I’d be saying something along the lines of “WTF ….@###”
    On a good day i might say subtle thigns like, “how about we make that “If True Then” ๐Ÿ˜‰

  5.   hurcane on June 30th, 2006          

    There are other examples where “if” statements that seem to be intuitive really aren’t.

    Here’s an intuitive statement I’m sure we would all like to be able to write…

    If SomeString = “999” Or “887” Then

    When I read it, I know what it means. But the computer doesn’t see it the same way. Option strict has an effect here. If it is off, this compiles and will run without any errors, other than unexpected results.

  6.   Tim on June 30th, 2006          

    Code like this really worries me, but it worries me even more that programmers do not understand how the compiler interprets such code. If I saw code like that in a real application, I would have serious concerns about the skills of the developer.

    The statement “2 < 1 < 5" and "(2 < 1) < 5" are identical statements and should produce identical results because of the "operator order of precedence" and automatic data type conversions imposed by the Visual Basic compiler. Unlike C/C++/C#, Visual Basic processes all operators at the same precedence level from left to right all the time. In this case, the compiler will always process "2 < 1" before it even thinks about "? < 5". Since "2 < 1" is a Boolean expression, it will return a Boolean data value (False in this case). So the entire statement becomes "False < 5". Visual Basic will convert the Boolean back into an Integer for comparison "0 < 5" and evaluate the new expression. In this case, the result is True. If the expression had been "2 < 1 < -5", the entire expression would return False, since "2 < 1" (or False or 0) is not less than -5.

  7.   bill on June 30th, 2006          

    Hey Tim,

    Re: ” Unlike C/C++/C#, Visual Basic processes all operators at the same precedence level from left to right all the time. ”

    Actually, that’s incorrect. VB has specific operator precedence rules. 2 + 5 * 10 is 52, not 70. See:
    for the details.