[openbeos] Re: Coding Suggestion -- Templates, Iterators

>I 100% agree with you about the typedef. Some things are nearly (if 
not completely)
>impossible without typedef (try using pointers to methods without a 
typedef).
>
>The macro? Well.... I can't say I like the whole #define thing. :-)
>The for loop, in general, is for iterations:
>for (int i=0; i<10; i++)
>
>That is a paradigm that EVERY C/C++ programmer out there has seen
>a bazillion times and understands. The foreach() macro is a new 
paradigm.
>I (as the viewer of your code) would go and look to see what was 
happening
>in there. That slows me down. 

Ok, Michael, your point is hard to argue with. I personally find macros 
very useful in some situations, but it's a different matter when 
dealing with a code base that is being worked on by multiple people. 
Using the standard language paradigms is the best way. So ditch the 
macro. Oh, and someone else just pointed out that STL has a for_each() 
anyway (don't know if that's a macro or not). That could be used as 
well (not as well known as the standard for-loop mechanism, but that 
may change over time as STL is used more and more).

Bill has it right that both the containter type and the iterator should 
be typedef'd -- that was just laziness on my part only doing the 
iterator. I would agree with most of what Bill said except for the part 
about short names. Short names are wonderful and readable. Nobody is 
ever going to convince me that

int
maxElement (int array[], int size)
        {
        int loopIndex;
        int maximumValue = a[0];

        for (loopIndex = 1; loopIndex< size; ++loopIndex)
                if (array[loopIndex] > maximumValue)
                        maximumValue = array[loopIndex];

        return maximumValue;
        }

is superior to

int
maxElement (int a[], int size)
        {
        int i;
        int max = a[0];

        for (i = 1; i < size; ++i)
                if (a[i] > max)
                        max = a[i];

        return max;
        }

Naturally, you should only used short names for local variables. And 
only in small functions where their meaning is pretty obvious (even 
then a comment to its purpose might be useful). But in those instances, 
their simplicity and readability make them much better than long names.

So are we in agreement that long or awkward declaration types (such as 
templates) should be typedef'd? If so, we'll add that to the coding 
guidelines (the OpenTracker guidelines are a good baseline, but they're 
a bit vague on some points and don't cover everything we'll have to 
deal with).




Other related posts: