Hello! My name is Andrew Jenner and I'm a Software Design Engineer (SDE) in the Visual Studio Devices team. I work on IDE functionality for managed projects (though some of the components I have written are also used by the native project system).
However, for the most part this blog isn't going to be about programming for Smart Devices, programming for the .NET Compact Framework or even .NET programming in general. Instead I'm going to be writing about what I know best - C++ programming techniques, and general programming techniques that can be applied in most languages.
To start off, here's a programming principle that seems to me to be so fundamental that it is often forgotten: Say what you mean.
What do I mean by this? Well, let's start off with a simple example. Suppose you have some code like this:
#include <iostream>
#include <iomanip>
typedef long long int64;
typedef unsigned long long uint64;
struct int128
{
int64 high;
uint64 low;
};int main()
{
int128 x={10,20}, y={30,40}, z; z.low = x.low + y.low;
z.high = x.high + y.high;
if (z.low < x.low)
++z.high; std::cout << std::hex << std::setw(16) << std::setfill('0') << z.high;
std::cout << std::setw(16) << std::setfill('0') << z.low << std::endl;
}
What does this code actually do? Well, it may not be obvious at first sight because it doesn't actually say what it does. But compare this slightly modified version:
#include <iostream>
#include <iomanip>
typedef long long int64;
typedef unsigned long long uint64;
struct int128
{
int64 high;
uint64 low;
};int128 sum_of_128bit_numbers(int128 x,int128 y)
{
int128 z;
z.low = x.low + y.low;
z.high = x.high + y.high;
if (z.low < x.low)
++z.high;
return z;
}int main()
{
int128 x={10,20}, y={30,40}, z; z = sum_of_128bit_numbers(x,y);
std::cout << std::hex << std::setw(16) << std::setfill('0') << z.high;
std::cout << std::setw(16) << std::setfill('0') << z.low << std::endl;
}
Now we can immediately see that this code implements addition of high precision integers. The simple act of naming the 4 mysterious lines of code (by putting them in a function with a descriptive name) has made the program much easier to understand (more so, I would argue, than an equivalent comment that just explains what those 4 lines of code do).
This change doesn't make any difference to the compiler (especially if it implements Named Return Value Optimization or chooses to inline the sum_of_128bit_numbers() function) but it greatly improves the readability of the program for humans. It's easy to write a program that a computer can understand (just change things until it compiles) but writing programs that are easy for people to understand is much more difficult. Since any non-trivial program will eventually need to be maintained, we should strive to make all our code as easy to read by humans as possible.