2

After getting in a discussion about the iOS Crypto Flaw also discussed on Ars Technica, someone mentioned that they encountered a case where, the line following a bracket-less if expression, was treated as an else.

if (<condition>)
   <expression A>;
   <expression B>;
<expression C>;

So, according to the person, expression B would be skipped if the condition was true, as if the else is implicit and unnecessary.

This in contrary to anything I've heard - my experience has been if the condition is true, then all three expressions would be executed - but seeing as I am a pro-bracket advocate, my experience may be limited, so I was hesitant to completely call the person out. Instead, I spent about 10 minutes clarifying and making sure I correctly understood what they were saying :)

Is there any truth in what they said? What language?

I'm pretty sure all major scripting languages follow my understanding. I think the person has a greater background in Objective-C. But if this is true, it could certainly be greatest reason to always use brackets.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
vol7ron
  • 40,809
  • 21
  • 119
  • 172
  • This may be too broad and perhaps not the right forum to ask the question, but I chose SO because it was so specific to `if/else` structures – vol7ron Mar 04 '14 at 17:22

1 Answers1

1

That would be highly illogical and I strongly doubt any language with such syntax rules would gain much support (although the languages I choose to use all have syntax I'd prefer were different, so it's certainly possible).

Any possible way (I could think of) of implementing this would result in either seemingly inconsistent behaviour when looking at other syntax rules in the language, or very restricting in terms of what's possible, and, either way, initially detrimental to readability - if it has the same indentation, how would you then have an if-statement with multiple lines in its block, or would this be disallowed? If the else part would have reduced indentation instead, how would you differentiate between code that is part of the else, or code that is just after the if-statement, or must every if-statement have an else part? And multi-line else would also be a problem either way. If it's purely based on when there is or isn't curly braces or similar, this will just be really confusing to start.

That said, there's also esoteric languages, which is really has an "anything goes" motto.

An esoteric programming language (sometimes shortened to esolang) is a programming language designed to test the boundaries of computer programming language design, as a proof of concept, or as a joke.

I wouldn't be surprised, nor would it be particularly meaningful, if there were an esoteric language with such syntax rules.

An alternative is perhaps that the person was looking at a situation like this:

<type> someFunction()
{
   if (<condition>)
      return <expression A>;
   return <expression B>;
}

This would be synonymous to an explicit else before the second return statement, as the return statement in the if-statement would cause the return statement following it to only be executed when the condition is false.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
  • 1
    Your example is what I initially thought the person was talking about, which is both common and practical. When they mentioned that `expression B` could be skipped if the condition was `true`, that's when my ears perked. I was thinking that Python, or a structure-based language, might use indentation to treat the following indented line as an else statement, but no language comes to mind. – vol7ron Mar 04 '14 at 17:29