If Only



  • I'm just wondering if it's healthy for me to have this slight distaste for if-statements. I see "natural paths" as prettier than "if this then do that else do such".

    I mean, look at a ballpoint pen. It's got a rotating cylinder with protrusions that push out or hold in the cartridge. Every click just rotates the cylinder, and the pen's state is inverted.

    There's no manual intelligent checking:

    //not sexy (despite appearances!)
    onclick {
    if (state = 'inside') pushOut();
    elseif (state = 'outside') pullIn();
    }

    But it's all automatic:

    //sexy
    onclick {
    state = !state
    changeState(state);
    }

    No semi-intelligence, just the input doing something different based on the "hard" state of the machine. If-statements are "soft".

    I also don't really like loops with a big if statement in 'em.

    I have no background or education in CS, and have read very little about automata or state machines, so my terminology and explanation may be a tad noobish.

    Can I be saved, or are my goals noble?



  • So, you get handed a retractable ball-point pen to write with. Chances are you assume it's in the "retracted" state and click to change state to "ready". Quite often you guessed wrong, and have to click it again. Bad OMI (Operator Machine Interface).

    Even worse with a complex piece of software with hundreds of states.

    "The road to hell is paved with good intentions."


  • ♿ (Parody)

    I'm not 100% I understand, but it seems to me like you're adding more code than necessary. Consider what your changeState function *must* look like:

    changeState(state) {
     if (state=='inside') pushOut();
     elseif (state=='outside') pullIn();
    }



  • I was more looking for discussion on a theoretical subject, rather than code help for something that doesn't exist. :)




  • Since you said you love haskell, let me try to show that this can be done in haskell without an "if": (Disclaimer: I don't really know haskell, just doing copy-paste from Wikipedia)

    click :: BallPointPenState -> BallPointPenState
    click inside = outside
    click outside = inside


    wow, isn't that great?



  • @ammoQ said:

    Since you said you love haskell, let me try to show that this can be done in haskell without an "if":

    click :: BallPointPenState -> BallPointPenState
    click inside = outside
    click outside = inside


    wow, isn't that great?


    <pedantic>Almost... that should be

    click :: BallPointPenState ->
    BallPointPenState
    click Inside = Outside
    click Outside = Inside


    Note the capital letters: variable names start with lower case letters, whereas data constructors (in this case, roughly equivalent to constants, although they can also take parameters) start with capital letters.  Your version would mean "bind the argument to the name <font face="Courier New">inside</font> and return the value of <font face="Courier New">outside</font> (presumably bound at the top level), and the second case would be ignored.  What you want is "if the argument is <font face="Courier New">Inside</font> return <font face="Courier New">Outside</font>, if it's <font face="Courier New">Outside</font> return <font face="Courier New">Inside</font>".
    </pedantic>

    @ammoQ said:
    (Disclaimer: I don't really know haskell, just doing copy-paste from Wikipedia)


    Well, now you know it a little better.  ;-)



  • @iwpg said:



    Well, now you know it a little better.  ;-)


    Thank you for the lesson, master. bowing



  • @dhromed said:


    //sexy
    onclick {
    state = !state
    changeState(state);
    }

    Problem is you can't really do it that elegantly, if state is some kind of enumeration or integer type.

    You'll have to fall back to either

    state = state == inside ? outside : inside;  // not so sexy

    or

    state = 1 - state;     // even a little repulsive

    Other than that... yeah, I agree, I would probably do it the same way. Even one line shorter:

    changeState(!state)

    ;)



  • truely, the most elegent is

    onclick() // mention RiX0R
       {
          changeState(!state)
       }

    however this might allow the testing for multiple positions

    onclick(pos)
       {
          changeState(++state mod pos)
       }



  • @RiX0R said:

    state = 1 - state;     // even a little repulsive



    I disagree. I see beauty inside tricks like these. (Except if the language does auto-conversion of the boolean state to an int 0 or 1, in which case it's real filthy.



    @RiX0R said:
    changeState(!state)


    Except that would only invert the state once.

    -------------------

    I just think it would be Really Good if I'd write code as though it was a bit of hardware; a piece of ciruitry. The impulse (i.e. the execution control) simply passes through its only possible path towards the script's end, unencumbered by value checks.

    -------------------

    Since you said you love haskell,


    How would I start with Haskell? I wonder. I mean, starting with JS is dumbfoundingly easy: just plop a script tag in a page, save it as .html and open in your browser. Instant compiler/interpeter/execution(er).

    But are there such easy ways for languages like Java, C++, etc? Quick runtime environments that take small scripts and run 'em?


Log in to reply