Friday, 22 April 2016

Avoid C++ Typo-bugs with this One Cool Trick!


Recently I found an online quiz created by a company which develops some static code analysis tool. You were presented with buggy C++ code which the tool had detected and you had to click on where the bug in the code was.

The large majority of the bugs in the questions were down to typos. These normally occurred when someone had written the same line of code 3 or 4 times, but with different attributes (e.g. .x, .y and .z for a vec3)

Well, here's a neat trick that will allow you to avoid such typos. In the following example I recalculate the min/max values of a bounding box given a vector (pos):

            /* Iterate the X, Y and Z attributes and assign them if necessary */
            auto attrs = { &Vec3::x, &Vec3::y, &Vec3::z };

            for(auto& attr: attrs) {
                if(pos.*attr < bounding_box.min.*attr) bounding_box.min.*attr = pos.*attr;
                if(pos.*attr > bounding_box.max.*attr) bounding_box.max.*attr = pos.*attr;
            }


By using C++11 initializer lists, and auto, you can elegantly create a list of pointers to member attributes. C++11 range-for loops allow you to iterate them easily without a bunch of type definitions, and then you can just apply the pointers to the two vectors. This ensures that you never accidentally check the value of .y against .x or something like that. It's also fewer lines of code than repeating the if checks (4 vs 6). You could also tidy it up more if you create some extra reference variables in the loop. Win.

2 comments:

  1. Thanks for sharing This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality information for free. best resume writing service share and keep on sharing

    ReplyDelete
  2. Nice share, really interesting and can help me deal with the problem on my own now. thanks alot for sharing the post with me i will share it with my friends now..

    ReplyDelete