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.
Subscribe to:
Post Comments (Atom)
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
ReplyDeleteNice 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..
ReplyDeleteReally got a useful blog to read today. Its very informative, keep posting more like this. picbear
ReplyDeleteUseful stuff. Do you find this article about persuasive essay outline useful?
ReplyDeleteMultiple 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.
ReplyDeleteThis 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
ReplyDeleteThanks 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스포츠토토
Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post
ReplyDelete한국야동
This is very Interesting & Informative content, There is also something to share with you guys, Off Track Garage Door Avondale AZ
ReplyDeleteyou guys try it for best garage services.
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. 야한동영상
ReplyDeleteAlso feel free to visit may webpage check this link
야설
Many thanks for starting this up. This website is something that is required on the web, someone with a bit of originality. 일본야동
ReplyDeleteAlso feel free to visit may webpage check this link
한국야동
Very good article! We will be linking to this great content on our site. Keep up the good writing 국산야동
ReplyDeleteAlso feel free to visit may webpage check this link
야설
What an interesting story! I'm glad I finally found what I was looking for Excellent reading 중국야동넷
ReplyDeleteAlso feel free to visit may webpage check this link
야설
Bugs should be taken care just like companies did for RFID injectable transponders to avoid mistakes
ReplyDelete"Hello, I am one of the most impressed people in your article. I'm very curious about how you write such a good article.
ReplyDeleteCLICK IT: 룰렛사이트탑
Amazing and Informative Post. Keep Writing and inspiring.
ReplyDeleteInsane Growth Hair Oil
Wonderful post. Thank you for such an informative and unique Blog.
ReplyDeleteBest Face Wash in Pakistan
Great Blog. Your article is amazing and inspiring. Keep writing and inspiring.
ReplyDeleteHair Oils
Amazing blog! very useful and informative blog.
ReplyDeleteBest Anti-aging Cream in Pakistan
슬롯커뮤니티
ReplyDeleteบาคาร่า เล่นยังไง
ReplyDeleteThis website always provide very helpful blog.
ReplyDeletestamped concrete
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