16

Just because you can do things in one line and throw in ternary operators everywhere, doesn't mean you should.. other people will touch your code. Make it readable instead of making it harder to maintain

Comments
  • 1
    I agree, but sometimes, ternary operators are more readable
  • 1
    Depends on the language.

    I personally don't think that

    if a:

    x = y

    else:

    x = z

    is inherently more readable than

    x = y if a else z

    Of course that is Python and readability is somewhat inbuilt in that language.
  • 0
    @david-hil as long as you don't nest ternaries like a madman they tend to be more readable than if/else
  • 0
    @desirous I have no problem with reading them. Don't be so condescending.. It's not a good look.

    Complex operations should be broken out so the logic is simpler to follow. That's my point. Easier to read, less of a headache to maintain.
  • 0
    I prefer to write them on three lines if they are a bit more complex and find them better to read :)
  • 0
    @desirous the code I'm referring to has nested linq statements in ternary form. I had to break it into several lines to make it readable, because the guy felt it necessary to put it all on one line.
  • 0
    @desirous yeah.. I don't mind ternary if it's straightforward.
  • 1
    C had ternary decades before non-binary became a thing.
Add Comment