News for hackers. Stuff that matters.
Re:Yoda Conditions by geofft on Friday February 17, 2017 @12:06PM (#13669808)
I ran across a bug at $dayjob where someone was clearly trying to use Yoda conditions, but messed up differently:

    if "start" == argv[1]:
        ....
    else if "stop" == argv[1]:
        ....
    else if "reload":
        ....
    else if "status" == argv[1]:
        ....
I think it'd be harder to make this mistake with the conditions the right way around. (Also, never mind that the equality bug isn't even possible in Python.)
Re:Yoda Conditions by jquast on Friday February 17, 2017 @09:37PM (#13673319)
I wish python had case statements for this very problem. If the statement blocks can be defined by a common function signature, your example could use

  {'start':  fn_start,
   'stop': fn_stop,
  }.get(
    argv[1] if len(argv) > 1 else None,
    fn_undefined
  )(*args, **kwargs)
I used this in a demonstration terminal nibbles/worms video game clone, https://github.com/jquast/blessed/blob/master/bin/worms.py#L...