Difference between revisions of "CodingStandards"
(→Do not use #define for constant variable: Formatting, wording) |
(Formatting) |
||
Line 1: | Line 1: | ||
− | Wesnoth uses modern/advanced C++ that is portable to modern C++ compilers. | + | Wesnoth uses modern/advanced C++ that is portable to modern C++ compilers targeting various commonly used platforms. |
== Formatting == | == Formatting == | ||
Line 7: | Line 7: | ||
You may use long lines. | You may use long lines. | ||
− | == Evil | + | == Evil things to avoid == |
=== Avoid implicit conversions === | === Avoid implicit conversions === | ||
− | Make all constructors which only take one argument that is of a different type to the class | + | Make all constructors which only take one argument that is of a different type to the class <tt>explicit</tt>. |
− | Do not use operator T() where T is a type to allow an implicit conversion to a different type. | + | Do not use <tt>operator T()</tt> (where <tt>T</tt> is a type) to allow an implicit conversion to a different type. For example: |
− | |||
− | |||
t_string(const std::string&); | t_string(const std::string&); | ||
− | This | + | This can cause many situations where a temporary t_string is implicitly created and then gets destroyed unexpectedly. |
− | === Do not | + | === Do not declare class data members as non-private === |
− | It's okay to have a struct with | + | It's okay to have a ''struct'' with only public members, if that's what you want. |
− | However, once something is a class | + | However, once something is a ''class'' with private data members, do not add public (or even protected) data members to the class. Doing this breaks encapsulation and can cause all kinds of confusing and evil things to happen. |
== Naming == | == Naming == | ||
Line 37: | Line 35: | ||
=== Use references when a value may not be NULL === | === Use references when a value may not be NULL === | ||
− | If a value passed to a function can never be NULL, use a reference instead of a pointer. | + | If a value passed to a function can never be <tt>NULL</tt>, use a reference instead of a pointer. For example: |
− | void | + | void my_function(T& obj); |
rather than | rather than | ||
− | void | + | void my_function(T* obj); |
− | This more clearly shows | + | This more clearly shows prospective users of the function that <tt>obj</tt> may never be <tt>NULL</tt>, without them having to consult documentation or the implementation of the function. |
− | without them having to consult documentation or the implementation of the function. | ||
=== Use the const keyword === | === Use the const keyword === | ||
− | The | + | The <tt>const</tt> keyword in C++ allows interfaces to more clearly specify how they treat objects. |
− | Always use const when you are not going to modify an object. | + | Always use <tt>const</tt> when you are not going to modify an object. For example: |
− | + | void my_function(const T& obj); | |
− | + | This shows to the caller that <tt>obj</tt> will not be modified. If <tt>my_function()</tt> may modify <tt>obj</tt>, then use the following instead: | |
− | + | void my_function(T& obj); | |
− | |||
− | + | Likewise, if a variable is not changed after initialization, make it <tt>const</tt>. | |
− | + | === Know the behavior of const references when types differ === | |
− | + | If you assign something to a <tt>const</tt> reference of a different type, if necessary (if the type is different but there is a conversion) the compiler will create a temporary and guarantee it lasts for the lifetime of the reference. So | |
− | If you assign something to a const reference of a different type, if necessary (if the type is different but there is a conversion) the compiler will create a temporary and guarantee it lasts for the lifetime of the reference. So | ||
− | + | char c = 0; | |
+ | const int& i = c; | ||
+ | c = 5; | ||
− | will result in c == 5 and i == 0 which may not be what you expect. | + | will result in c == 5 and i == 0, which may not be what you expect. |
=== Write exception-safe code === | === Write exception-safe code === | ||
Line 75: | Line 72: | ||
Wesnoth code should be exception-safe, even if you do not use exceptions directly. That is, you should be able to assume that an exception is thrown almost anywhere from within the code, with well-defined results (i.e. no resource leaks). | Wesnoth code should be exception-safe, even if you do not use exceptions directly. That is, you should be able to assume that an exception is thrown almost anywhere from within the code, with well-defined results (i.e. no resource leaks). | ||
− | Code that uses a pattern like | + | Code that uses a pattern like the following is bad: |
{ | { | ||
Line 83: | Line 80: | ||
} | } | ||
− | + | The code may throw an exception, and <tt>image</tt> will never be freed. Instead, use wrapper objects which free the object in their destructor. | |
− | Instead, use wrapper objects which free the object in their destructor. | ||
− | For SDL_Surface objects, | + | For <tt>SDL_Surface</tt> objects, the <tt>surface</tt> type is used throughout the Wesnoth source code to achieve this purpose. So you could rewrite the above code as follows: |
− | So you could rewrite the above code | ||
{ | { | ||
Line 94: | Line 89: | ||
} ''the image is automatically freed here when 'image' is destroyed | } ''the image is automatically freed here when 'image' is destroyed | ||
− | Instead of allocating memory directly using new[] or malloc(), use language-provided containers, such as vector. | + | Instead of allocating memory directly using <tt>new[]</tt> or <tt>malloc()</tt>, use language-provided containers, such as vector. |
=== Do not use sprintf === | === Do not use sprintf === | ||
− | + | The <tt>sprintf()</tt> function does not check whether or not it is writing past the end of the space allocated. This is a security problem if someone other than the person running the game can cause <tt>sprintf()</tt> to write very long strings. In Wesnoth, this untrusted data could come potentially from other players in a multiplayer game, or from downloaded add-ons. Instead you should use <tt>snprintf()</tt> with the second argument being the <tt>sizeof</tt> of the buffer that will hold the result. | |
− | This is a security problem if someone other than the person running the game | ||
− | can cause sprintf to write very long strings. | ||
− | In Wesnoth this untrusted data could come potentially from other players | ||
− | in a multiplayer game or from downloaded | ||
− | Instead you should use snprintf with the second argument being sizeof of the buffer | ||
− | that will hold the result. | ||
== Standard C++ to avoid == | == Standard C++ to avoid == | ||
Line 110: | Line 99: | ||
=== Do not use wstring === | === Do not use wstring === | ||
− | The standard C++ wstring class | + | The standard C++ <tt>std::wstring</tt> class (defined as a <tt>std::basic_string< wchar_t ></tt>) does not exist in some platforms supported by Wesnoth. Use <tt>wide_string</tt> instead (defined in <tt>language.hpp</tt>). The <tt>wide_string</tt> type is actually defined as <tt>std::vector< wchar_t ></tt>. |
− | does not exist in some platforms supported by Wesnoth. | ||
− | Use wide_string | ||
− | wide_string is actually defined as | ||
=== Do not use 0 when you mean NULL === | === Do not use 0 when you mean NULL === | ||
Line 120: | Line 106: | ||
== C legacy to be avoided == | == C legacy to be avoided == | ||
− | |||
− | |||
=== Use util::array instead of C-style Arrays === | === Use util::array instead of C-style Arrays === | ||
− | C-style arrays are very efficient, but their interface is ugly. | + | C-style arrays are very efficient, but their interface is ugly. Use <tt>util::array</tt> defined in <tt>array.hpp</tt> instead. It is a wrapper for an array which has a C++ container-style interface. If you need to, extend it to make it fit your needs. |
− | Use util::array defined in array.hpp. | ||
− | It is a wrapper for an array which has a C++ container-style interface. | ||
− | If you need to, extend it to make it fit your needs. | ||
=== Do not use C-style casts === | === Do not use C-style casts === | ||
Line 134: | Line 115: | ||
The following code, | The following code, | ||
− | + | if(i->second.side() == (size_t)player_number_) { | |
− | is considered bad practice in C++ since a C-style cast is overpowered -- if types change around it could end up casting away constness, or performing an implementation-defined data reinterpretation (basically a C-style cast is a compiler generated combination of static_cast, reinterpret_cast, and const_cast). | + | is considered bad practice in C++ since a C-style cast is overpowered -- if types change around it could end up casting away constness, or performing an implementation-defined data reinterpretation (basically a C-style cast is a compiler-generated combination of <tt>static_cast</tt>, <tt>reinterpret_cast</tt>, and <tt>const_cast</tt>). |
− | Good programming style is to use the least powerful tool available that does what you want. | + | Good programming style is to use the least powerful tool available that does what you want. For example: |
− | For example | ||
− | + | if(i->second.side() == static_cast<size_t>(player_number_)) { | |
− | Alternatively, a constructor call may be used for non- | + | Alternatively, a constructor call may be used for non-built-in types. |
− | Note: there may be some obscure cases where a C-style cast is desirable, | + | ''Note: there may be some obscure cases where a C-style cast is desirable, such as converting a pointer to an integer type of unspecified size.'' |
− | such as converting a pointer to an integer type of unspecified size. | ||
=== Do not use #define for constants === | === Do not use #define for constants === | ||
− | <nowiki>#</nowiki>define foo X is not a typesafe approach to define constants. Instead, you can something like the following (in an anonymous namespace) to achieve the same goal in a typesafe fashion. | + | <tt><nowiki>#</nowiki>define foo X</tt> is not a typesafe approach to define constants. Instead, you can something like the following (in an anonymous namespace) to achieve the same goal in a typesafe fashion. |
namespace { | namespace { | ||
Line 158: | Line 137: | ||
== Documentation == | == Documentation == | ||
− | === Document | + | === Document config preconditions and postconditions === |
− | In the Wesnoth code you will commonly encounter a data container known as | + | In the Wesnoth code you will commonly encounter a data container type known as <tt>config</tt>, which contains hierarchical string data (such as WML contents or game settings). The tagged ''children'' of the <tt>config</tt> object and their string ''attributes'' are arranged in an ordered and mapped format, internally implemented using the C++ STL. |
− | which contains | ||
− | The tagged | ||
− | in an ordered and mapped format internally using STL. | ||
− | Because config data is utilized in so many ways and places, | + | Because <tt>config</tt> data is utilized in so many ways and places, it can be difficult to track across the scope of the entire program. Thus, you should document all public functions that take/return <tt>config</tt> objects, specifying content expectations and updating any related entries in the [[ReferenceWML]] wiki pages. In particular, if your function requires a <tt>config</tt> parameter, specify where/how the <tt>config</tt> object should be created. This will be a great help to any future coders who need to call or modify your function. |
− | In particular, if your function requires a config parameter, specify where/how the config should be created. This will be a great help to any future coders who need to call or modify your function. | ||
=== Doxygen === | === Doxygen === | ||
− | See [[Doxygen]] for tips on how to comment the code, | + | |
− | so that | + | See [[Doxygen]] for tips on how to comment the code, so that Doxygen can nicely document it. |
== See also == | == See also == | ||
+ | |||
* [[HackingWesnoth]] | * [[HackingWesnoth]] | ||
[[Category:Development]] | [[Category:Development]] |
Revision as of 04:08, 25 February 2012
Wesnoth uses modern/advanced C++ that is portable to modern C++ compilers targeting various commonly used platforms.
Contents
Formatting
When working on C++ for Wesnoth, indent your code with a tab character. After fully indenting, if you still need to line up the text with a specific character on the line above, you may further align it using space characters.
You may use long lines.
Evil things to avoid
Avoid implicit conversions
Make all constructors which only take one argument that is of a different type to the class explicit.
Do not use operator T() (where T is a type) to allow an implicit conversion to a different type. For example:
t_string(const std::string&);
This can cause many situations where a temporary t_string is implicitly created and then gets destroyed unexpectedly.
Do not declare class data members as non-private
It's okay to have a struct with only public members, if that's what you want.
However, once something is a class with private data members, do not add public (or even protected) data members to the class. Doing this breaks encapsulation and can cause all kinds of confusing and evil things to happen.
Naming
End non-public class data members with an underscore
All non-public data members of classes should have their names terminated with an underscore, to show that they are a class member. This makes for more readable code, once one is familiar with the convention.
Idioms
Use references when a value may not be NULL
If a value passed to a function can never be NULL, use a reference instead of a pointer. For example:
void my_function(T& obj);
rather than
void my_function(T* obj);
This more clearly shows prospective users of the function that obj may never be NULL, without them having to consult documentation or the implementation of the function.
Use the const keyword
The const keyword in C++ allows interfaces to more clearly specify how they treat objects. Always use const when you are not going to modify an object. For example:
void my_function(const T& obj);
This shows to the caller that obj will not be modified. If my_function() may modify obj, then use the following instead:
void my_function(T& obj);
Likewise, if a variable is not changed after initialization, make it const.
Know the behavior of const references when types differ
If you assign something to a const reference of a different type, if necessary (if the type is different but there is a conversion) the compiler will create a temporary and guarantee it lasts for the lifetime of the reference. So
char c = 0; const int& i = c; c = 5;
will result in c == 5 and i == 0, which may not be what you expect.
Write exception-safe code
Wesnoth code should be exception-safe, even if you do not use exceptions directly. That is, you should be able to assume that an exception is thrown almost anywhere from within the code, with well-defined results (i.e. no resource leaks).
Code that uses a pattern like the following is bad:
{ SDL_Surface* image = IMG_Load("image.bmp"); ...some code, which uses 'image'... SDL_FreeSurface(image); }
The code may throw an exception, and image will never be freed. Instead, use wrapper objects which free the object in their destructor.
For SDL_Surface objects, the surface type is used throughout the Wesnoth source code to achieve this purpose. So you could rewrite the above code as follows:
{ surface image(IMG_Load("image.bmp")); ...some code, which uses 'image'... } the image is automatically freed here when 'image' is destroyed
Instead of allocating memory directly using new[] or malloc(), use language-provided containers, such as vector.
Do not use sprintf
The sprintf() function does not check whether or not it is writing past the end of the space allocated. This is a security problem if someone other than the person running the game can cause sprintf() to write very long strings. In Wesnoth, this untrusted data could come potentially from other players in a multiplayer game, or from downloaded add-ons. Instead you should use snprintf() with the second argument being the sizeof of the buffer that will hold the result.
Standard C++ to avoid
Do not use wstring
The standard C++ std::wstring class (defined as a std::basic_string< wchar_t >) does not exist in some platforms supported by Wesnoth. Use wide_string instead (defined in language.hpp). The wide_string type is actually defined as std::vector< wchar_t >.
Do not use 0 when you mean NULL
Several Wesnoth developers, including Dave, find the number 0 to be very ambiguous when used in a non-numeric context. In keeping with the precedent that has already been established in the Wesnoth source code, you should avoid using literal zero for initializing and/or comparing null pointers.
C legacy to be avoided
Use util::array instead of C-style Arrays
C-style arrays are very efficient, but their interface is ugly. Use util::array defined in array.hpp instead. It is a wrapper for an array which has a C++ container-style interface. If you need to, extend it to make it fit your needs.
Do not use C-style casts
The following code,
if(i->second.side() == (size_t)player_number_) {
is considered bad practice in C++ since a C-style cast is overpowered -- if types change around it could end up casting away constness, or performing an implementation-defined data reinterpretation (basically a C-style cast is a compiler-generated combination of static_cast, reinterpret_cast, and const_cast).
Good programming style is to use the least powerful tool available that does what you want. For example:
if(i->second.side() == static_cast<size_t>(player_number_)) {
Alternatively, a constructor call may be used for non-built-in types.
Note: there may be some obscure cases where a C-style cast is desirable, such as converting a pointer to an integer type of unspecified size.
Do not use #define for constants
#define foo X is not a typesafe approach to define constants. Instead, you can something like the following (in an anonymous namespace) to achieve the same goal in a typesafe fashion.
namespace { const T foo = X; }
Documentation
Document config preconditions and postconditions
In the Wesnoth code you will commonly encounter a data container type known as config, which contains hierarchical string data (such as WML contents or game settings). The tagged children of the config object and their string attributes are arranged in an ordered and mapped format, internally implemented using the C++ STL.
Because config data is utilized in so many ways and places, it can be difficult to track across the scope of the entire program. Thus, you should document all public functions that take/return config objects, specifying content expectations and updating any related entries in the ReferenceWML wiki pages. In particular, if your function requires a config parameter, specify where/how the config object should be created. This will be a great help to any future coders who need to call or modify your function.
Doxygen
See Doxygen for tips on how to comment the code, so that Doxygen can nicely document it.