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)


  • Winner of the 2016 Presidential Election

    @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



  • @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.



  • @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".

     


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.