[Qt-interest] Qt coding guidelines/conventions

Ross Driedger ross at earz.ca
Thu Mar 25 15:01:00 CET 2010


On 25-Mar-10, at 7:15 AM, qt-interest-request at trolltech.com wrote:
>
>
> What coding convention/guidelines do you guys follow while writing  
> your Qt apps?

Hmmmm.  I didn't think that this list was for a discussion of  
religious issues. :)

Seriously, they are what you want them to be.  Each shop has its own  
standards -- some strict, some not -- and it is best to follow the  
policy.  A coding standard should make sense, be consistent, and make  
the code more understandable.  Other than that, it is up to you.  I  
would suggest looking at Steve McConnell's book Code Complete to see  
what he has to say about these points.

One thing is for certain in my experience, though, whatever standards  
you adopt, someone will be opposed and suggest something they think is  
better.

> I come from Java world :) so am familiar with the camel notation.
> Other than that I wanted to know if there's a std that ppl usually
> follow while writing code.
> eg I've seen in Qt, member variables have a "m_" prefixed to
> them...something similar is mentioned in the MythTV guidelines which
> uses Qt (http://www.mythtv.org/wiki/Coding_Standards

I started doing this with Windows programming and still do if I'm on a  
Windows project.  Other projects use a starting underscore with camel  
caps for private members.

> Also, since I no longer have the luxury of automatic garbage
> collection (ah...I miss java! :P) and guidelines on how to protect
> yourself from those nefarious memory leaks?

I started in the C world and I tend to view automatic GC as a crutch  
which has covered a multitude of horribly programmed apps.  In fact,  
when I used t interview Java programmers, I would give them tests in  
manual deletion in pseudo code because being able to match mallocs/ 
frees or news/deletes is a sign of a careful programmer.

But prejudices aside, valgrind (http://valgrind.org) has become my  
best programming buddy.  Too bad there isn't a Windows version, but my  
work now is almost all cross-platform.

> Some helpful stuff I found was using QPointer, which makes the task of
> checking for dangling pointers easy.
> http://doc.trolltech.com/4.6/qpointer.html
> http://doc.trolltech.com/4.6/qobjectcleanuphandler.html
>
> I'm also looking at other 3rd party C++ libs which help in this job.
>
> Any other tips/suggestions?

I would suggest adopting a very careful approach to using memory.  GC  
encourages an attitude of carelessness about heap memory, which I  
don't have because of my programmer roots in C.  As an exercise, you  
might want to try some good old fashioned C project (like a hash  
table) that uses dynamic memory, just to get into the good programming  
habits of matching allocations and releases.

I would also suggest getting the book version of the comp.lang.c++ FAQ  
(more exhaustive than the online versions).  I consider the book a  
must for every serious C++ code slinger and it has some very good  
suggestions with respect to using heap memory.

Ross Driedger
ross (at) earz (dot) ca



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100325/9a2f8290/attachment.html 


More information about the Qt-interest-old mailing list