Tuesday, October 12, 2010

About C / C++ / C#: A Fun Week - Not!

If you can't see this email, click here

About.com

C / C++ / C#

Fundamentals

Tips & Tools

Going Pro



From David Bolton, your Guide to C / C++ / C#
Last week saw me maintaining some existing bad code and trying to cope with a number range that was sparsely populated and yet had to be extended. It was a thorny problem but one I eventually cracked. Details below under Evils of Number ranges. Congrats to Pedro for his superbly fast answer to Contest 39. It was nice to have 13 entries. We've already got two for Contest 40. Back next week!

Final Positions for Contest 39
Congrats to Pedro Graca whose ultra fast time was never going to be beaten. Thank you to everyone who entered. For once I got all entries built and run without any problems after the resubmitted C. Thanks Dinh! It's quite remarkable that the timing difference between 4th and 9th positions was only 0.0000207027 of a second. There were 6 entries all in that time scale. As usual, all were built for release and run from the command line. The source code files for all 13 are now live as well.

The Evils of Number ranges
I've been maintaining an application that uses a particular database table. Each day some new rows are created and a few older ones deleted. Unfortunately the index for the table is made up of several different types of entities and each has a different possible range of integer values; it's possible that even though they're not all currently used they could be. Type a has 0-78,000, type b has 100,000- 90 million and type c has 100-999 million. To unify these easily, a previous developer came up with a scheme where an abstract member function GetNum(), was overridden for each of three types. Effectively these added a billion so the range of values was 1 billion up to 1.999 billion (with lots of gaps). There was a reason for doing this, because entities of the three types could be mixed in with this combined type. Messy? Yes. Difficult to maintain? Extremely. Now it's been proposed to add a new type based upon the combined type but with another offset, however, it's not just a case of adding another billion. In theory it could be done as the unsigned range of 32 bit indexes is 0 to 4 billion. But even though there are no negative values, the code was written using int as the fundamental type so only the available values are 0-2 billion and the third type would push past the 2 billion max int value (INT_MAX in limits.h) code and database table and add another column to indicate the underlying type but managed to get past this limit by passing a string instead! Yuck! Using number ranges to classify types, while in theory a good idea is a time bomb. It's much better and generally more efficient to use a distinct value. Then all you need are a couple of mapping member functions between the range and the entity values.

List of Online Repositories and Pastebins
There seems to be so many such sites and paste bins that I thought I'd list them on a page and update as I go along. Interestingly some of them let you compile the code, one in over 40 languages! If you know of others that should be listed here, please email me at cplus.guide@about.com and I'll keep the page updated.

Development Projects from Initial Design to Completed Code
Each project is a complete application with notes on the requirements, the design, the architecture, code and final project review. With full source code in C, C++ or C#.

 


C / C++ / C# Ads
Featured Articles
Reviews of Software - Tools, Compilers, Editors etc
Book Reviews
Top Tools, Utilities and Resources
Glossary of Programming Terms
Welcome to the C++ Area with Articles about C++ Development
Other Blogs You might find Interesting

 

More from About.com

Is it a Cold or the Flu?
Knowing whether you have a cold or the flu can help you treat your symptoms more effectively. More >



Concerned About Your Drinking?
This short quiz can help you identify whether or not you have a drinking problem. Take the quiz now >




This newsletter is written by:
David Bolton
C / C++ / C# Guide
Email Me | My Blog | My Forum
 
Sign up for more free newsletters on your favorite topics
You are receiving this newsletter because you subscribed to the About C / C++ / C# newsletter. If you wish to change your email address or unsubscribe, please click here.

About respects your privacy: Our Privacy Policy

Contact Information:
249 West 17th Street
New York, NY, 10011

© 2010 About.com
 


Must Reads
Programming Challenges
About C, C++ and C#
C++ for Beginners
C++ Programming
Intro to OOP

Advertisement

No comments: