6

Hacky code post:
property var value: if(editing || !editing) function(othervalue)

I am coding in a property system that only updates an expression if a variable involved emits a signal. Well the function won't get called unless I change a value. I also want to "value" to be updated when editing changes. I also want it to update even if editing doesn't change. So the or-ing of the state of editing.

The result is the function gets called when object is initialized. Then when the editing flag changes it gets updated again. The workaround of doing this is much worse and requires more hacky code. So I am resigned myself to just or-negating the editing value.

Comments
  • 2
    Wow, that is awful.
  • 0
    I suddenly don't feel bad for

    //this will break the fuck out of everything if ran during tests - don't ask, just go with it.

    If(!config.testsAreRunning){
    // do this because it's an actual user
    }
  • 0
    This looks like a legitimate place for one of the most hard-to-write comments ever.
  • 0
    @C0D4 Ya need good mocks.
  • 0
    @Oktokolo The comment right before this line of code:

    // this next line of code is NOT a mistake
  • 1
    @Demolishun
    That isn't enough information.
    I don't know the language. But:
    The combination of expression result caching and reevaluation on change of participating variables is probably not what the average coder expects to happen.
    This probably triggers a WTF-is-going-on?! moment in whoever reads that code - including yourself one year from now...

    // The always-true check on editing triggers reevaluation of
    // the function whenever editing changes:
    property var value: if(editing || !editing) function(othervalue)

    This still isn't a really good comment, but it at least tries to explain, why the WTF expression is there and what the author expected it to do.
    If reevaluation of expressions is rarely used (not a common idiom), you might want to also mention, that change of othervalue also triggers reevaluation...
  • 1
    @Oktokolo After researching and seeing comments in posts on the forum for this language I am thinking this kind of hacking is common.
  • 1
    @Demolishun
    That is a bad sign. Run before the language ruins you forever.
  • 1
    @Oktokolo Well I use 3 languages at once: QML, Javascript, and C++. It is also my job, so no running away. QML is declarative and uses signals to notify of value changes. This is the part I am hacking. The rest is "okay".
  • 0
    If this is javascript, it sounds an awful lot like Adam Haile's S.js.
Add Comment