posted on
Saturday, June 07, 2008 8:27 PM |
I tend to like inline statements. Here is my new favorite. I even commented!
protected int calculateData(byte[] data)
{
// why the index++?
// I want index 1 and 2, since i++ increments it after i been used
// going int i = 1; Console.Write(i++ + "::" + i++);
// will "1::2"
return ((index + 1) < data.Length) ?
calculateData(data[index++], data[index++]) : 0;
}
Update: Why I love it and why I did this
It is heavily compacted. It is an inline if statement along with how I did incremented the index. It won’t compile if you just ran it since I overloaded calculateData and have a (byte, byte) overload.
if index started at 1, it would leave as 3 from the function. it calls calculateData like this, calculateData(1,2) and the next cycle would be calculateData(3,4).
So I use this function when I’m creating the ImuData return when I get data from the gyros and accelerometers. I have a lot of data and the packets need to be parsed. Since I know the order the items appear in when returning data, I just say, Battery = calculateData(data); in the proper order too. I know the data will always be 2 bytes per variable too. This is nice since I don’t have to worry about fixing indexes when I add / remove data from the IMU.
Thanks Alfred for calling me out on not explaining this. I was in a rush and got side tracked. Just like a teacher, always marking up my work.