[Ktechlab-devel] a matter of style

Julian Bäume julian at svg4all.de
Mon Dec 7 23:45:34 UTC 2009


On Monday 07 December 2009 21:49:11 P Zoltan wrote:
> > My votes for the record:
> > java-style iterators and foreach-macro, when possible
> 
>   +1, the STL style is not my favorite
So I guess, it's 2 vs 1 in favour of STL style, for now ;)
 
>   foreach is something new for me, so I don't have a string opinion about
> that. Note that instead of that while(), a for() could be also used in
> java style code:
> 
>    for(QStringListIterator it(route); it.hasNext(); ??? ) { ... }
> 
>   Where do you increment that iterator? It's strange.
it.next() will increase the position and return the object. If you don't want 
to increase the pointer, use it.peekNext(). It will just return the object. 
During the first cycle of the loop, it.peekNext() will return the first 
element, until it.next() is called. This is slightly more code, than with STL 
iterators, of course. But IMHO this is much more readable.

So your example would read like:
    for(QStringListIterator it(route); it.hasNext(); it.next() ) { //use 
it.peekNext() to point to the object... }

Also note, QStringListIterator is a const iterator. You can only access the 
objects in the list read-only. If you want to alter the objects, you need to 
use QMutableStringListIterator, instead. (Same applies for STL-style. there is 
QStringList::iterator and QStringList::const_iterator)

Concerning the foreach-macro, well. It's just useful in the special case, when 
iterating over the complete list. It just saves you some lines of code.

>   Note that there is a coding style page in the wiki, so these discussions
> can be noted there.
Okay. I will try to stick to java-style iterators, for now. And if there are 
no further objections, we can manifest it onto that page.

bye
julian
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/ktechlab-devel/attachments/20091208/ab4797bc/attachment.sig>


More information about the Ktechlab-devel mailing list