[Ktechlab-devel] a matter of style
P Zoltan
zoltan.padrah at gmail.com
Mon Dec 7 20:49:11 UTC 2009
On Mon, 07 Dec 2009 13:35:03 +0100, Julian Bäume <julian at svg4all.de> wrote:
> 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
+1, the STL style is not my favorite
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.
Note that there is a coding style page in the wiki, so these discussions
can be noted there.
>
> bye then
> julian
More information about the Ktechlab-devel
mailing list