[openbeos] Re: Coding Suggestion -- Templates, Iterators
- From: "Bill Beebe" <wbeebe@xxxxxxxxxx>
- To: <openbeos@xxxxxxxxxxxxx>
- Date: Sat, 27 Oct 2001 20:46:50 -0700
Yes. So-called long or awkward names are better off being typedefed (as long
as the typedef is sane) and placed in the class header file.
One other thing that we've discovered over time: try to forward declare
classes in the header rather than include the header files. This keeps
recompilation down when headers do change and speeds up builds. If all you're
doing is referencing a class as a type (and not any of its member elements)
you can just forward declare them and include the headers in the cpp file.
This won't mean much at first but it will as the (sub)systems grow larger.
for_each is not a macro but a templated funciton in algorythm. There's a
tremendous amount of functionality already in the STL. For a test of this
check out http://www.camtp.uni-mb.si/books/Thinking-in-C++/Chapter05.html
As for short names, here again is my take on what you wrote as an example:
template<typename T>
T maxElement(T array[], int size)
{
int
idx;
T
max_val = array[0];
for (idx = 1; idx < size; ++idx)
{
if (max_val < array[idx])
{
max_val = array[idx];
}
}
return max_val;
}
My example, while more verbose than your second, is shorter and more succinct
than your straw-man first example. Note that now that I've made it a templated
function that some of those names begin to make sense. I won't continue to
belabor this point (it's getting silly), but I write just enough to clearly
convey what I mean, and to make it easy to find when buried in a larger body
of code. I find it far easier to look for all instances of 'idx' than for just
'i'. And for the function arguments, I do like to have them spelled out a
little more than internally. That's the portion that becomes the public
interface, and public interfaces should be spelled out.
One other thing: I use curly braces on all conditional logic even if I have
only one thing to do. This makes it far easier to find operations executed for
a given condition.
- Follow-Ups:
- [openbeos] Re: Coding Suggestion -- Templates, Iterators
- From: Bill Beebe
- References:
- [openbeos] Re: Coding Suggestion -- Templates, Iterators
- From: Daniel Reinhold
Other related posts:
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- » [openbeos] Re: Coding Suggestion -- Templates, Iterators
- [openbeos] Re: Coding Suggestion -- Templates, Iterators
- From: Bill Beebe
- [openbeos] Re: Coding Suggestion -- Templates, Iterators
- From: Daniel Reinhold