[Ktechlab-devel] a matter of style

Julian Bäume julian at svg4all.de
Mon Dec 7 12:35:03 UTC 2009


hi again,
I want to have short input on which iterators to use. Qt provides both, STL- 
and JAVA-style iterators. Here an example:
java-style:
    QStringListIterator it(route);
    QPointF p;
    p.setX(it.next().toDouble()*8);
    p.setY(it.next().toDouble()*8);
    moveTo( p );
    while (it.hasNext())
    {
        p.setX(it.next().toDouble());
        p.setY(it.next().toDouble());
        p*=8;
        lineTo( p );
    }

same code with STL-style:
    QStringListI::const_iterator it = route.constBegin();
    QPointF p;
    p.setX((*it++).toDouble()*8);
    p.setY((*it++).toDouble()*8);
    moveTo( p );
    while ( it != route.constEnd() )
    {
        p.setX((*it++).toDouble());
        p.setY((*it++).toDouble());
        p*=8;
        lineTo( p );
    }

which one should we use? There is now speed-impact of one over the other. Just 
a matter of taste.

When iterating the whole list, I personally prefer the foreach macro, like:
    foreach (QVariant component, m_model->components())
    {
        if (component.canConvert(QVariant::Map)) {
            addItem( new ComponentItem( component.toMap(), m_theme ) );
        }
    }
Is it okay? Or should we use another method, to iterate over every item in a 
list. (alternatives are: for-loops in combination with List::size() or 
iterators)

I know, we have talked about that, earlier, but IMHO, we didn't agree on 
anything, yet. At least, I'm not aware of it. We should specify these things 
in the wiki.. and I'm willing to go through the code and change everything 
according to our agreements. This doesn't have to be done, right now, I just 
want to produce new code according to our agreements.

My votes for the record:
java-style iterators and foreach-macro, when possible

bye then
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/20091207/5efbaba8/attachment.sig>


More information about the Ktechlab-devel mailing list