Page 27 of 27 FirstFirst ... 17252627
Results 261 to 263 of 263

Thread: Programmer's Den

  1. #261
    Now we know... Asteroids Champion ACow's Avatar
    Type
    INTP
    Join Date
    Dec 2013
    Location
    Melbourne, Australia
    Posts
    2,840
    I admit a subjective distaste for ternary operators in general.

    I mean, everything's a function anyway, but then we gotta give ground for this stupid cultural thing we have with binary operators which means now we have to worry about PEMDAS and implicit order of operations and non simple evaluation rules and all that what not, and then after losing that battle someone's like "let's have a threesome!".

    I'm like a sweet little Christian boy who left the movie theatre because there was a kissing scene only to find someone's been shooting porno in my apartment.

    don't worry, its my problem, I'll deal with it...

  2. #262
    Senior Member
    Type
    intp
    Join Date
    Dec 2013
    Posts
    1,687
    Quote Originally Posted by ACow View Post
    I'm not a JavaScript person, so first I have to ensure my interpretation is correct, and it might be that some of the edge cases don't apply to the language.

    my interpretation is that there's a block "do something". You execute this block if:

    a) somevariable is false

    OR

    b) somevariable is true, and the return value of setSomeValue() is true, assuming evaluation is lazy, and hence you also want setSomeValue() to only fire after the conditional check on somevariable is true, and that there's a possible distinction between complication between setSomeValue()'s side-effect, its possible state after the conditional check on somevariable, and its return value.

    Phew...

    That might seem pedantic to write it all out like that, but those are the conditions where I can see such code existing, because I agree that is general form has a bad code-smell about it.

    And if those relatively few conditions don't hold or aren't required, I can see there being so many simpler (albeit possibly slightly more verbose, but clearer) ways of telling the program to do something.

    And honestly, although I'm not a JavaScript guy, so take my opinion with a grain of salt here, if those conditions do hold, that suggests to me bad code smell in the programs design, since the number of times you'd actually want to have to think about state interaction and evaluation order like that should be extremely limited.

    I'm guessing that actually there is just a piss easy basic procedural code that gets you to the same place you need to be in practice.

    let me know how close I am to being right
    You're right, yeah. When I first saw this at work I didn't consider that setSomeValue() might have a return value. I think because the naming of it implies there shouldn't be one ("set"). Probably. But there could be... Further confusion. Is it supposed to have a return value? Does it return a true/false under different situations?

    I definitely prefer a bit more code if it means I can read it and understand it pretty easily and if length is the only negative factor.

    somevariable is true, and the return value of setSomeValue() is true, assuming evaluation is lazy, and hence you also want setSomeValue() to only fire after the conditional check on somevariable is true, and that there's a possible distinction between complication between setSomeValue()'s side-effect, its possible state after the conditional check on somevariable, and its return value.
    Hmm, setSomeValue() will definitely only fire after someValue is checked, and maybe not at all (if it's false). It could alter the value of someValue, but only after it's already been used in that conditional check. JavaScript is very focused on performance, because it's got to both compile and run in front of the user, so any code that doesn't need to run, will not (if it can be detected).

    Quote Originally Posted by ACow View Post
    I admit a subjective distaste for ternary operators in general.

    I mean, everything's a function anyway, but then we gotta give ground for this stupid cultural thing we have with binary operators which means now we have to worry about PEMDAS and implicit order of operations and non simple evaluation rules and all that what not, and then after losing that battle someone's like "let's have a threesome!".

    I'm like a sweet little Christian boy who left the movie theatre because there was a kissing scene only to find someone's been shooting porno in my apartment.

    don't worry, its my problem, I'll deal with it...
    Ah, I like ternary operators in some situations! Setting values, they can be a lot cleaner. It's got to be a very basic condition though.

    Otherwise, just make it a proper fucking if/else.

    I once came across something like:
    b ? c ? d : e : f
    i.e. nested if conditioned two or three levels deep, as ternary operators, on one line. Each a decently complex statement to also comprehend. Completely unreadable. I stripped that fucker out of there.

  3. #263
    schlemiel Faust's Avatar
    Type
    INTP
    Join Date
    Dec 2013
    Posts
    3,069
    Quote Originally Posted by scarydoor
    You're right, yeah. When I first saw this at work I didn't consider that setSomeValue() might have a return value. I think because the naming of it implies there shouldn't be one ("set"). Probably. But there could be... Further confusion. Is it supposed to have a return value? Does it return a true/false under different situations?
    It looks as though it should return a bool if successful, but you can't see the actual function? The author should have left a comment.
    "All my heroes are dead" - John Zorn

    "It's not selfish if you hate yourself"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •