Save some work



  • There must be a antipattern for this; does anyone know the name?

    if (this.somePropName != '') {

      this.somePropName = '';

    }



    After all, why set the value when it is already that value? Surely it must be faster to skip the assignment operator....



  • Well, if it's a language that allows it, they could have overloaded the = operator, and assignment is a very costly operation now, such as writing to a database.



  • It has many names. Defensive coding, paranoi, guaranteed-to-be-incompetent peers. ...outsourcing...

    It looks like C++, so somePropName could be a string. But there is still the possibility that someone brought their own version of String to the game... or just rewrote the stl. </sarcasm>

     



  • @Buttembly Coder said:

    Well, if it's a language that allows it, they could have overloaded the = operator, and assignment is a very costly operation now, such as writing to a database.

    Which kind of moron codes a language such that an assignment means an automatic write to the database?



  • @Rhywden said:

    @Buttembly Coder said:
    Well, if it's a language that allows it, they could have overloaded the = operator, and assignment is a very costly operation now, such as writing to a database.

    Which kind of moron codes a language such that an assignment means an automatic write to the database?

    It might not be automatic; it might only do the write if you first ran the != operator.



  • @OldCrow said:

    It looks like C++

    It's javascript, but the same thing can be found in many languages and code bases.



  • I'm a believer in the phrase "the best line of code is the one you didn't need to write"

    That is, the code like this:

    if (x != 3) {
      x=3;

    }

    has 2 lines of code; the equivalent code

    x=3;

    has one line of code. It's much easier to introduce a bug in the two-line version. What if I needed to check to ensure x is 4? If I change the 3 to a 4 on the first line, but not on the second, I've introduced a bug

    if (x != 4) {

      x=3;

    }

    whereas the single-line code can't really break:

    x=4;



  • @Rhywden said:

    @Buttembly Coder said:
    Well, if it's a language that allows it, they could have overloaded the = operator, and assignment is a very costly operation now, such as writing to a database.

    Which kind of moron codes a language such that an assignment means an automatic write to the database?

     

     Is there more than one kind?

    Well... I guess, if you want to split hairs, it is the kind of moron that is given opportunity. A moron that was in the wrong place at the wrong time... for the rest of us; I'm sure he was happy with his own handiwork. ...hypothetically speaking. I'm still hoping that it stays a purely academic question. ...But I fear the worst.

     

    Edit:

    Scratch that. Rule ...34?... if someone can think it up and can (at least in theory) code it, it exists and is online.



  • @Rhywden said:

    @Buttembly Coder said:
    Well, if it's a language that allows it, they could have overloaded the = operator, and assignment is a very costly operation now, such as writing to a database.

    Which kind of moron codes a language such that an assignment means an automatic write to the database?

      Larry Wall???

     



  • Which kind of moron codes a language such that an assignment means an automatic write to the database?

    Operator overloading has always been a bugaboo. It's intended purpose, of course, is to make an operation easier to write code for. You're supposed to preserve the semantics of the operator; so that when I write a = b, I know that I'm assigning some value (b) to a variable (a). There may be computation occurring there, or type conversion, or something. But there should not be stuff like database access, writing to a log file, playing music, etc.


    Although that sounds intriguing -- musicPlayer = "The Sound of Music Theme" rather than musicPlayer.play("The Sound of Music Theme"). Hmm, have to think about that.



  • @DrPepper said:

    Which kind of moron codes a language such that an assignment means an automatic write to the database?

    Operator overloading has always been a bugaboo. It's intended purpose, of course, is to make an operation easier to write code for. You're supposed to preserve the semantics of the operator; so that when I write a = b, I know that I'm assigning some value (b) to a variable (a). There may be computation occurring there, or type conversion, or something. But there should not be stuff like database access, writing to a log file, playing music, etc.

     

     What if given object really is backed by database? I mean it have almost no variables except DB connection, and every property read/write involve access to DB?

    It could be true even for JavaScript, with custom, possibly complex, setter.

     

    @DrPepper said:

    Although that sounds intriguing -- musicPlayer = "The Sound of Music Theme" rather than musicPlayer.play("The Sound of Music Theme"). Hmm, have to think about that.
     

    More likelly:

    musicPlayer.currentSong = "The Sound of Music Theme"

    ... will trigger searching for song of this name then play it.

     



  • @spamcourt said:

    What if given object really is backed by database? I mean it have almost no variables except DB connection, and every property read/write involve access to DB?

    It could be true even for JavaScript, with custom, possibly complex, setter.

    I can see it now...

    <font face="Comic Sans MS">variable.prototype["="] = function(newValue){
    $.post("./runsql?s=UPDATE+TABLE+SET+COLUMN1=" + newValue);
    }



  • @Buttembly Coder said:

    @spamcourt said:

    What if given object really is backed by database? I mean it have almost no variables except DB connection, and every property read/write involve access to DB?

    It could be true even for JavaScript, with custom, possibly complex, setter.

    I can see it now...

    <font face="Comic Sans MS">variable.prototype["="] = function(newValue){
    $.post("./runsql?s=UPDATE+TABLE+SET+COLUMN1=" + newValue);
    }</font>

    No, no, you have an SQL injection in your code. That should read:

    <font face="Comic Sans MS">variable.prototype["="] = function(newValue){
    $.post("./runsql?s=UPDATE+TABLE+SET+COLUMN1='" + addslashes(newValue) + "'");
    }</font>



  • If you know you are dealing with straight strings or fields/variables, then it is kind of pointless, but not too costly.  It would be cleaner to set the value directly.

    However, if you are unsure, you might want to do it this way.  You may have more code on the setter than just settigng a backing variable, sometimes a lot of code.  In several UIs now, setting certain properties will trigger other changes, possibly repainting a UI.

    Of course, if you are doing that (MVVM, etc.), then your setter is supposed to have that check in it so you don't go off and do a bunch of work if the value has not actually changed.

    Or, it could be someone doing copy-paste coding... "that is the way it was done over here...".  (Then again, who didn't do that at one point in their learning)


  • Considered Harmful

    @Buttembly Coder said:

    @spamcourt said:

    What if given object really is backed by database? I mean it have almost no variables except DB connection, and every property read/write involve access to DB?

    It could be true even for JavaScript, with custom, possibly complex, setter.

    I can see it now...

    <font face="Comic Sans MS">variable.prototype["="] = function(newValue){
    $.post("./runsql?s=UPDATE+TABLE+SET+COLUMN1=" + newValue);
    }</font>

    <html><body style='color:#000000; background:#ffffff; '>
    var foo = {};
    foo.__defineSetter__( 'bar', function( value ) {
        alert( value );
    } ); 
    foo.bar = 'Hello, world!';
    


  • Or

    var foo = Object.defineProperties({}, {
      bar: { set: function (value) {
        alert(value);
      } }
    });
    
    foo.bar = 'Hello, world!'
    


  • @DrPepper said:

    There must be a antipattern for this; does anyone know the name?
    if (this.somePropName != '') {
      this.somePropName = '';
    }

    After all, why set the value when it is already that value? Surely it must be faster to skip the assignment operator....
     

    Actually, for Javascript, the results of the two are NOT identical, because the code uses != rather than !==.

     != will do type coercion, which can do a bunch of wild and crazy things (especially for objects that overload toString). It's certainly possible that this.somePropName == '' but this.somePropName !== ''.


  • Discourse touched me in a no-no place

    @Rhywden said:

    Which kind of moron codes a language such that an assignment means an automatic write to the database?
    It seems to be quite a common concept with a certain type of coder. Exhibit #1: MUMPS


  • Discourse touched me in a no-no place

    @DrPepper said:

    There must be a antipattern for this; does anyone know the name?

    if (this.somePropName != '') {

      this.somePropName = '';

    }



    After all, why set the value when it is already that value?
    Seriously guys... I can't be the only one who noticed that it's setting the value if it's not already that value.


  • :belt_onion:

    @spamcourt said:

    What if given object really is backed by database? I mean it have almost no variables except DB connection, and every property read/write involve access to DB?

    It could be true even for JavaScript, with custom, possibly complex, setter.

    @DrPepper said:

    Although that sounds intriguing -- musicPlayer = "The Sound of Music Theme" rather than musicPlayer.play("The Sound of Music Theme"). Hmm, have to think about that.
    More likelly:

    musicPlayer.currentSong = "The Sound of Music Theme"

    ... will trigger searching for song of this name then play it.

    Think "Observer" pattern. You want to send triggers when the value of a property changes. When the value==3 and some code does value=3, you probably do not want to fire the trigger.

    I've seen this pattern in conjunction with INotifyPropertyChanged in c#. And I'm using it myself to keep information on a webpage directly synchronized with the database backend. In this case a textbox is bound to an Observable object (using knockout.js). To this observable I have subscribed a function that POSTs the new value to my webservice. I don't want to get notifications when a user overrides the existing value "3" with the same "3".

     



  • The level of WTFness depends entirely on how fast the comparison is done via the != operator, and how fast the assignment is done taking into account any side effects.



    Assuming no side effects on assignment then there really isn't any point in guarding against the string property already being empty given that actual string comparison is likely to be slower.



    Its not actually that bad though, as comparing against an empty string isn't ever going to take very long, since a maximum of one character will ever be compared (if that... empty string objects could well be handled by a special case under the hood of your interpreter, that doesn't even need to inspect the contents).



  • Perhaps there were more lines in the brackets that have now been removed. Check source control for changes.



  • @dkf said:

    @Rhywden said:
    Which kind of moron codes a language such that an assignment means an automatic write to the database?
    It seems to be quite a common concept with a certain type of coder. Exhibit #1: MUMPS
    Exhibit #2: Perl tie


    Maybe I was too subtle before when I mentioned Larry Wall.



  • @Buttembly Coder said:

    <font face="Comic Sans MS">

    Fuck you.

    Unrelated: CS blows goats. That turned all of the text on this page to Comic Sans. Hmmmm.....I has an idear.....



  • @mikeTheLiar said:

    @Buttembly Coder said:

    <font face="Comic Sans MS">

    Fuck you.

    Unrelated: CS blows goats. That turned all of the text on this page to Comic Sans. Hmmmm.....I has an idear.....

    ...What browser are you using?


  • Chrome Version 26.0.1410.64 m



  • @PJH said:

    @DrPepper said:
    There must be a antipattern for this; does anyone know the name?
    if (this.somePropName != '') {
      this.somePropName = '';
    }

    After all, why set the value when it is already that value?
    Seriously guys... I can't be the only one who noticed that it's setting the value if it's not already that value.
    No, the rest of us just understood DrPepper's Lonely Heart Club Band... rhetorical question.

    As in: (playing the part of the original coder) "why would we set the property when it is already that value? We shouldn't, so let's skip the assigment if it is"

     

    Edit: in your defense, I initially had to think twice too.



  • @bjolling said:

    Think "Observer" pattern. You want to send triggers when the value of a property changes. When the value==3 and some code does value=3, you probably do not want to fire the trigger.

    I've seen this pattern in conjunction with INotifyPropertyChanged in c#. And I'm using it myself to keep information on a webpage directly synchronized with the database backend. In this case a textbox is bound to an Observable object (using knockout.js). To this observable I have subscribed a function that POSTs the new value to my webservice. I don't want to get notifications when a user overrides the existing value "3" with the same "3".

    Good catch. Although I know that where I found the code, that it is really and truely just a plain old "x=y" statement; and that there is nothing nefarious going on with the assignment operator, your point is well made. When the assignment operator is overridden, there can be a difference.


    BUT I'd (hope) that when you're doing the INotifyPropertyChanged, that you've written the assignment override to use a backing variable; and have code in the setter to fire the notification only if the variable actually changed (so that the consumer of your property can just use "x = y" and it works correctly and does not fire a notification when the value is unchanged by the assignment operator.



    And for knockout, I'd really try to do the same thing -- its an error waiting to happen, when some properties can just use the "x = y", and others are required to do "if (x != y) x = y"



  • @Zecc said:

    As in: (playing the part of the original coder) "why would we set the property when it is already that value? We shouldn't, so let's skip the assigment if it is"

    You said it better, my friend. Remind me next time to read my posts before I push the button.



  • @dkf said:

    It seems to be quite a common concept with a certain type of coder. Exhibit #1: MUMPS
     

    That's amusing, because I actually *am* a MUMPS coder (though I think that article has many inaccuracies).

    MUMPS does allow direct access to its database just like it was a variable, so yes you can read and write the database by direct variable access.  At the same time, the syntax makes it pretty clear that you're dealing with a global (database) and not a local variable, so it's not a hidden side effect - it's the main purpose of the command you're executing.


  • Discourse touched me in a no-no place

    @Cat said:

    That's amusing, because I actually am a MUMPS coder (though I think that article has many inaccuracies).
    The funny part is that I (and other people on this forum I know for sure; we were at the same conference…) have seen systems that map an associative array variable to network operations so as to maintain a global shared state space across a network of application instances. It apparently works well for some people, but it scares me with all the potential for excrement/ventilator impact events implied by all that.



  •  Two reasons (I did not see previously posted)

    1) It makes it trivial to establish a breakpoint that only gets hit on an actual change in value.

    2) Assignments can be very expensive. The most common one is in VB (since forever) and even many other platforms, of writing to a controls properties. I was able to make significant (read 2x+) performance improvements my ensuring that assignments to Visible, Enabled, Text and similar properties only inside of that form of check.


Log in to reply