C++ Coding Standard: The If Block

Almost two months ago I went to a CocoaHeads meeting during Macworld. They had Mike Lee talk. His presentation was about “Pimping Your App”. There were a bunch of interesting points, but one thing really stuck in my head. Mike was talking about how is a messy person in his life. His car is messy. His room is messy. His desk is messy. Everything is messy, except his code. HIs code is crystal clean, squeaky even. As a programmer you need to make sure your code is consistent and clean. Ever since I have been thinking about standardizing the way I write code. This is the first post in hopefully a stream of posts about quality code. ...

March 10, 2009 · zacharyzacharyccom

Stupid std::vector Class

The Standard Template Library in C++ is nice to provide us with a bunch of different container classes so we don’t have to re-invent the wheel every time we write new code. One of the classes is called “Vector”, if you aren’t familiar with it, you might not get too much out of this post. But basically, it is a dynamically growing array. Meaning it has contiguous memory and can be indexed like a regular array. It’s a great class and I use it all over the place, but for the second time in one week I find it lacking. ...

January 21, 2009 · zacharyzacharyccom

Leap Year Spells Trouble for Zune Users

On December 31st, all 30GB Zune users woke up to their music players not working. In a rarity for Microsoft problems, the source for for the problem was found. There is a good explanation of the problem here. There are two lessons to be learned from this: 1) be careful of your looping conditions. 2) Try and write your code in small snippets that are testable, and write tests! A simple iteration through the total amount of days including a leap year would have caught this bug. ...

January 2, 2009 · zacharyzacharyccom

The Address of Monkey

Have you seen the following C code sample: 1 2 char x = 1; char c = x["monkey"]; Do you know what it the value of c is? Don’t read on unless you want to know the answer and why. The value of c is ‘o’. Why? Well, I wrote some code to start playing around with this. The answer seemed simple, but here were some suggestions about why the answer is ‘o’: ...

September 6, 2008 · zacharyzacharyccom

Why 2 can sometimes equal 1

Ran into a fun situation today where I was writing some code, and I came across an interesting situation in C++. Now, before I get to the end of this post, I’ll give you the punch-line, Developer stupidity. So I was working on a exercise where I needed to write some sort of state machine. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 enum STATES{ STATE_1 = 0, STATE_2, STATE_3, ... }; /* ... some other code ... */ switch(state) { case STATE_1: // Do something state = STATE_2; break; case STATE_2: // Do some other stuff state = STATE_3; break; case STATE_3: // Do the last state of stuff // This code never gets called. break; } In this code STATE_3 is never reached. The code for the enum was working fine, but the state wasn’t being reached. I went over this for a while, until I found out the problem. ...

August 2, 2008 · zacharyzacharyccom