]> git.saurik.com Git - wxWidgets.git/blame - docs/html/standard.htm
item about constants naming added
[wxWidgets.git] / docs / html / standard.htm
CommitLineData
97979ddf
JS
1<HTML>
2<HEAD>
3 <TITLE>wxWindows Programmer Style Guide</TITLE>
4</HEAD>
5
6<BODY>
7
8<a name="top"></a>
9
10<font face="Arial, Lucida Sans, Helvetica">
11
12<table width=100% border=4 cellpadding=5 cellspacing=0>
13<tr>
14<td bgcolor="#660000">
15<font size=+1 face="Arial, Lucida Sans, Helvetica" color="#FFFFFF">
16wxWindows Programmer Style Guide
17</font>
18</td>
19</tr>
20</table>
21
22<P>
23
24by <A HREF=mailto:zeitlin@dptmaths.ens-cachan.fr>Vadim Zeitlin</A><P>
25
26This guide is intended for people who are (or intending to start) writing code
27for <A HREF="http://web.ukonline.co.uk/julian.smart/wxwin/" target=_top>wxWindows</A> class library.
28
29<P>
30The guide is separated into two parts: the first one addresses the general
31compatibility issues and is not wxWindows-specific. The advises in this part
32will hopefully help you to write programs which compile and run on greater
33variety of platforms. The second part details the wxWindows code organization and
34its goal it to make wxWindows as uniform as possible without imposing too
35many restrictions on the programmer.
36<P>
172d3acb 37Acknowledgements: This guide is partly based on <A
97979ddf
JS
38HREF=http://www.mozilla.org/docs/tplist/catBuild/portable-cpp.html target=_top>
39C++ portability guide</A> by David Williams.
40
41<P>
42<H3>General C++ Rules</H3>
43<UL>
44 <LI>New or not widely supported C++ features</LI>
45 <OL>
46 <LI><A HREF="#no_templates">Don't use C++ templates</A></LI>
47 <LI><A HREF="#no_exceptions">Don't use C++ exceptions</A></LI>
48 <LI><A HREF="#no_rtti">Don't use RTTI</A></LI>
49 <LI><A HREF="#no_namespaces">Don't use namespaces</A></LI>
50 <LI><A HREF="#no_stl">Don't use STL</A></LI>
51 <LI><A HREF="#no_fordecl">Don't declare variables inside <TT>for()</TT></A></LI>
52 <LI><A HREF="#no_nestedclasses">Don't use nested classes</A></LI>
53 </OL>
54 <BR>
55 <LI>General recommendations</LI>
56 <OL>
57 <LI><A HREF="#no_globals">No global variables with constructor</A></LI>
58 <LI><A HREF="#no_warnings">Turn on all warnings and eradicate them</A></LI>
59 <LI><A HREF="#no_assume_sizeof">Don't rely on <TT>sizeof(int) == 2</TT>...</A></LI>
60 <LI><A HREF="#no_assignment_in_if">No assignments in conditional expressions</A></LI>
61 <LI><A HREF="#no_comment_code">Use <TT>#if 0</TT> rather than comments to temporarily
62 disable blocks of code</A></LI>
63 <LI><A HREF="#no_extra_semicolon">Don't use extra semi-colons on top level</A></LI>
64 </OL>
65 <BR>
66 <LI>Unix/DOS differences</LI>
67 <OL>
68 <LI><A HREF="#use_cpp_ext">Use .cpp for C++ source file extension</A></LI>
69 <LI><A HREF="#no_backslash">Don't use backslash ('\\') in &#35;includes</A></LI>
70 <LI><A HREF="#no_carriagereturn">Avoid carriage returns in cross-platform code</A></LI>
71 <LI><A HREF="#no_caps_in_filenames">Use only lower letter filenames</A></LI>
72 <LI><A HREF="#no_incomplete_files">Terminate the files with a new-line</A></LI>
73 </OL>
74 <BR>
75 <LI>Style choices</LI>
76 <OL>
77 <LI><A HREF="#naming_conv">Naming conventions: use <TT>m_</TT> for members</A></LI>
78 <LI><A HREF="#no_void_param">Don't use <TT>void</TT> for functions without
79 arguments</A></LI>
80 <LI><A HREF="#no_const_int">Don't use <TT>const</TT> for non pointer/reference
81 arguments</A></LI>
82 </OL>
83</UL>
84
85<P>
86
87<H3>wxWindows Rules</H3>
88<UL>
89 <LI>Files location and naming conventions</LI>
90 <OL>
91 <LI><A HREF="#file_locations">File locations</A></LI>
92 <LI><A HREF="#include_guards">Include guards</A></LI>
93 <LI><A HREF="#pch">Precompiled headers</A></LI>
94 </OL>
95
96 <BR>
97 <LI>File layout and indentation</LI>
98 <OL>
99 <LI><A HREF="#wxwin_header">wxWindows standard header</A></LI>
100 <LI><A HREF="#indentation">Indent your code with 4 spaces (no tabs!)</A></LI>
101 <LI><A HREF="#class_decl">Order of parts in a class declarations</A></LI>
102 </OL>
172d3acb 103
97979ddf
JS
104 <BR>
105 <LI>More about naming conventions</LI>
106 <OL>
107 <LI><A HREF="#wx_prefix">Use wx or WX prefix for all public symbols</A></LI>
172d3acb 108 <LI><A HREF="#wxdllexport">Use WXDLLEXPORT with all classes/functions in
97979ddf
JS
109 wxMSW/common code</A></LI>
110 <LI><A HREF="#set_get">Use Set/Get prefixes for accessors</A></LI>
172d3acb 111 <LI><A HREF="#constants">wxNAMING_CONSTANTS</A></LI>
97979ddf
JS
112 </OL>
113
114 <BR>
115 <LI>Miscellaneous</LI>
116 <OL>
117 <LI><A HREF="#forward_decl">Use forward declarations whenever possible</A></LI>
118 <LI><A HREF="#debug_macros">Use debugging macros</A></LI>
119 </OL>
120</UL>
121
122<HR>
123
124<H3>General C++ Rules</H3>
125<UL>
126 <LI>New or not widely supported C++ features</LI>
172d3acb 127
97979ddf
JS
128 <P>The usage of all features in this section is not recommended for one reason: they appeared in C++ relatively recently and are not yet
129supported by all compilers. Moreover, when they're supported, there are
130differences between different vendor's implementations. It's understandable that
131you might love one (or all) of these features, but you surely can write C++
132programs without them. Where possible, workarounds to compensate for absence
133of your favourite C++ abilities are indicated.
134 <P>Just to suppress any doubts that there are compilers which don't support
135these new features, you can think about Win16 (a.k.a. Win 3.1) compilers,
136<I>none</I> of which supports <I>any</I> feature from the list below.
172d3acb 137
97979ddf
JS
138 <OL>
139 <P><LI><A NAME="no_templates"></A><B>Don't use C++ templates</B></LI><P>
140Besides the reasons mentioned above, template usage also makes the
141program compile much slower (200%-300% is not uncommon) and their support
142even in the compilers which have had it for a long time is far from perfect
143(the best example is probably gcc).
144<P><U>Workaround</U>: The things you would like to use templates for are,
145most commonly, polymorphic containers (in the sense that they can contain objects of
146any type without compromising C++ type system, i.e. using <TT>void *</TT>
147is out of question). wxWindows provides <A HREF="TODO">dynamic
148arrays and lists</A> which are sufficient in 99% of cases - please don't hesitate
172d3acb 149to use them. Lack of template is not a reason to use static arrays or
97979ddf
JS
150type-less (passing by <TT>void *</TT>) containers.
151
152 <P><LI><A NAME="no_exceptions"></A><B>Don't use C++ exceptions</B></LI><P>
153The C++ exception system is an error-reporting mechanism. Another reasons not to use it,
154besides portability, are the performance penalty it imposes (small, but, at least for
155current compilers, non-zero), and subtle problems with
156memory/resource deallocation it may create (the place where you'd like to use
157C++ exceptions most of all are the constructors, but you need to be very
158careful in order to be able to do it).
159<P><U>Workaround</U>: there is no real workaround, of course, or the exceptions
160wouldn't have been added to the language. However, there are several rules which
161might help here:<P>
162
163<OL>
164 <LI>Every function returns an integer (or at least boolean) error code.
165 <P>There is no such thing as a function that never fails - even if it can't
166 fail now, it might do it later, when modified to be more powerful/general.
167 Put the <TT>int</TT> or <TT>bool</TT> return type from the very beginning!<P>
168 </LI><LI>Every function you call may fail - check the return code!
169 <P>Never rely on the function's success, always test for a possible error.<P>
170 </LI><LI>Tell the user about the error, don't silently ignore them.
171 <P>Exceptions are always caught and, normally, processed when they're
172 caught. In the same manner, the error return code must always be processed
173 somehow. You may choose to ignore it, but at least tell the user that
174 something wrong happened using <A HREF="TODO"><TT>wxLogError</TT></A> or
175 <A HREF="TODO"><TT>wxLogWarning</TT></A> functions. All wxWindows
176 functions (must) log the error messages on failure - this can be disabled
177 by using <A HREF="TODO">wxLogNull</A> object before calling it.
178 <P>Examples:<UL>
179 <LI><I>Wrong</I>:
180 <PRE>
181void ReadAddressBookFile(const wxString& strName)
182{
183 wxFile file;
172d3acb 184
97979ddf
JS
185 if ( !file.Open(strFile) )
186 return;
172d3acb 187
97979ddf
JS
188 ...process it...
189}
190 </PRE>
191 </LI><LI><I>Correct</I>:
192 <PRE>
193// returns false if the address book couldn't be read
194bool ReadAddressBookFile(const wxString& strName)
195{
196 wxFile file;
172d3acb 197
97979ddf
JS
198 if ( !file.Open(strFile) ) {
199 // wxFile logged an error because file couldn't be opened which
200 // contains the system error code, however it doesn't know what
201 // this file is for and an error message "can't open $GLCW.ADB"
202 // can be quite confusing for the user. Here we say what we mean.
172d3acb 203 wxLogError("Can't read address book from '%s'!",
97979ddf
JS
204 strName.c_str());
205 return false;
206 }
172d3acb 207
97979ddf 208 ...process it...
172d3acb 209
97979ddf
JS
210 return true;
211}
212 </PRE>
213 or, if it's not an error if file doesn't exist (here we could just check
214 its existence, but let's suppose that there is no <TT>wxFile::Exists()</TT>)
215 we can also write:
216 <PRE>
217// returns false if address book file doesn't exist
218bool ReadAddressBookFile(const wxString& strName)
219{
220 wxFile file;
172d3acb 221
97979ddf
JS
222 // start a block inside which all log messages are suppressed
223 {
224 wxLogNull noLog;
225 if ( !file.Open(strFile) )
226 return false;
227 }
172d3acb 228
97979ddf 229 ...process it...
172d3acb 230
97979ddf
JS
231 return true;
232}
233 </PRE></LI>
234 </UL>
235 </OL>
172d3acb 236
97979ddf
JS
237 <P><LI><A NAME="no_rtti"></A><B>Don't use RTTI</B></LI><P>
238RTTI stands for Run-Time Type Information and there is probably no other
239reason not to use it except the portability issue and the fact that it adds
240<TT>sizeof(void *)</TT> bytes to any class having virtual functions (at least,
241in the implementations I'm aware of).
242<P><U>Workaround</U>: use wxWindows RTTI system which allows you to do almost
243everything which the new C++ RTTI, except that, of course, you have to use
172d3acb 244macros instead of the (horrible looking, BTW) <TT>dynamic_cast</TT>.
97979ddf
JS
245
246 <P><LI><A NAME="no_namespaces"></A><B>Don't use namespaces</B></LI><P>
247This topic is subject to change with time, however for the moment all wxWindows
248classes/functions live in the global namespace.
249<P><U>Workaround</U>: None.
250
251 <P><LI><A NAME="no_stl"></A><B>Don't use STL</B></LI><P>
252STL is the new C++ standard library, proposing all kinds of template containers
253and generic algorithm implementations. Templates are the heart (and almost
254everything else) of the library, so its usage is out of question. Besides, even
255with the compilers which do support templates, STL has many of its own problems,
256there are many "not 100% standard compatible" vendor implementations, none of existing debuggers understands its
257complicated data structures, ... the list can go on (almost) forever.
258<P><U>Workaround</U>: Use wxString, dynamic arrays and lists and other wxWindows
259classes. wxString has many of the most often used functions of std::string STL
260class (typedef to be precise).
261 <P><LI><A NAME="no_fordecl"></A><B>Don't declare variables inside <TT>for()
262 </TT></B></LI><P>
263The scope of a variable declared inside <TT>for()</TT> statement changed several
264years ago, however many compilers still will complain about second declaration
265of <TT>i</TT> in the following code:
266<PRE>
267 for ( int i = 0; i < 10; i++ ) {
268 ...
269 }
270
271 ...
272
273 for ( int i = 0; i < 10; i++ ) {
274 ...
275 }
276</PRE>
277Even if it's perfectly legal now.
278<P><U>Workaround</U>: write this instead:
279<PRE>
280 int i;
281 for ( i = 0; i < 10; i++ ) {
282 ...
283 }
284
285 ...
286
287 for ( i = 0; i < 10; i++ ) {
288 ...
289 }
290</PRE>
291
292 <P><LI><A NAME="no_nestedclasses"></A><B>Don't use nested classes</B></LI><P>
293Nested classes are, without doubt, a very good thing because they allow to hide
294"private" (in the sense that they're used only inside the library) classes and,
295generally, put the related things together.
296<P>Unfortunately, some compilers have trouble understanding them, so we must
297sacrifice the ideals of software design to get a working program in this case.
298<P><U>Workaround</U>: instead of
299<PRE>
300 // in the header
301 class PublicLibClass {
302 ...
303 private:
304 class PrivateLibClass { ... } m_object;
305 };
306</PRE>
307you can try the following:
308<PRE>
309 // in the header
310 class PrivateLibClass; // fwd decl
311 class PublicLibClass {
312 ...
313 private:
314 class PrivateLibClass *m_pObject;
315 };
172d3acb 316
97979ddf
JS
317 // in the .cpp file
318 class PrivateLibClass { ... };
172d3acb 319
97979ddf
JS
320 PublicLibClass::PublicLibClass()
321 {
322 m_pObject = new PrivateLibClass;
172d3acb 323
97979ddf
JS
324 ...
325 }
172d3acb 326
97979ddf
JS
327 PublicLibClass::~PublicLibClass()
328 {
329 delete m_pObject;
330 }
331</PRE>
332<P>A nice side effect is that you don't need to recompile all the files
333including the header if you change the PrivateLibClass declaration (it's
334an example of a more general interface/implementation separation idea).
335</OL>
336
337 <BR>
338 <LI>General recommendations</B></LI><P>
339While the recommendations in the previous section may not apply to you if you're
340only working with perfect compilers which implement the very newest directives of
172d3acb 341C++ standard, this section contains compiler- (and language-) independent advice
97979ddf 342which <B>must</B> be followed if you wish to write correct, i.e. working, programs. It
172d3acb 343also contains some C/C++ specific remarks in the end which are less
97979ddf
JS
344important.
345 <OL>
346 <P><LI><A NAME="no_globals"></A><B>No global variables with constructors</B></LI><P>
347In C++, the constructors of global variables are called before the
172d3acb 348<TT>main()</TT> function (or <TT>WinMain()</TT> or any other program entry point)
97979ddf
JS
349starts executing. Thus, there is no possibility to initialize <I>anything</I>
350before the constructor call. The order of construction is largely
351implementation-defined, meaning that there is no guarantee that one global
352object will be initialized before another one (except if they are both defined
353in the same translation unit, i.e. .cpp file). Most importantly, no custom
354memory allocation operators are installed at the moment of execution of global
355variables constructors, so a (less restrictive) rule is that you should have
356no global variables which allocate memory (or do anything else non-trivial) in
357the constructor. Of course, if an object doesn't allocate memory in its constructor
358right now, it may start making it later, so you can only be sure about this if
359you don't use <I>any</I> variables of object (as opposed to simple:
360<TT>int</TT>, ...) types. Example: currently, wxString doesn't allocate memory
361in its default constructor, so you might think that having a global (initially)
362empty wxString is safe. However, if wxString starts allocating some minimal
363amount of memory in its default constructor (which doesn't look unreasonable),
364you would have all kinds of problems with <TT>new</TT>
365and <TT>delete</TT> operators (overloaded in wxWindows), especially because the first <TT>new</TT> called
366is the standard one (before wxWindows overloads them) and <TT>delete</TT> will
367be the overloaded operator.
368
369 <P><LI><A NAME="no_warnings"></A><B>Turn on all warnings and eradicate them</B></LI><P>
370Give the compiler a chance to help you - turn on all warnings! You should always
371use the maximum available warning level of your compiler and understand and
372correct each of them. If, for whatever reasons, a compiler gives a warning on
373some perfectly legal line of code and you can't change it, please insert a
374comment indicating it in the code. Most oftenly, however, all compiler warnings
375may be avoided (not suppressed!) with minimal changes to your code.
376
377 <P><LI><A NAME="no_assume_sizeof"></A><B>Don't rely on <TT>sizeof(int) == 2</TT>...</B></LI><P>
378You should never assume any absolute constraints on data type sizes. Currently,
379we have 16-bit, 32-bit and 64-bit machines and even inside each class data type
380sizes are different. A small table illustrates it quite well:
381<TABLE BORDER COLS=5 WIDTH="100%" NOSAVE >
382<TR>
383 <TD>Architecture/OS</TD>
384 <TD>sizeof(short)</TD>
385 <TD>sizeof(int)</TD>
386 <TD>sizeof(long)</TD>
387 <TD>sizeof(void *)</TD>
388</TR>
389
390<TR>
391 <TD>i386/Windows 3.1</TD>
392 <TD>2</TD>
393 <TD>2</TD>
394 <TD>4</TD>
395 <TD>2 or 4</TD>
396</TR>
397
398<TR>
399 <TD>i386/Windows 95</TD>
400 <TD>2</TD>
401 <TD>4</TD>
402 <TD>4</TD>
403 <TD>4</TD>
404</TR>
405
406<TR>
407 <TD>Merced/Win64</TD>
408 <TD>2</TD>
409 <TD>4</TD>
410 <TD>4</TD>
411 <TD>8</TD>
412</TR>
413
414<TR>
415 <TD>Alpha/Linux</TD>
416 <TD>???</TD>
417 <TD>???</TD>
418 <TD>???</TD>
419 <TD>???</TD>
420</TR>
421</TABLE>
422
423 <P><LI><A NAME="no_assignment_in_if"></A><B>No assignments in conditional expressions</B></LI><P>
424Although close to the heart of many C programmers (I plead guilty), code like
425classical <TT>if ( (c = getchar()) != EOF )</TT> is bad because it prevents you
426from enabling "assignment in conditional expression" warning (see also
172d3acb 427<A HREF="#no_warnings">above</A>) which is helpful to detect common
97979ddf
JS
428mistypes like <TT>if ( x = 2 )</TT> instead of <TT>if ( x == 2 )</TT>.
429
430 <P><LI><A NAME="no_comment_code"></A><B>Use <TT>#if 0</TT> rather than comments to temporarily
431 disable blocks of code</B></LI><P>
432If you have to temporarily disable some code, use
433<PRE>
434 #if 0 // VZ: I think this code is unneeded, it probably must be removed
435 ...
436 #endif // 0
437</PRE>
438instead of
439<PRE>
440 /*
441 ...
442 */
443</PRE>
444The reason is simple: if there are any <TT>/* ... */</TT> comments inside
445<TT>...</TT> the second version will, of course, miserably fail.
446
447 <P><LI><A NAME="no_extra_semicolon"></A><B>Don't use extra semi-colons on top level</B></LI><P>
448Some compilers don't pay any attention to extra semicolons on top level, as in
449<PRE>
450 class Foo { };;
451</PRE>
452while others complain loudly about it. Of course, you would rarely put 2
453semicolons yourself, but it may happen if you're using a macro
454(<TT>IMPLEMENT_something</TT>, for example) which already has a ';' inside and
455put another one after it.
456 </OL>
457
458 <BR>
459 <LI>Unix/DOS differences</B></LI><P>
460 Two operating systems supported by wxWindows right now are (different flavours
461of) Unix and Windows 3.1/95/NT (although Mac, OS/2 and other ports exist/are
462being developed as well). The main differences between them are summarized
463here.
464
465 <OL>
466 <P><LI><A NAME="use_cpp_ext"></A><B>Use .cpp for C++ source file extension</B></LI><P>
467There is, unfortunately, no standard exceptions for C++ source files. Different
468people use .C, .cc, .cpp, .cxx, .c++ and probably several others I forgot. Some
469compilers don't care about extension, but there are also other ones which can't
470be made to compile any file with "wrong" extension. Such compilers are very
471common in DOS/Windows land, that's why the .cpp extension is the least likely to
472cause any problems - it's the standard one under DOS and will probably be
473accepted by any Unix compiler as well (any counter examples?). The extension
474for the header files is .h.
475
476 <P><LI><A NAME="no_backslash"></A><B>Don't use backslash ('\\') in &#35;includes</B></LI><P>
477Although it's too silly to mention, please don't use backslashes in
478<TT>&#35;include</TT> preprocessor statement. Even not all Windows compilers accept
479it, without speaking about all other ones.
480
481 <P><LI><A NAME="no_carriagereturn"></A><B>Avoid carriage returns in cross-platform code</B></LI><P>
482This problem will hopefully not arise at all, with CVS taking care of this
483stuff, however it's perhaps not useless to remember that many Unix compilers
484(including, but not limited to, gcc) don't accept carriage returns
485(= <Ctrl-M> = '\r') in C/C++ code.
486
487 <P><LI><A NAME="no_caps_in_filenames"></A><B>Use only lower case filenames</B></LI><P>
488DOS/Windows 3.1 isn't case sensitive, Windows 95/NT are case preserving, but not
489case sensitive. To avoid all kinds of problems with compiling under Unix (or
490any other fully case-sensitive OS), please use only lower case letters in the
491filenames.
492
493 <P><LI><A NAME="no_incomplete_files"></A><B>Terminate the files with a new-line</B></LI><P>
494While DOS/Windows compilers don't seem to mind, their Unix counterparts don't
495like files without terminating new-line. Such files also give a warning message
496when loaded to vim (the Unix programmer's editor of choice :-)), so please think
497about terminating the last line.
498 </OL>
172d3acb 499
97979ddf
JS
500 <BR>
501 <LI>Style choices</B></LI><P>
172d3acb 502 All wxWindows specific style guidelines are specified in the next
97979ddf
JS
503section, here are the choices which are not completely arbitrary,
504but have some deeper and not wxWindows-specific meaning.
505
506 <OL>
507 <P><LI><A NAME="naming_conv"></A><B>Naming conventions: use <TT>m_</TT> for members</B></LI><P>
508It's extremely important to write readable code. One of the first steps in this
509direction is the choice of naming convention. It may be quite vague or strictly
510define the names of all the variables and function in the program, however it
511surely must somehow allow the reader to distinguish between variable and
512functions and local variables and member variables from the first glance.
513<P>The first requirement is commonly respected, but for some strange reasons, the
514second isn't, even if it's much more important because, after all, the immediate
515context usually allows you to distinguish a variable from a function in
516C/C++ code. On the other hand, you <I>cannot</I> say what <TT>x</TT> in the
517following code fragment is:
518<PRE>
519 void Foo::Bar(int x_)
520 {
521 ...
172d3acb 522
97979ddf 523 x = x_;
172d3acb 524
97979ddf
JS
525 ...
526 }
527</PRE>
528It might be either a local variable (unluckily the function is too long so you
529don't see the variable declarations when you look at <TT>x = x_</TT> line), a
530member variable or a global variable - you have no way of knowing.
531<P>The wxWindows naming convention gives you, the reader of the code, much more
532information about <TT>x</TT>. In the code above you know that it's a local
533variable because:<P>
534<OL>
535 <LI>global variables are always prefixed with <TT>g_</TT></LI>
536 <LI>member variables are always prefixed with <TT>m_</TT></LI>
537 <LI>static variables are always prefixed with <TT>s_</TT></LI>
538</OL>
539<P>Examples:
540<PRE>
541 extern int g_x; // of course, 'x' is not the best name for a global...
542
543 void Bar()
544 {
545 int x;
546 }
547
548 class Foo {
549 public:
550 void SetX(int x) { m_x = x; }
551 private:
552 int m_x;
553 };
554</PRE>
555As you see, it also solves once and for all the old C++ programmer's question:
556how to call <TT>SetX()</TT> parameter? The answer is simple: just call it
557<TT>x</TT> because there is no ambiguity with <TT>Foo::m_x</TT>.
558<P>The prefixes can be combined to give <TT>ms_</TT> and <TT>gs_</TT> for static
559member (a.k.a. class) variables and static global variables.
560<P>The convention is, of course, completely worthless if it is not followed:
561nothing like being sure that <TT>x</TT> is a local variable in the code fragment
562above and discovering later the following lines in the header:
563<PRE>
564 class Foo {
565 ...
566 int x; // I don't like wxWindows naming convention
567 };
568</PRE>
569Please do use these prefixes, they make your code much easier to read. Also
570please notice that it has nothing to do with the so-called <I>Hungarian notation</I>
571which is used in wxMSW part of wxWindows code and which encodes the <I>type</I>
572of the variable in its name - it is actually quite useful in C, but has little
573or no sense in C++.
574
575 <P><LI><A NAME="no_void_param"></A><B>Don't use <TT>void</TT> for functions without
576 arguments</B></LI><P>
577In ANSI C, <TT>void Foo()</TT> takes an arbitrary number of arbitrarily typed
578arguments (although the form <TT>void Foo(...)</TT> is preferred) and <TT>void
579Foo(void)</TT> doesn't take any arguments. In C++, however, the situation is
580different and both declarations are completely equivalent. As there is no need
581to write <TT>void</TT> in this situation, let's not write it - it can only be
582confusing and create an impression that it really means something when it's not
583at all the case.
584
585 <P><LI><A NAME="no_const_int"></A><B>Don't use <TT>const</TT> for non pointer/reference
586 arguments</B></LI><P>
587In both C and C++ an argument passed by value cannot be modified - or, more
588precisely, if it is modified in the called function, only the local copy is
589really changed, not the caller's variable. So, semantically speaking, there is
590no difference between <TT>void Foo(int)</TT> and <TT>void Foo(const int)</TT>.
591However, the <TT>const</TT> keyword is confusing here, adds nothing to the code
592and even cannot be removed if <TT>Foo()</TT> is virtual and overridden (because
593the names are mangled differently). So, <I>for arguments passed by value</I>
594you shouldn't use <TT>const</TT>.
172d3acb 595<P>Of course, it doesn't apply to functions such as
97979ddf
JS
596<TT>void PrintMessage(const char *text)</TT> where <TT>const</TT> is mandatory.
597 </OL>
598</UL>
599
600<P>
601
602<H3>wxWindows rules</H3>
603<UL>
604 <P><LI>File location and naming conventions</LI><P>
605 <OL>
606 <P><LI><A NAME="file_locations"></LI><B>File locations</B><P>
607The wxWindows files for each supported platform have their own subdirectories
608in "include" and "src". So, for example, there is "src/msw", "include/gtk"
609etc. There are also two special subdirectories called "common" and
610"generic". The common subdirectory contains the files which are platform
172d3acb 611independent (wxObject, wxString, ...) and the generic one the generic
97979ddf
JS
612implementations of GUI widgets, i.e. those which use only other wxWindows
613classes to implement them. For the platforms where the given functionality
614cannot be implemented natively, the generic implementation is used and the native
615one is used for the others. As I feel that it becomes a bit too confusing,
616here is an example: wxMessageBox function is implemented natively under
617Windows (where it just calls MessageBox API), but there is also a generic
618implementation which is used under, for example, GTK. A generic class should
619normally have a name that distinguishes it from any platform-specific implementation.
620A #define will allow wxGenericMessageDialog to be wxMessageDialog on some
621platforms, for example.
622
623<P>This scheme applies not only for the .cpp files, but also for the headers.
624However, as the program using wxWindows should (ideally) not use any
625"<TT>&#35;ifdef &lt;platform&gt;</TT>" at all, the headers are always included with
626"<TT>&#35;include &lt;wx/msgdlg.h&gt;</TT>" (for example). This file, in turn, includes
627the right header for given platform. Any new headers should conform to this
628setup as well to allow including <TT>&lt;wx/foo.h&gt;</TT> on any platform.<P>
629
630Note that wxWindows implementation files should use quotes when including wxWindows
631headers, not angled brackets. Applications should use angled brackets. There
632is a reason for it (can anyone remember what this is?).
633
634 <P><LI><A NAME="include_guards"></LI><B>Include guards</B><P>
635To minimize the compile time C++ programmers often use so called include
636guards: for example, in the header file foo.h you might have
637
638<PRE>
639&#35;ifndef _FOO_H_
640&#35;define _FOO_H_
641
642... all header contents ...
643
644&#35;endif
645 //_FOO_H_
646</PRE>
647
648In this way, the header will only be included once for the compilation
649of any .cpp (of course, it still will be included many times for the
650compilation of the whole project, so it has nothing to do with precompiled
651headers). wxWindows is no exception and also uses include guards which should use
652the above form, except for top-level headers which include files with identical
653names, in which case you should use _FOO_H_BASE_.
654
655 <P><LI><A NAME="pch"></LI><B>Precompiled headers</B><P>
656The precompiled headers greatly (we're speaking about orders of hundreds of
657percent here) reduce the compilation time. wxWindows uses them if the target
658compiler supports them (it knows about MS Visual C++, Borland C++ and g++).
659You should include all the headers included from <TT><wx/wx_prec.h></TT> only
660inside "<TT>&#35;if !USE_PRECOMP</TT>" to avoid unnecessary overhead in the case
661when the precompiled headers are used.<P>
662
663The start of a cpp implementation file after the heading might look like this:<P>
664
665<PRE>
666&#35;ifdef __GNUG__
667&#35;pragma implementation "bitmap.h"
668&#35;endif
669
670// For compilers that support precompilation, includes "wx.h".
671&#35;include "wx/wxprec.h"
672
673&#35;ifdef __BORLANDC__
674&#35;pragma hdrstop
675&#35;endif
676
677&#35;ifndef WX_PRECOMP
678&#35;include &lt;stdio.h&gt;
679&#35;include "wx/setup.h"
680&#35;include "wx/list.h"
681&#35;include "wx/utils.h"
682&#35;include "wx/app.h"
683&#35;include "wx/palette.h"
684&#35;include "wx/bitmap.h"
685&#35;include "wx/icon.h"
686&#35;endif
687
688&#35;include "wx/msw/private.h"
689&#35;include "assert.h"
690</PRE>
691
692
693<P>Any header file should containg the following lines:
694<PRE>
695&#35;ifdef __GNUG__
696 &#35;pragma interface "foo.h"
697&#35;endif
698</PRE>
699and the corresponding .cpp file:
700<PRE>
701&#35;ifdef __GNUG__
702 &#35;pragma implementation "foo.h"
703&#35;endif
704</PRE> for g++ compilation.
705 </OL>
706
707 <P><LI>File layout and indentation</LI><P>
708 <OL>
709 <P><LI><A NAME="wxwin_header"></LI><B>wxWindows standard header</B> <a href="header.txt">here</a>. The
710copyright holder is the original author. It is assumed the author does not assert copyright,
711under the terms of the wxWindows licence. This is a legal interpretation of the informal
712usage 'public domain' (the copyright holder does not assert the copyright).<P>
713 <P><LI><A NAME="indentation"></LI><B>Indent your code with 4 spaces (no tabs!)</B>
714 <P><LI><A NAME="class_decl"></LI><B>Order of parts in a class declarations</B><P>
715 </OL>
172d3acb 716
97979ddf
JS
717 <P><LI>More about naming conventions</LI><P>
718 <OL>
719 <P><LI><A NAME="wx_prefix"></LI><B>Use wx or WX prefix for all public symbols</B>.
720wx should be used for functions and classes, WX for macros.
172d3acb 721 <P><LI><A NAME="wxdllexport"</LI><B>Use WXDLLEXPORT with all classes/functions in
97979ddf
JS
722 wxMSW/common code</B>
723The title says it all - every public (in the sense that it is not internal to
724the library) function or class should have WXDLLEXPORT macro in its
725declaration to allow compilation of wxWindows as shared library. For example:<P>
726
727<pre>
728bool WXDLLEXPORT wxYield(void);
729class WXDLLEXPORT MyClass; // (for forward declarations and real declarations)
730WXDLLEXPORT_DATA(extern wxApp*) wxTheApp;
731</pre>
732
733The reason for the strange syntax for data is that some compilers use different
734keyword ordering for exporting data.
735
736<P>There also several other places where you should take care of shared
737library case: all IMPLEMENT_xxx macros which are usually used in the
172d3acb 738corresponding .cpp files must be taken inside
97979ddf
JS
739"<TT>&#35;if !USE_SHARED_LIBRARY</TT>" and in the <TT>&#35;if USE_SHARED_LIBRARY</TT>
740case you should put them inside <TT>common/cmndata.cpp</TT> file.
741
742 <P><LI><A NAME="set_get"></LI><B>Use Set/Get prefixes for accessors</B><P>
172d3acb
VZ
743There is a convention in wxWindows to prefix the accessors (i.e. any simple, in
744general, inline function which does nothing else except changing or returning
97979ddf 745the value of a member variable) with either <TT>Set</TT> or <TT>Get</TT>.
172d3acb
VZ
746
747 <P><LI><A NAME="constants"></LI><B>wxNAMING_CONSTANTS</B><P>
748The constants in wxWindows code should be defined using <TT>enum</TT> C++
749keyword (and not with <TT>#define</TT> or <TT>static const int</TT>). They
750should be declared in the global scope (and not inside class declaration) and
751their names should start with a <TT>wx</TT> prefix. Finally, the constants
752should be in all capital letters (except the first 2) to make it easier to
753distinguish them from the variables with underscores separating the words.
754
755<P>For example, file-related constants should be declared like this:
756<pre>
757enum
758{
759 wxFILEOPEN_READ,
760 wxFILEOPEN_WRITE,
761 wxFILEOPEN_READWRITE
762};
763</pre>
764
97979ddf
JS
765 </OL>
766
767 <P><LI>Miscellaneous</LI><P>
768 <OL>
769 <P><LI><A NAME="forward_decl"></LI><B>Use forward declarations whenever possible</B><P>
770It's really a trivial piece of advice, but remember that using forward declarations
771instead of including the header of corresponding class is better because not
772only does it minimize the compile time, it also simplifies the dependencies
773between different source files.
172d3acb 774<P>On a related subject, in general, you should try not to include other
97979ddf
JS
775headers from a header file.
776
777 <P><LI><A NAME="debug_macros"></LI><B>Use debugging macros</B><P>
778wxWindows provides the debugging macros <TT>wxASSERT, wxFAIL</TT> and
779<TT>wxCHECK_RET</TT> in <TT><wx/wx.h></TT> file. Please use them as often as
780you can - they will never do you any harm but can greatly simplify the bug
781tracking both for you and for others.
782<P>Also, please use <TT>wxFAIL_MSG("not implemented")</TT> instead of writing
783stubs for not (yet) implemented functions which silently return incorrect
784values - otherwise, a person using a not implemented function has no idea that
785it is, in fact, not implemented.
786<P>As all debugging macros only do something useful if the symbol
172d3acb 787<TT>__WXDEBUG__</TT> is defined, you should compile your programs in debug mode to profit
97979ddf
JS
788from them.
789 </OL>
790</UL>
791
792<P>
793
794<HR>
795Please send any comments to <A HREF=mailto:zeitlin@dptmaths.ens-cachan.fr>Vadim Zeitlin</A>.
796
797</font>
798
799</BODY>
800</HTML>