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.

23 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
  3. Really got a useful blog to read today. Its very informative, keep posting more like this. picbear

    ReplyDelete
  4. Useful stuff. Do you find this article about persuasive essay outline useful?

    ReplyDelete
  5. Multiple inheritances are a function of some item-orientated computer programming languages wherein an item or magnificence can inherit characteristics and features from a couple of parent objects or determine Auto Repair Workshop in Dubai class. This could be addressed in various approaches, along with the usage of virtual inheritance.

    ReplyDelete
  6. This Blog Is What I Used To Be Searching Out. This Piece Of Content Material Will Certainly Assists Me. Thank You For Sharing It. visit my site: Air separation unit suppliers

    ReplyDelete
  7. Thanks for sharing the informative post. If you are looking the Linksys extender setup guidelines . so, we have a best technical expert for handlings your quires. for more information gets touch with us. If you would like to get much from this piece of writing then you have to apply these techniques to your won website.
    스포츠토토

    ReplyDelete
  8. Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post
    한국야동

    ReplyDelete
  9. This is very Interesting & Informative content, There is also something to share with you guys, Off Track Garage Door Avondale AZ
    you guys try it for best garage services.

    ReplyDelete
  10. You're so interesting! I don’t think I've truly read through a single thing like this before. So wonderful to discover another person with genuine thoughts on this subject. Seriously. 야한동영상

    Also feel free to visit may webpage check this link
    야설

    ReplyDelete
  11. Many thanks for starting this up. This website is something that is required on the web, someone with a bit of originality. 일본야동

    Also feel free to visit may webpage check this link
    한국야동

    ReplyDelete
  12. Very good article! We will be linking to this great content on our site. Keep up the good writing 국산야동

    Also feel free to visit may webpage check this link
    야설

    ReplyDelete
  13. What an interesting story! I'm glad I finally found what I was looking for Excellent reading 중국야동넷

    Also feel free to visit may webpage check this link
    야설

    ReplyDelete
  14. Bugs should be taken care just like companies did for RFID injectable transponders to avoid mistakes

    ReplyDelete
  15. "Hello, I am one of the most impressed people in your article. I'm very curious about how you write such a good article.
    CLICK IT: 룰렛사이트탑

    ReplyDelete
  16. Amazing and Informative Post. Keep Writing and inspiring.
    Insane Growth Hair Oil

    ReplyDelete
  17. Wonderful post. Thank you for such an informative and unique Blog.
    Best Face Wash in Pakistan

    ReplyDelete
  18. Great Blog. Your article is amazing and inspiring. Keep writing and inspiring.
    Hair Oils

    ReplyDelete
  19. Amazing blog! very useful and informative blog.
    Best Anti-aging Cream in Pakistan

    ReplyDelete
  20. This website always provide very helpful blog.
    stamped concrete

    ReplyDelete
  21. So, when it comes to C++, we all know how easy it is to overlook those sneaky typo bugs, don't we? It's as if they're hiding in plain sight! But here's a neat trick: always proofread your code twice and, if possible, have a fresh pair of eyes look it over. It's like a breath of fresh air for your coding journey, believe me. And, hey, if you ever need assistance with a topic, especially if you're looking to buy assignment UK or need advice.

    ReplyDelete