[Kde-pim] foreach and akonadi

Tom Albers tomalbers at kde.nl
Sun Apr 27 18:32:17 BST 2008


Hi There,

Albert is verifying the usage of foreach in kde's repository and send me the following interesting mail and ask me to forward it to the list.

Toma


Op zaterdag 26 april 2008 01:00 schreef Albert Astals Cid <aacid at kde.org>:
> Doing my routine foreach cleanup i discovered a few places on akonadi where 
> foreach is used to modify the contents of a list
> 
> Example:
>     foreach ( Part part, parts ) {
>       part.setData( QByteArray() );
>       if ( !part.update() )
>         qDebug() << "failed to update item part" << part.id();
>     }
> 
> The problem here is that you are calling a "modifier" on "part" variable, 
> that "part" is a copy of the element pressent on "parts", so it is really not 
> modifying anything once you go out of the foreach (see my blog [1])
> 
> But NO! Will you say, it is working! 
> 
> Yeah, i know it is working, it works "by luck" because on the Part copy 
> constructor you share the private pointer with the original element so in the 
> end, calling setData modifies the private pointer of the original parts 
> element, so it works.
> 
> But in my opinion you should try to avoid these kind of foreach constructions 
> as foreach 99.99% does not work for modyfiyng lists and it makes newbies to 
> akonady code like me that look at it, think that it is wrongly written.
> 
> Albert
> 
> [1] http://tsdgeos.blogspot.com/2008/04/qforeach-is-your-friend.html

-------------- next part --------------
_______________________________________________
KDE PIM mailing list kde-pim at kde.org
https://mail.kde.org/mailman/listinfo/kde-pim
KDE PIM home page at http://pim.kde.org/


More information about the kde-pim mailing list