Menu

Code style improvements

Andrei
2019-04-13
2019-04-27
  • Andrei

    Andrei - 2019-04-13

    Emanuele, regarding your commit [r949]: in some places you have passed float by const reference instead of value, as in:

    void function(float arg);
    void function(const float &arg);
    

    Unless you have measured performance benefits, please undo these changes: it is agreed[1][2][3] that there is no benefit in passing built in types (such as int, float, double) by reference instead of by value. The code merely becomes uglier to read and potentially more annoying to modify (what if we'd like to be able to modify arg inside the function as a temporary value?)

    Only pass by reference objects (such as std::string, std::vector<float>, a_big_struct, etc.) Or a better thought, only pass by reference things whose size in bytes exceeds the size of a pointer: on a 32-bit machine a pointer will be 4 bytes, on a 64-bit machine it will be 8 bytes; a float is typically 4 bytes on both 32-bit and 64-bit [4].

    [1] https://stackoverflow.com/q/4112914
    [2] https://scotchi.net/cpp-pitfalls/#references
    [3] https://stackoverflow.com/a/898844
    [4] https://stackoverflow.com/a/25524605

    EDIT: added an extra link.

     

    Related

    Commit: [r949]


    Last edit: Andrei 2019-04-13
  • Andrei

    Andrei - 2019-04-20

    I have reverted these changes in [r950] considering 6 days without a response.

     

    Related

    Commit: [r950]

  • Onsemeliot

    Onsemeliot - 2019-04-20

    Wouldn't it have been easier to just fix the data types? Or would that have messed things up?

     
  • Andrei

    Andrei - 2019-04-20

    Wouldn't it have been easier to just fix the data types?

    It was easier to "fix" the commit by reverting it entirely.

     
  • Emanuele Sorce

    Emanuele Sorce - 2019-04-24

    Hi! Sorry for the delay in reverting my mistake (@Andrei thanks for fixing it!) and for the mistake itself. I was absolutely sure it was better to pass by reference any type, but as you pointed, I was wrong.

     
  • Andrei

    Andrei - 2019-04-27

    @Emanuele: no worries, every C++ programmer makes this mistake at least once in life, I did too.

     

Log in to post a comment.