This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| dev:dg_code_style_guide [2016/01/20 12:01] – [Naming] deva | dev:dg_code_style_guide [2024/07/26 10:46] (current) – [Classes] deva | ||
|---|---|---|---|
| Line 3: | Line 3: | ||
| | **//Code is written once by one person but read many times by many people.//** | | | **//Code is written once by one person but read many times by many people.//** | | ||
| - | What this essentially boils down to is; don't make syntactic short-cuts that makes it easier to write the code if it makes it harder to read the read. | + | What this essentially boils down to is; don't make syntactic short-cuts that makes it easier to write the code if it makes it harder to read the code. |
| Always write comments to logical blocks in the code that explains for example what a for loop does or how a couple of function calls interact. | Always write comments to logical blocks in the code that explains for example what a for loop does or how a couple of function calls interact. | ||
| Line 15: | Line 15: | ||
| =====Curly Braces===== | =====Curly Braces===== | ||
| Use [[https:// | Use [[https:// | ||
| - | This basically boils down to 'curly braces always on a new line' | + | This basically boils down to 'curly braces always on a new line': |
| <code c++> | <code c++> | ||
| + | namespace Foo | ||
| + | { | ||
| + | |||
| void function() | void function() | ||
| { | { | ||
| Line 24: | Line 27: | ||
| } | } | ||
| } | } | ||
| + | |||
| + | } // Foo:: | ||
| </ | </ | ||
| Line 37: | Line 42: | ||
| { | { | ||
| then_that(); | then_that(); | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | else_this(); | ||
| } | } | ||
| </ | </ | ||
| Line 43: | Line 52: | ||
| Plain switch case: | Plain switch case: | ||
| <code c++> | <code c++> | ||
| - | switch(value) { | + | switch(value) |
| + | { | ||
| case 1: | case 1: | ||
| break; | break; | ||
| Line 55: | Line 65: | ||
| If a variable needs to be declared inside a case further braces are needed. They should be indented as follows: | If a variable needs to be declared inside a case further braces are needed. They should be indented as follows: | ||
| <code c++> | <code c++> | ||
| - | switch(value) { | + | switch(value) |
| + | { | ||
| case 1: | case 1: | ||
| { | { | ||
| - | int myValue | + | int my_value |
| } | } | ||
| break; | break; | ||
| Line 78: | Line 89: | ||
| { | { | ||
| public: | public: | ||
| - | MyClass(int | + | MyClass(int |
| protected: | protected: | ||
| - | int someMember; | + | int some_member; |
| }; | }; | ||
| </ | </ | ||
| <code c++> | <code c++> | ||
| - | MyClass:: | + | MyClass:: |
| - | : someMember(someMember) | + | : some_member(some_member) |
| - | , SomeBaseClass(someMember) | + | , SomeBaseClass(some_member) |
| - | , SomeOtherBaseClass(someMember) | + | , SomeOtherBaseClass(some_member) |
| { | { | ||
| // ... | // ... | ||
| Line 111: | Line 122: | ||
| }; | }; | ||
| </ | </ | ||
| + | |||
| + | Member variables should always be initialized in the header file, and should use the curly-brace notation: | ||
| + | <code c++> | ||
| + | class MyClass | ||
| + | { | ||
| + | private: | ||
| + | int some_member_variable{}; | ||
| + | int start_position{42}; | ||
| + | static constexpr int step_size{2}; | ||
| + | std:: | ||
| + | }; | ||
| + | </ | ||
| + | |||
| =====Pointers and References===== | =====Pointers and References===== | ||
| Pointer stars and reference ampersands must be placed to the left without space after the type: | Pointer stars and reference ampersands must be placed to the left without space after the type: | ||
| Line 121: | Line 145: | ||
| Use [[http:// | Use [[http:// | ||
| - | Tab-width is irrelevant as it is up to the editor | + | < |
| + | --->// | ||
| + | ---> | ||
| + | ---> | ||
| + | ---> | ||
| + | ---> | ||
| + | ---> | ||
| + | </ | ||
| + | |||
| + | With smart-tabs the tab-width | ||
| + | aligned which makes it up to the individual developer | ||
| + | tab-width of his or her taste. | ||
| Download [[https:// | Download [[https:// | ||
| Line 176: | Line 211: | ||
| Always range-based for loops and in cases where these are not available at least use the auto keyword for the iterator type: | Always range-based for loops and in cases where these are not available at least use the auto keyword for the iterator type: | ||
| <code c++> | <code c++> | ||
| - | std:: | + | std:: |
| - | for(auto | + | for(auto |
| { | { | ||
| - | foo(anInt); | + | foo(an_int); |
| } | } | ||
| </ | </ | ||
| Line 188: | Line 223: | ||
| <code c++> | <code c++> | ||
| int w = 70; // <-- BAD | int w = 70; // <-- BAD | ||
| - | int windowWidth | + | int window_width |
| </ | </ | ||
| The following naming schemes apply:\\ | The following naming schemes apply:\\ | ||
| - | // | + | // |
| <code c++> | <code c++> | ||
| class UpperCaseCamelCase | class UpperCaseCamelCase | ||
| Line 200: | Line 235: | ||
| </ | </ | ||
| - | // | + | // |
| <code c++> | <code c++> | ||
| void lowerCaseCamelCase() | void lowerCaseCamelCase() | ||
| </ | </ | ||
| - | // | + | // |
| <code c++> | <code c++> | ||
| int lower_case_snake_case = 42; | int lower_case_snake_case = 42; | ||
| Line 212: | Line 247: | ||
| // | // | ||
| <code c++> | <code c++> | ||
| - | # | + | # |
| </ | </ | ||
| Line 241: | Line 276: | ||
| //! This is a method. | //! This is a method. | ||
| //! \param foo The foo argument is an integer. | //! \param foo The foo argument is an integer. | ||
| - | //! \param bar The bar argument is a boolan. | + | //! \param bar The bar argument is a boolean. |
| //! \return This method returns a float. | //! \return This method returns a float. | ||
| float method(int foo, bool bar); | float method(int foo, bool bar); | ||