]> git.saurik.com Git - wxWidgets.git/blame - docs/doxygen/mainpages/strategies.h
correct hhp2cached path
[wxWidgets.git] / docs / doxygen / mainpages / strategies.h
CommitLineData
4514447c
FM
1/////////////////////////////////////////////////////////////////////////////
2// Name: strategies.h
3// Purpose: Strategies page of the Doxygen manual
4// Author: wxWidgets team
5// RCS-ID: $Id$
6// Licence: wxWindows license
7/////////////////////////////////////////////////////////////////////////////
8
9
880efa2a 10/**
4514447c 11
928f1a07 12@page page_strategies Programming strategies
4514447c 13
928f1a07
FM
14This chapter is intended to list strategies that may be useful when
15writing and debugging wxWidgets programs. If you have any good tips,
16please submit them for inclusion here.
4514447c 17
928f1a07
FM
18@li @ref page_strategies_reducingerr
19@li @ref page_strategies_portability
20@li @ref page_strategies_debug
4514447c
FM
21
22
928f1a07 23<hr>
4514447c
FM
24
25
928f1a07 26@section page_strategies_reducingerr Strategies for reducing programming errors
4514447c 27
928f1a07 28@subsection page_strategies_reducingerr_useassert Use ASSERT
4514447c 29
928f1a07
FM
30It is good practice to use ASSERT statements liberally, that check for conditions
31that should or should not hold, and print out appropriate error messages.
4514447c 32
928f1a07
FM
33These can be compiled out of a non-debugging version of wxWidgets
34and your application. Using ASSERT is an example of `defensive programming':
35it can alert you to problems later on.
4514447c 36
928f1a07 37See wxASSERT for more info.
4514447c 38
928f1a07 39@subsection page_strategies_reducingerr_usewxstring Use wxString in preference to character arrays
4514447c 40
928f1a07 41Using wxString can be much safer and more convenient than using wxChar *.
4514447c 42
928f1a07
FM
43You can reduce the possibility of memory leaks substantially, and it is much more
44convenient to use the overloaded operators than functions such as @c strcmp.
45wxString won't add a significant overhead to your program; the overhead is compensated
46for by easier manipulation (which means less code).
4514447c 47
928f1a07 48The same goes for other data types: use classes wherever possible.
4514447c
FM
49
50
51
928f1a07 52@section page_strategies_portability Strategies for portability
4514447c 53
928f1a07 54@subsection page_strategies_portability_usesizers Use sizers
4514447c 55
928f1a07
FM
56Don't use absolute panel item positioning if you can avoid it. Different GUIs have
57very differently sized panel items. Consider using the @ref overview_sizer instead.
4514447c 58
928f1a07 59@subsection page_strategies_portability_useresources Use wxWidgets resource files
4514447c 60
928f1a07
FM
61Use .xrc (wxWidgets resource files) where possible, because they can be easily changed
62independently of source code. See the @ref overview_xrc for more info.
4514447c
FM
63
64
65
928f1a07 66@section page_strategies_debug Strategies for debugging
4514447c 67
928f1a07 68@subsection page_strategies_debug_positivethinking Positive thinking
4514447c 69
928f1a07
FM
70It is common to blow up the problem in one's imagination, so that it seems to threaten
71weeks, months or even years of work. The problem you face may seem insurmountable:
72but almost never is. Once you have been programming for some time, you will be able
73to remember similar incidents that threw you into the depths of despair. But
74remember, you always solved the problem, somehow!
4514447c 75
928f1a07
FM
76Perseverance is often the key, even though a seemingly trivial problem
77can take an apparently inordinate amount of time to solve. In the end,
78you will probably wonder why you worried so much. That's not to say it
79isn't painful at the time. Try not to worry -- there are many more important
80things in life.
4514447c 81
928f1a07 82@subsection page_strategies_debug_simplifyproblem Simplify the problem
4514447c 83
928f1a07
FM
84Reduce the code exhibiting the problem to the smallest program possible
85that exhibits the problem. If it is not possible to reduce a large and
86complex program to a very small program, then try to ensure your code
87doesn't hide the problem (you may have attempted to minimize the problem
88in some way: but now you want to expose it).
4514447c 89
928f1a07
FM
90With luck, you can add a small amount of code that causes the program
91to go from functioning to non-functioning state. This should give a clue
92to the problem. In some cases though, such as memory leaks or wrong
93deallocation, this can still give totally spurious results!
4514447c 94
928f1a07 95@subsection page_strategies_debug_usedebugger Use a debugger
4514447c 96
928f1a07
FM
97This sounds like facetious advice, but it is surprising how often people
98don't use a debugger. Often it is an overhead to install or learn how to
99use a debugger, but it really is essential for anything but the most
100trivial programs.
4514447c 101
928f1a07 102@subsection page_strategies_debug_uselogging Use logging functions
4514447c 103
928f1a07
FM
104There is a variety of logging functions that you can use in your program:
105see @ref page_func_cat_log.
4514447c 106
928f1a07
FM
107Using tracing statements may be more convenient than using the debugger
108in some circumstances (such as when your debugger doesn't support a lot
109of debugging code, or you wish to print a bunch of variables).
4514447c 110
928f1a07 111@subsection page_strategies_debug_usedebuggingfacilities Use the wxWidgets debugging facilities
4514447c 112
928f1a07
FM
113You can use wxDebugContext to check for
114memory leaks and corrupt memory: in fact in debugging mode, wxWidgets will
115automatically check for memory leaks at the end of the program if wxWidgets is suitably
116configured. Depending on the operating system and compiler, more or less
117specific information about the problem will be logged.
4514447c 118
928f1a07
FM
119You should also use @ref page_macro_cat_debugging as part of a `defensive programming' strategy,
120scattering wxASSERTs liberally to test for problems in your code as early as possible.
121Forward thinking will save a surprising amount of time in the long run.
4514447c 122
928f1a07 123See the @ref overview_debugging for further information.
4514447c
FM
124
125*/