Live data from Hacker News

Yoda Conditions

en.wikipedia.org

61–70 of 83 posts

Re: Yoda Conditions

#61
I didn't know abut "Yoda conditions," and thought it was going to be those "reversed" if statements that were so startling when I'd started coding in Perl.

      $days = 28  if $month eq "February";

Re: Yoda Conditions

#62

In PHP I like using Yoda Conditions because there's a common idiom of testing assignment in the conditional: if ($value = getSomeValue()) { // Safely use value } Yoda Conditions defend nicely against accidents when '=' and '==' can be used legally this way and honestly you get used to reading them pretty quick.

I don't see at all how you could call this a Yoda Condition: it couldn't be written any other way, as it would cause a syntax error.

Re: Yoda Conditions

#63
post #40
post #36

Earlier quoted context omitted.

One of the worst parts of Ruby. I can see the argument that it could be better to have save if valid rather than if valid save end but what I see 9 times out of 10 is things.do each |thing| foo bar baz end if valid or really.long.thing.that.i.try.to.parse.in.my.head if acutally_almost_never_happens which is harder to read since I read top to bottom / left to right, but the flow is bottom to top and right to left

You can write gibberish in any language. You shouldn't kill useful features because they are sometimes misused.

Not even PHP's "register_globals"?

Re: Yoda Conditions

#64
I was once officially reprimanded for using Yoda conditionals. That was at the same place that required this:

  if( booleanVariable == true )
  {
      ...
  }
  else if( booleanVariable == false )
  {
      ...
  }
  else
  {
      // Typically, this section would include an exact copy
      // of one of the above sections, as it was 3 years ago,
      // presumably to pad out the SLoC metrics.
  }
That place had its head so far up its own ass....

Equivalence is commutative. Please never complain about the order of its operands as "confusing" or "less readable". It just tells me that you're an ass, trying to enforce a coding convention that has no objective reason to exist.

Re: Yoda Conditions

#65
post #16

Earlier quoted context omitted.

The real lesson is for language designers, who for some reason love using equals for assignment. Lisp got it right a hundred years ago and nobody learned.

Well, languages can either use another assignment operator or have type system that wont check that kind of statement. Both ways fix it. In fact, it's not easy to have this kind of bug on your language. C just has it because people wanted to write stuff like `int a = b = 0`.

`int a Programmers using equals for assignment is a bit of a misunderstanding of what the mathematical idiom "let x = 5" means. The assignment is signaled by "let", not by "=". In fact, several programming languages use "let" exactly for this purpose too.

Re: Yoda Conditions

#66
post #20
post #16

Earlier quoted context omitted.

The real lesson is for language designers, who for some reason love using equals for assignment. Lisp got it right a hundred years ago and nobody learned.

There is nothing wrong with using equals for assignment, the language designers just have to disallow assignments in if conditions.

There's nothing wrong with using assignment in if conditions. For example, `if let Some(thing) = computation_that_may_fail { ... }`.

Re: Yoda Conditions

#68
I'm fond of Yoda conditionals, but not for the commonly cited reasons. Mostly I just like that they promote the interesting/unpredictable part of the comparison to the front. I consider the safety from unexpected assignment, from null pointer exceptions, and the Star Wars reference all to be minor perks.

Take this example:

  function handle_request($http){
    if("OPTIONS" === $http.request.method){
      ...
    }else if("GET" === $http.request.method){
       ...
    }else if("PUT" === $http.request.method){
      ...
    }else if($http.request.method === "POST"){
      ...
    }
    ...
  }
Did you read $http.request.method four times? I bet not. Some of you may have read it the first time; I wouldn't have. But to know that the fourth block handled POST requests I made you read to the end of the line. That makes it harder to scan the code looking for the section that you want to change. In production code that I've seen, the predictable side of a comparison is often quite long.

What's especially bizarre to me is the reason people give when advocating writing in the last style. Yes, it verbalizes nicely as "Otherwise, if the http request method post, then..." But who internally translates their code to English to understand it? COBOL was designed to read like natural language. Do you enjoy programming in COBOL? If English-like syntax is desirable for code readability, why don't modern programming languages prioritize English-like syntax, too?

Greek mathematics got stuck because it remained a verbal, pictorial activity, Moslem "algebra", after a timid attempt at symbolism, died when it returned to the rhetoric style, and the modern civilized world could only emerge —for better or for worse— ... thanks to the carefully, or at least consciously designed formal symbolisms that we owe to people like Vieta, Descartes, Leibniz, and (later) Boole.

-- Dijkstra,

Re: Yoda Conditions

#69

Earlier quoted context omitted.

The trick saves the person writing the code and anyone modifying it down the road. You can't force everyone who edits your code to have linters and warnings turned on. Not everyone follows bests practices.

Does it? You also can't force everyone who edits your code to follow your conventions.

actually you can, depending on the management structure in an organization :)

Re: Yoda Conditions

#70
post #3

Don't do this. In languages where accidental assignment is possible (i.e. writing if(a=b) when you meant if(a==b) ), configure the compiler or linter to emit a warning in this situation. For example in C, GCC will complain about this when compiling with -Wall, which you should be using anyway.

Counterargument: Do do this if the coding standards for the language/framework you're working in require it. Like WordPress.

Wordpress is a legacy procedural codebase and should not be used as an example of good development practices.
Post reply on HN