Andrew Jenner's WebLog

Modern programming techniques

std::cout << "Hello, World!" << std::endl;

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.

Published Thursday, July 29, 2004 4:02 PM by ajenner

Comments

 

Vatsan said:

I'm nitpicking, but there is no such thing as a 'long long' :-|


[Yeah, I can hear it too! Asok shouting to Dilbert, "I won the meeting!"]
July 29, 2004 9:59 PM
 

Andrew Jenner said:

Hehe! Please feel free to nitpick here - I shouldn't be allowed to get away with non-standard extensions just because it's my blog...

True, "long long" isn't part of ISO C++, though it is a fairly common extension for many compilers (including Visual C++, which surprise surprise is the one I usually use). It's also part of the C99 standard, and will probably be part of a future C++ standard.

Aren't you glad that the only places I used "long long" were in typedefs? That way it's very easy to port the program to another compiler which has a different syntax for 64 bit ints. In a larger program I would have put these definitions in a header file which could be swapped out for an appropriate replacement when the program is compiled with a different compiler.
July 29, 2004 11:02 PM
 

Atilla Ozgur said:

This code example is a very good example for <a href="http://www.refactoring.com/catalog/extractMethod.html">
extract method </a> refactoring. This refactoring mostly used to remove duplicate information. But it is also used to introduce meaningfull functions. In the fowler's book. Example is like this.
// explaining comment here
hard to understand code here.

remove comment and hard to understand code introduce a method call with good name.

August 2, 2004 3:21 AM
Anonymous comments are disabled

This Blog

Syndication

Tags

No tags have been created or used yet.

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker