KWWidgets/Projects/UIDesigner/KWWidgetsCodingStyle: Difference between revisions
From KitwarePublic
Jump to navigationJump to search
Line 9: | Line 9: | ||
:<u><b>Abbreviations</b></u> | :<u><b>Abbreviations</b></u> | ||
::Avoid using abbreviated words, as it is hard to read or pronounce. | ::Avoid using abbreviated words, as it is hard to read or pronounce. | ||
::For example: writte vtkKWMyLocalVariableNamedAlpha instead of vtkKWMyLclVrBlNmdAlph. | ::For example: writte vtkKWMyLocalVariableNamedAlpha instead of vtkKWMyLclVrBlNmdAlph. | ||
::Abbreviations can be used, but it should be entirely in capital letters, for example vtkLODActor, where LOD means 'Level Of Detail'.<br><br> | ::Abbreviations can be used, but it should be entirely in capital letters, for example vtkLODActor, where LOD means 'Level Of Detail'.<br><br> | ||
:<u><b>Local variables</b></u> | :<u><b>Local variables</b></u> | ||
::Their name MUST begin with a lowercase letter. | ::Their name MUST begin with a lowercase letter. | ||
::To separate words either use an underscore, or capitalize the first letter of each new word. For example: my_local_variable_alpha or myLocalVariableAlpha. | ::To separate words either use an underscore, or capitalize the first letter of each new word. For example: my_local_variable_alpha or myLocalVariableAlpha. | ||
::Barre *honours* best the first approach in KWWidgets. | ::Barre *honours* best the first approach in KWWidgets. | ||
::''Note: In VTK only pre-processor variables, written ALL in capital letters, use the underscore to separate words.''<br><br> | ::''Note: In VTK only pre-processor variables, written ALL in capital letters, use the underscore to separate words.''<br><br> | ||
:<u><b>The class pointer 'this'</b></u> | :<u><b>The class pointer 'this'</b></u> | ||
::ALWAYS explicit the 'this' pointer for EVERY occurrence of a class member variable and/or method. | ::ALWAYS explicit the 'this' pointer for EVERY occurrence of a class member variable and/or method. | ||
::For example: this->vtkKWMyClassMemberVar or this->vtkKWMyClassSomeMethod().<br><br> | ::For example: this->vtkKWMyClassMemberVar or this->vtkKWMyClassSomeMethod().<br><br> | ||
:<u><b>Comments</b></u> | :<u><b>Comments</b></u> | ||
::When using the C/C++ single-line comment style, place | ::When using the C/C++ single-line comment style, place at least ONE space after the two slashes. | ||
::For example: | ::For example: | ||
:::// The word comment commes from the late | :::// The word comment commes from the late | ||
Line 31: | Line 38: | ||
:<u><b>Methods/Functions Delimitation</b></u> | :<u><b>Methods/Functions Delimitation</b></u> | ||
::Delimit the begining of each class method by placing a separator before each implementation. This helps readability of the code. | ::Delimit the begining of each class method by placing a separator before each implementation. This helps readability of the code. | ||
::For example: | ::For example: | ||
:://-------------------------------------------- | :://-------------------------------------------- | ||
Line 73: | Line 81: | ||
:<u><b>Forward declaration</b></u> | :<u><b>Forward declaration</b></u> | ||
::In your header files, do not include the headers of the classes you are using as member. Instead of that you should forward-declare them. This technique allows faster compilations, because only the super-class header file is included. It also reduces a lot the set of dependencies, meaning that the depencencies check are much faster, and if you modify any of those other header files, less files will get recompiled. | ::In your header files, do not include the headers of the classes you are using as member. Instead of that you should forward-declare them. This technique allows faster compilations, because only the super-class header file is included. It also reduces a lot the set of dependencies, meaning that the depencencies check are much faster, and if you modify any of those other header files, less files will get recompiled. | ||
::For example use: | ::For example use: | ||
::: #include <vtkKWApplication.h> | ::: #include <vtkKWApplication.h> | ||
Line 87: | Line 96: | ||
::: vtkKWYourExistingBethaClass* Beta; | ::: vtkKWYourExistingBethaClass* Beta; | ||
::: vtkKWYourExistingPhiClass* Phi; | ::: vtkKWYourExistingPhiClass* Phi; | ||
::Instead of the "classic": | ::Instead of the "classic": | ||
::: #include <vtkKWApplication.h> | ::: #include <vtkKWApplication.h> | ||
Line 107: | Line 117: | ||
:<u><b>The 'bool'-ean type</b></u> | :<u><b>The 'bool'-ean type</b></u> | ||
::Do not use 'bool' in the header files, as it can not be wrapped properly. This is IMPORTANT. Use 'int' instead. | ::Do not use 'bool' in the header files, as it can not be wrapped properly. This is IMPORTANT. Use 'int' instead. | ||
::For example use: | ::For example use: | ||
:::int isMyPropertySet(); // Returns 1 if my property is set, else it returns 0 | :::int isMyPropertySet(); // Returns 1 if my property is set, else it returns 0 | ||
::Instead of: | ::Instead of: | ||
:::bool isMyPropertySet(); // Returns true if my property is set, else it returns false | :::bool isMyPropertySet(); // Returns true if my property is set, else it returns false | ||
::You can use 'bool' in your .cxx files, if you need to, especially if you use the STL for your datastructure --''STL utilization is strongly recommend by Barre''--, but in one should normally use 'int' for standard boolean operations.<br><br> | ::You can use 'bool' in your .cxx files, if you need to, especially if you use the STL for your datastructure --''STL utilization is strongly recommend by Barre''--, but in one should normally use 'int' for standard boolean operations.<br><br> | ||
:<u><b>The VTK error, debug and warning macros</b></u> | :<u><b>The VTK error, debug and warning macros</b></u> | ||
::Take advantage of the 'vtkErrorMacro', 'vtkDebugMacro', 'vtkWarning' macros, defined in the 'vtkSetGet.h' VTK header file, to indicate error, debug and/or warning messages inside your classes. | ::Take advantage of the 'vtkErrorMacro', 'vtkDebugMacro', 'vtkWarning' macros, defined in the 'vtkSetGet.h' VTK header file, to indicate error, debug and/or warning messages inside your classes. | ||
::Besides the advantages of using pre-defined macros, you will benefit from the fact that you can simply turn ALL these message on and off by conviniently calling the 'vtkObject::SetGlobalWarningDisplay()' static method.<br><br> | ::Besides the advantages of using pre-defined macros, you will benefit from the fact that you can simply turn ALL these message on and off by conviniently calling the 'vtkObject::SetGlobalWarningDisplay()' static method.<br><br> |
Revision as of 17:18, 12 May 2006
VTK/KWWidgets Coding Style
This section is a personnal and unofficial compilation of some of the coding style convention used in VTK/KWWidgets. It it based on the contents of e-mails exchanged with Sebastien Barre, the KWWidgets conceptor and main developer.
You can find a more official source in chapter 9, Contributing Code, of the VTK User's Guide book.
That being said here is the list:
- Abbreviations
- Avoid using abbreviated words, as it is hard to read or pronounce.
- For example: writte vtkKWMyLocalVariableNamedAlpha instead of vtkKWMyLclVrBlNmdAlph.
- Abbreviations can be used, but it should be entirely in capital letters, for example vtkLODActor, where LOD means 'Level Of Detail'.
- Abbreviations can be used, but it should be entirely in capital letters, for example vtkLODActor, where LOD means 'Level Of Detail'.
- Local variables
- Their name MUST begin with a lowercase letter.
- To separate words either use an underscore, or capitalize the first letter of each new word. For example: my_local_variable_alpha or myLocalVariableAlpha.
- Barre *honours* best the first approach in KWWidgets.
- Note: In VTK only pre-processor variables, written ALL in capital letters, use the underscore to separate words.
- Note: In VTK only pre-processor variables, written ALL in capital letters, use the underscore to separate words.
- The class pointer 'this'
- ALWAYS explicit the 'this' pointer for EVERY occurrence of a class member variable and/or method.
- For example: this->vtkKWMyClassMemberVar or this->vtkKWMyClassSomeMethod().
- For example: this->vtkKWMyClassMemberVar or this->vtkKWMyClassSomeMethod().
- Comments
- When using the C/C++ single-line comment style, place at least ONE space after the two slashes.
- For example:
- // The word comment commes from the late
- // Latin word 'commentum', meaning interpretation
- // ....
- For example:
- Methods/Functions Delimitation
- Delimit the begining of each class method by placing a separator before each implementation. This helps readability of the code.
- For example:
- //--------------------------------------------
- void vtkKWMyClass::vtkKWMyClassMyMethodAlpha()
- {
- std::cerr << "The 'vtkKWMyClassMyMethodAlpha()' method is being executed." << std::endl;
- }
- //--------------------------------------------
- void vtkKWMyClass::vtkKWMyClassMyMethodBetha()
- {
- std::cerr << "The 'vtkKWMyClassMyMethodBetha()' method is being executed." << std::endl;
- }
- Use '//----' as your separator, since it is the one already used in KWWidgets.
- Copyright notice
- Use the following copyright notice on top of each .h and .cxx file. This is just the base *template* copyright notice.It might (or not) get modified depending on the project under development. Note: Replace vtkKWClass with the actual name of the implemented class.
- Use the following copyright notice on top of each .h and .cxx file. This is just the base *template* copyright notice.It might (or not) get modified depending on the project under development. Note: Replace vtkKWClass with the actual name of the implemented class.
- /*=========================================================================
- Module: $RCSfile: vtkKWClass.h,v $
- Copyright (c) Kitware, Inc.
- All rights reserved.
- See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
- This software is distributed WITHOUT ANY WARRANTY; without even
- the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- PURPOSE. See the above copyright notice for more information.
- =========================================================================*/
- File extension
- Your C++ source files should have the '.cxx' extension, and your header files the '.h' extension. This is not a must, but more of an 'implicit' convention used throughout the KWWidgets files written so far.
- Your C++ source files should have the '.cxx' extension, and your header files the '.h' extension. This is not a must, but more of an 'implicit' convention used throughout the KWWidgets files written so far.
- The 'Delete' method
- Avoid reimplementing the 'Delete()' class method. Everything related to destruction should be taken care of inside the class destructor.
- Avoid reimplementing the 'Delete()' class method. Everything related to destruction should be taken care of inside the class destructor.
- The TCL/TK interpreter
- Initialization of an TCL/TK interpreter MUST to be done BEFORE any vtkKWApplication instance variable is allocated. This inialization is done by calling the vtkKWApplication::InitializeTcl static method.
- Initialization of an TCL/TK interpreter MUST to be done BEFORE any vtkKWApplication instance variable is allocated. This inialization is done by calling the vtkKWApplication::InitializeTcl static method.
- Forward declaration
- In your header files, do not include the headers of the classes you are using as member. Instead of that you should forward-declare them. This technique allows faster compilations, because only the super-class header file is included. It also reduces a lot the set of dependencies, meaning that the depencencies check are much faster, and if you modify any of those other header files, less files will get recompiled.
- For example use:
- #include <vtkKWApplication.h>
- class vtkKWWindow;
- class vtkKWSplashScreen;
- class vtkKWYourExistingBethaClass;
- class vtkKWYourExistingPhiClass;
- class vtkKWYourAlphaClass : public vtkKWApplication
- {
- protected:
- vtkKWWindow* MainWindow;
- vtkKWSplashScreen* SplashScreen;
- vtkKWYourExistingBethaClass* Beta;
- vtkKWYourExistingPhiClass* Phi;
- For example use:
- Instead of the "classic":
- #include <vtkKWApplication.h>
- #include <vtkKWWindow>
- #include <vtkKWSplashScreen>
- #include "vtkKWYourExistingBethaClass.h"
- #include "vtkKWYourExistingPhiClass.h"
- class vtkKWYourAlphaClass : public vtkKWApplication
- {
- protected:
- vtkKWWindow* MainWindow;
- vtkKWSplashScreen* SplashScreen;
- vtkKWYourExistingBethaClass* Beta;
- vtkKWYourExistingPhiClass* Phi;
- Instead of the "classic":
- The New-Delete 'compensation'
- For EVERY instace variable created using the New() method, you MUST use its counter-part Delete() method. This is necessary to balance the reference count of objects. Objects are ONLY deleted from memory, the moment their reference count equals zero. So this 'compensation' avoids memory leaks.
- For EVERY instace variable created using the New() method, you MUST use its counter-part Delete() method. This is necessary to balance the reference count of objects. Objects are ONLY deleted from memory, the moment their reference count equals zero. So this 'compensation' avoids memory leaks.
- The 'bool'-ean type
- Do not use 'bool' in the header files, as it can not be wrapped properly. This is IMPORTANT. Use 'int' instead.
- For example use:
- int isMyPropertySet(); // Returns 1 if my property is set, else it returns 0
- For example use:
- Instead of:
- bool isMyPropertySet(); // Returns true if my property is set, else it returns false
- Instead of:
- You can use 'bool' in your .cxx files, if you need to, especially if you use the STL for your datastructure --STL utilization is strongly recommend by Barre--, but in one should normally use 'int' for standard boolean operations.
- You can use 'bool' in your .cxx files, if you need to, especially if you use the STL for your datastructure --STL utilization is strongly recommend by Barre--, but in one should normally use 'int' for standard boolean operations.
- The VTK error, debug and warning macros
- Take advantage of the 'vtkErrorMacro', 'vtkDebugMacro', 'vtkWarning' macros, defined in the 'vtkSetGet.h' VTK header file, to indicate error, debug and/or warning messages inside your classes.
- Besides the advantages of using pre-defined macros, you will benefit from the fact that you can simply turn ALL these message on and off by conviniently calling the 'vtkObject::SetGlobalWarningDisplay()' static method.
- Besides the advantages of using pre-defined macros, you will benefit from the fact that you can simply turn ALL these message on and off by conviniently calling the 'vtkObject::SetGlobalWarningDisplay()' static method.