DEFAULT STYLE PROGRAMMING - GesielFreitas/CrossRoads-IJE-UnB GitHub Wiki
-
Class, Functions and Variables Declarations
-
Indent Style, braces and operators
-
Header Style
-
Comments
-
Exceptions
-
Good habits
-
Lines and withespaces
-
Statements
For declaration:
1.1. UpperCamelCase: this change the first letter should be upper case obligatorily, normally used in the statements:
- Class name
- Name Of Enums
class gameobject{
}
class button {
}
class GameObject {
}
class Button {
}
enum class audiostate {
}
enum class AudioState {
}
- Variables
- Functions
void MonsterAI::processPos() {
...
}
void MonsterAI::Process_pos() {
...
}
hasDamage = false;
hasdamage = false;
void MonsterAI::process_pos() {
...
}
has_damage = false;
const int player_distance = 850;
const int PlayerDistance = 850;
const int Player_distance = 850;
const int PLAYER_DISTANCE = 850;
Wrong Example:
if ( foo ) {
do_something();
}
Right Example:
if( foo ) {
do_
}
When start a new code block use braces for emulating pure blocks.The opening brace must appear on the same line of the structure who contains it, separated by 2 blank spaces after its end. The closing brace must appear on the line after the block end.
Wrong Example:
void example( foo )
{
...
}
void example( foo ) {...}
Right Example:
void example( foo ) {
...
}
All the operators must be separated from the operands by a blank space.
Wrong example:
int foo=4+4;
int foo= 4+4;
Right example:
int foo = 4 + 4;
All header files should have #define guards to prevent multiple inclusion. The format of the symbol name should be H. To guarantee uniqueness, they should be based on the full path in a project's source tree.
Wrong example:
#define BAZ_H_
...
#endif // BAZ_H_
Right example:
#define FOO_BAR_BAZ_H_
...
#endif // FOO_BAR_BAZ_H_
4.8 Block comments - two or more lines - should begin / * followed the comment and closed with * / in new line. The content of the comment should be idented in relation to bookmarks /* */.
Wrong Example:
// Loop to iterate in the range of table
for (i in range 0,10) {
...
};
//Imprimindo a interação
std::cout << i ;
// Returns an iterator for this table. It is the client's responsibility to delete the iterator when it is done with it, and it must not use the iterator once the GargantuanTable object on which the iterator was created has been deleted.
// Returns an iterator for this table. It is the client's
// responsibility to delete the iterator when it is done with it,
// and it must not use the iterator once the GargantuanTable object
// on which the iterator was created has been deleted.
/*
Returns an iterator for this table. It is the client's
responsibility to delete the iterator when it is done with it,
and it must not use the iterator once the GargantuanTable object
on which the iterator was created has been deleted.
*/
Right Example:
// Loop to iterate in the range of table
for (i in range 0,10) {
...
};
// Printing the iterator
std::cout << i ;
/*
Returns an iterator for this table. It is the client's
responsibility to delete the iterator when it is done with it,
and it must not use the iterator once the GargantuanTable object
on which the iterator was created has been deleted.
*/
Wrong Example:
assert(false);
Right Example:
assert( !variable );
Reasons why exceptions is better than errors:
- Exceptions can't be silently ignored;
- Exceptions propagate automatically;
- Exception handling removes error handling and recovery from the main line of control flow;
- Exception handling is better than the alternatives for reporting errors from constructors and operators.
Wrong Example:
catch( MyException& e ) {
e.AppendContext("Passed through here");
throw e;
}
Right Example:
catch( MyException& e ) {
e.AppendContext("Passed through here");
throw;
}
Wrong Example:
class Abc {
virtual f() throw( X, Y, Z);
};
Right Example:
class MyDerived : public Abc {
virtual f() throw( X, Y, Z);
};
This section aims describe good habits that should be adopted on a daily basis in programmer routine for the purpose of generate code safe, clean and principally easy to maintain.
- Follow the style sheet;
- Prioritize clarity rather than conciseness:
- Prioritize simple resolutions;
- Break the code into smaller pieces;
- Break arithmetic expressions;
- Reduce scope
- Protect data and internal functions;
- Avoid using global variables;
- Never ignore compiler warnings;
- Use secure data structures;
- Check all return values;
- Fix errors received
- Release the resources(memory, threads, files, locks...) that are no more used;
- Initialize all variables in declaration;
- Declare the variables as close to their use as possible;
- Don't reuse the auxiliary variables name;
- Don't abbreviate the names of variables, functions, classes, and constants
- Use standard language sentences and resources.
The code must be separated into logical paragraphs by a blank line, so as to make clear the identification of parts of the code, such as initialization of variables, execution of a particular procedure, etc.
Matrix4x4 matrix = new Matrix4x4();
double cosAngle = Math.cos(angle);
double sinAngle = Math.sin(angle);
matrix.setElement(1, 1, cosAngle);
matrix.setElement(1, 2, sinAngle);
matrix.setElement(2, 1, -sinAngle);
matrix.setElement(2, 2, cosAngle);
multiply(matrix);
Matrix4x4 matrix = new Matrix4x4();
double cosAngle = Math.cos(angle);
double sinAngle = Math.sin(angle);
matrix.setElement(1, 1, cosAngle);
matrix.setElement(1, 2, sinAngle);
matrix.setElement(2, 1, -sinAngle);
matrix.setElement(2, 2, cosAngle);
multiply(matrix);
#include <iostream>
using namespace std;
void first_function() {
...
}
void second_function() {
...
}
#include <iostream>
using namespace std;
void first_function() {
...
}
void second_function() {
...
}
bool first_function( int value, int max ) {
if( value > max ) {
return false;
}
return true;
}
class Rectangle {
int width, height;
public:
void set_values (int,int);
int area (void);
} rect;
bool first_function( int value, int max ) {
if( value > max ) {
return false;
}
return true;
}
class Rectangle {
int width, height;
public:
void set_values (int,int);
int area (void);
} rect;
7.4 In decision structures and loop, the statements should begin in next line after structure declaration
if( value > max ) {
return false;
}
while( value < max ) {
cout << value << endl;
}
if( value > max ) {
return false;
}
while( value < max ) {
cout << value << endl;
}
7.5 In if and while statements if the parenthesis content is not empty, a blank space should be added after opening the parenthesis and a blank before closing
if(value > max) {
...
}
while(value < max ) {
}
move_monster( );
if( value > max ) {
...
}
while( value < max ) {
}
move_monster();
if ( value > max ) {
...
}
if( value > max ) {
...
}
if ( value > max ){
...
}
if( value > max ) {
...
}
if ( value > max ){
...
}
if( value > max ) {
...
}
#include"monsterAI.hpp"
#include<stdio.h>
#include "monsterAI.hpp"
#include <stdio.h>
7.10 A line can be a maximum of 80 characters. If it is necessary to break the line it must be indented according to its the origin of its sentence
if( _main_game_object->main_positionX<0 || _main_game_object->main_positionX+_main_game_object->main_width>800 )
if( _main_game_object->main_positionX<0 ||
_main_game_object->main_positionX+_main_game_object->main_width>800 )
7.11 Parameters of functions should not contain spaces in relation to parenthesis. If there is more than one parameter, a whitespace should be included after each separating comma
m_monster_controler->play_animation("monster_walk",true);
m_monster_controler->play_animation( "monster_walk", true );
m_monster_controler->play_animation( "monster_damage" );
m_monster_controler->play_animation("monster_walk", true);
m_monster_controler->play_animation("monster_damage");
compound_key = (registration
+ id);
compound_key = (registration +
id);
8.1 If statements should always mark the block with keys. In the case of keywords else and else if you should always start them on the next line after the key is closed.
if( !has_ground() ) {
...
} else {
...
}
if( !has_ground() ) {
...
}
else {
...
}
8.2 The initialization of variances of a class by passing a list of parameters to the constructor must have a blank before and after the colon - if it fits on a single line. If more than one line is required, the list must start on the next line and be indented according to the parameters of the constructor method
Heart(GameObject &main_game_objecte): Component(main_game_object, id) {}
Heart(GameObject &main_game_objecte) :
Component(main_game_object, id) {}
Heart(GameObject &main_game_object, std::string id, Player* player, int life):
Component(main_game_object, id), m_player(player), m_life(life) {}
Heart(GameObject &main_game_objecte) : Component(main_game_object, id) {}
Heart(GameObject &main_game_object, std::string id, Player* player, int life) :
Component(main_game_object, id), m_player(player), m_life(life) {}
GameObject * ground;
GameObject* ground;
Player(GameObject & main_game_object);
GameObject *ground;
Player(GameObject &main_game_object);
8.4 Dynamic cast should not have spaces inside the '<' and '>' operators and should contain a whitespace after the cast is closed
AudioComponent *boss_dash_audio = (dynamic_cast<AudioComponenti*>());
AudioComponent *boss_dash_audio = (dynamic_cast< AudioComponenti* > ());
AudioComponent *boss_dash_audio = (dynamic_cast<AudioComponenti *> ());
AudioComponent *boss_dash_audio = (dynamic_cast<AudioComponenti*> ());