[openbeos] Re: Coding Suggestion -- Templates, Iterators
- From: "Michael Phipps" <mphipps1@xxxxxxxxxxxxxxxx>
- To: openbeos@xxxxxxxxxxxxx
- Date: Sat, 27 Oct 2001 17:04:20 -0400
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.
I think that, though, a function (inline, maybe) might work well for sometime
like that.
Implementation is an exercise for the reader. :-)
>At the risk (ah heck, certainty) of starting a language war, I have a
>couple of suggestion regarding coding style with C++.
>
>I came across the following code today (actually its from CppTest):
>
>void TestSuite::run (TestResult *result)
>{
> for (std::vector<Test *>::iterator it = m_tests.begin ();
> it != m_tests.end ();
> ++it) {
> // loop code
> }
>
>}
>
>(I've snipped out the loop code)
>
>A couple of things occur to me as I stare at this code (besides the
>fact that I'm not crazy about C++). One is that the template
>declaration syntax is often extremely awkward to read. In many cases,
>it makes sense to typedef these buggers into something more friendly.
>In this example, I would do:
>
>typedef std::vector<Test *>::iterator TestIterator;
>
>Another thing is that iterators are pretty common, so I think having a
>standard macro for traversing with one would be very useful. Here's my
>suggestion:
>
>#define foreach(elem,set) for (elem = set.begin (); elem != set.end ();
>++elem)
>
>Oh, I know, I can hear the groaning out there... "we don't use #define
>macros -- that's old-style, icky C crap that we sophisticated C++
>programmers don't use". Maybe, but I still think macros are extremely
>useful when used sparingly and judiciously. They're awfully good at
>hiding uglies.
>
>Now using those two code snippets above, I can rewrite the example
>(using OpenTracker formatting) as follows:
>
>void
>TestSuite::run (TestResult *result)
>{
> TestIterator i;
> foreach (i, m_tests) {
> // loop code
> }
>}
>
>Now that's a bit cleaner/clearer. When reading the iterator loop, you
>have to mentally put the word "in" in there. IOW, read it as "for each
>i in m_tests".
>
>So whadya think? Love it? Hate it?
>
>
- References:
- [openbeos] 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] Coding Suggestion -- Templates, Iterators
- From: Daniel Reinhold