0027231: Obsolete OCCT features still mentioned in the documentation
[occt.git] / dox / dev_guides / contribution / coding_rules.md
1 Coding Rules {#occt_dev_guides__coding_rules}
2 ======================================
3
4 @tableofcontents
5
6 @section occt_coding_rules_1 Introduction
7
8 The purpose of this document is to define a common programming style for Open CASCADE Technology.
9
10 The common style facilitates understanding and maintaining a code developed cooperatively by several programmers. In addition, it enables construction of tools that incorporate knowledge of these standards to help in the programming.
11
12 OCCT programming style follows common and appropriate best practices, so some guidelines have been excerpted from the public domain.
13
14 The guide can be improved in the future as new ideas and enhancements are added.
15
16 @subsection occt_coding_rules_1_1 Scope of the document
17
18 Rules in this document refer to C++ code. However, with minor exceptions due to language restrictions, they are applicable to any sources in Open CASCADE Technology framework, including:
19 - C/C++
20 - GLSL programs
21 - OpenCL kernels
22 - TCL scripts and test cases
23
24 @section occt_coding_rules_2 Naming Conventions
25
26 @subsection occt_coding_rules_2_1 General naming rules
27
28 The names considered in this section mainly refer to the interface of Open CASCADE Technology libraries or source code itself.
29
30 ### International language [MANDATORY]
31
32 Open CASCADE Technology is an open source platform available for an international community, thus all names need to be composed of English words or their abbreviations.
33
34 ### Meaningful names
35
36 Names should be meaningful or, at least, contain a meaningful part. To better understand this requirement, let us examine the existing names of toolkits, packages, classes and methods:
37 - Packages containing words *Geom* or *Geom2d* in their names are related to geometrical data and operations.
38 - Packages containing words *TopoDS* or *BRep* in their names are related to topological data and operations.
39 - Packages ending with <i>...Test</i> define Draw Harness plugins.
40 - Methods starting with *Get...* and *Set...* are usually responsible for correspondingly retrieving and storing data.
41
42 ### Related names
43
44 Names related to a logically connected functionality should have the same prefix (start with the same letters) or, at least, have any other common part.
45 For example, method *GetCoord* returns a triple of real values and is defined for directions, vectors and points. The logical connection is obvious.
46
47 ### Camel Case style
48 Camel Case style is preferred for names.
49 For example:
50
51 ~~~~~{.cpp}
52 Standard_Integer awidthofbox;  // this is bad
53 Standard_Integer width_of_box; // this is bad
54 Standard_Integer aWidthOfBox;  // this is OK
55 ~~~~~
56
57 @subsection occt_coding_rules_2_2 Names of development units
58
59 Usually a unit (e.g. a package) is a set of classes, methods, enumerations or any other sources implementing a common functionality, which is self-contained and independent from other parts of the library.
60
61 ### No underscores in unit names [MANDATORY]
62
63 Names of units should not contain underscores, unless the use of underscores is allowed explicitly.
64
65 ### File name extensions [MANDATORY]
66
67 The following extensions should be used for source files, depending on their type:
68
69 * <i>.cxx</i> -- C++ source files
70 * <i>.hxx</i> -- C++ header files
71 * <i>.lxx</i> -- additional headers containing definitions of inline methods and auxiliary code
72
73 ### Prefix for toolkit names [MANDATORY]
74
75 Toolkit names are prefixed by *TK*, followed by a meaningful part of the name explaining the domain of functionality covered by the toolkit (e.g. *TKOpenGl*).
76
77 ### Names of classes
78
79 Usually the names of source files located in a unit start from the unit name separated from the other part of the file name by underscore "_".
80
81 Thus, the names of files containing sources of C++ classes that belong to a package are constructed according to the following template:
82
83 ~~~~~
84     <package-name>_<class-name>.cxx (or .hxx)
85 ~~~~~
86
87 For example, file *Adaptor2d_Curve2d.cxx* belongs to the package *Adaptor2d*
88
89 Files that contain sources related to the whole unit are called by the unit name with appropriate extension.
90
91 ### Names of functions
92
93 The term **function** here is defined as:
94 - Any class method
95 - Any package method
96 - Any non-member procedure or function
97
98 It is preferred to start names of public methods from an upper case character and to start names of protected and private methods from a lower case character.
99
100
101 ~~~~~{.cpp}
102 class MyPackage_MyClass
103 {
104
105 public:
106
107   Standard_Integer Value() const;
108   void             SetValue (const Standard_Integer theValue);
109
110 private:
111
112   void setIntegerValue (const Standard_Integer theValue);
113
114 };
115 ~~~~~
116
117 @subsection occt_coding_rules_2_3 Names of variables
118
119 There are several rules that describe currently accepted practices for naming variables.
120
121 ### Naming of variables
122
123 Name of a variable should not conflict with the existing or possible global names (for packages, macros, functions, global variables, etc.).
124
125 The name of a variable should not start with an underscore.
126
127 See the following examples:
128
129 ~~~~~{.cpp}
130 Standard_Integer Elapsed_Time = 0; // this is bad - possible class   name
131 Standard_Integer gp = 0;           // this is bad - existing package name
132 Standard_Integer aGp = 0;          // this is OK
133 Standard_Integer _KERNEL = 0;      // this is bad
134 Standard_Integer THE_KERNEL = 0;   // this is OK
135 ~~~~~
136
137 ### Names of function parameters
138
139 The name of a function (procedure, class method) parameter should start with prefix *the* followed by the meaningful part of the name starting with a capital letter.
140
141 See the following examples:
142
143 ~~~~~{.cpp}
144 void Package_MyClass::MyFunction (const gp_Pnt& p);        // this is bad
145 void Package_MyClass::MyFunction (const gp_Pnt& theP);     // this is OK
146 void Package_MyClass::MyFunction (const gp_Pnt& thePoint); // this is preferred
147 ~~~~~
148
149 ### Names of class member variables
150
151 The name of a class member variable should start with prefix *my* followed by the meaningful of the name starting with a capital letter.
152
153 See the following examples:
154
155 ~~~~~{.cpp}
156 Standard_Integer counter;   // This is bad
157 Standard_Integer myC;       // This is OK
158 Standard_Integer myCounter; // This is preferred
159 ~~~~~
160
161 ### Names of global variables
162
163 It is strongly recommended to avoid defining any global variables.
164 However, as soon as a global variable is necessary, its name should be prefixed by the name of a class or a package where it is defined followed with <i>_my</i>.
165
166 See the following examples:
167
168 ~~~~~{.cpp}
169 Standard_Integer MyPackage_myGlobalVariable = 0;
170 Standard_Integer MyPackage_MyClass_myGlobalVariable = 0;
171 ~~~~~
172
173 Static constants within the file should be written in upper-case and begin with prefix *THE_*:
174 ~~~~~{.cpp}
175 namespace
176 {
177   static const Standard_Real THE_CONSTANT_COEF = 3.14;
178 };
179 ~~~~~
180
181 ### Names of local variables
182
183 The name of a local variable should be distinguishable from the name of a function parameter, a class member variable and a global variable.
184
185 It is preferred to prefix local variable names with *a* and *an* (or *is*, *to* and *has* for Boolean variables).
186
187 See the following example:
188
189 ~~~~~{.cpp}
190 Standard_Integer theI;    // this is bad
191 Standard_Integer i;       // this is bad
192 Standard_Integer index;   // this is bad
193 Standard_Integer anIndex; // this is OK
194 ~~~~~
195
196 ### Avoid dummy names
197 Avoid dummy names, such as <i>i, j, k</i>. Such names are meaningless and easy to mix up.
198
199 The code becomes more and more complicated when such dummy names are used there multiple times with different meanings, or in cycles with different iteration ranges, etc.
200
201 See the following examples for preferred style:
202
203 ~~~~~{.cpp}
204 void Average (const Standard_Real** theArray,
205               Standard_Integer      theRowsNb,
206               Standard_Integer      theRowLen,
207               Standard_Real&        theResult)
208 {
209   theResult = 0.0;
210   for (Standard_Integer aRow = 0; aRow < aRowsNb; ++aRow)
211   {
212     for (Standard_Integer aCol = 0; aCol < aRowLen; ++aCol)
213     {
214       theResult += theArray[aRow][aCol];
215     }
216     theResult /= Standard_Real(aRowsNb * aRowLen);
217   }
218 }
219 ~~~~~
220
221 @section occt_coding_rules_3 Formatting rules
222
223 To improve the open source readability and, consequently, maintainability, the following set of rules is applied.
224
225 ### International language [MANDATORY]
226
227 All comments in all sources must be in English.
228
229 ### Line length
230
231 Try to stay within the limit of 120 characters per line in all sources.
232
233 ### C++ style comments
234
235 Prefer C++ style comments in C++ sources.
236
237 ### Commenting out unused code
238
239 Delete unused code instead of commenting it or using \#define.
240
241 ### Indentation in sources [MANDATORY]
242
243 Indentation in all sources should be set to two space characters.
244 Use of tabulation characters for indentation is disallowed.
245
246 ### Separating spaces
247
248 Punctuation rules follow the rules of the English language.
249 * C/C++ reserved words, commas, colons and semicolons should be followed by a space character if they are not at the end of a line.
250 * There should be no space characters after '(' and before ')'. Closing and opening brackets should be separated by a space character.
251 * For better readability it is also recommended to surround conventional operators by a space character. See the following examples:
252
253 ~~~~~{.cpp}
254 while (true)                            // NOT: while( true ) ...
255 {
256   DoSomething (theA, theB, theC, theD); // NOT: DoSomething(theA,theB,theC,theD);
257 }
258 for (anIter = 0; anIter < 10; ++anIter) // NOT: for (anIter=0;anIter<10;++anIter){
259 {
260   theA = (theB + theC) * theD;          // NOT: theA=(theB+theC)*theD
261 }
262 ~~~~~
263
264 ### Separate logical blocks
265
266 Separate logical blocks of code with one blank line and comments.
267
268 See the following example:
269
270 ~~~~~{.cpp}
271 // check arguments
272 Standard_Integer anArgsNb = argCount();
273 if (anArgsNb < 3 || isSmthInvalid)
274 {
275   return THE_ARG_INVALID;
276 }
277
278 // read and check header
279 ...
280 ...
281
282 // do our job
283 ...
284 ...
285 ~~~~~
286
287 Notice that multiple blank lines should be avoided.
288
289 ### Separate function bodies [MANDATORY]
290
291 Use function descriptive blocks to separate function bodies from each other.
292 Each descriptive block should contain at least a function name and purpose description.
293
294 See the following example:
295
296 ~~~~~{.cpp}
297 // =======================================================================
298 // function : TellMeSmthGood
299 // purpose  : Gives me good news
300 // =======================================================================
301 void TellMeSmthGood()
302 {
303   ...
304 }
305
306 // =======================================================================
307 // function : TellMeSmthBad
308 // purpose  : Gives me bad news
309 // =======================================================================
310 void TellMeSmthBad()
311 {
312   ...
313 }
314 ~~~~~
315
316 ### Block layout [MANDATORY]
317 Figure brackets <i>{ }</i> and each operator <i>(for, if, else, try, catch)</i> should be written on a dedicated line.
318
319 In general, the layout should be as follows:
320
321 ~~~~~{.cpp}
322 while (expression)
323 {
324   ...
325 }
326 ~~~~~
327
328 Entering a block increases and leaving a block decreases the indentation by one tabulation.
329
330 ### Single-line operators
331
332 Single-line conditional operators <i>(if, while, for,</i> etc.) can be written without brackets on the following line.
333
334 ~~~~~{.cpp}
335 if (!myIsInit) return Standard_False; // bad
336
337 if (thePtr == NULL)                   // OK
338   return Standard_False;
339
340 if (!theAlgo.IsNull())                // preferred
341 {
342   DoSomething();
343 }
344 ~~~~~
345
346 Having all code in the same line is less convenient for debugging.
347
348 ### Alignment
349
350 Use alignment wherever it enhances the readability. See the following example:
351
352 ~~~~~{.cpp}
353 MyPackage_MyClass anObject;
354 Standard_Real     aMinimum = 0.0;
355 Standard_Integer  aVal     = theVal;
356 switch (aVal)
357 {
358   case 0:  computeSomething();              break;
359   case 12: computeSomethingElse (aMinimum); break;
360   case 3:
361   default: computeSomethingElseYet();       break;
362 }
363 ~~~~~
364
365 ### Indentation of comments
366
367 Comments should be indented in the same way as the code to which they refer or they can be in the same line if they are short.
368
369 The text of the comment should be separated from the slash character by a single space character.
370
371 See the following example:
372
373 ~~~~~{.cpp}
374 while (expression)   //bad comment
375 {
376   // this is a long multi-line comment
377   // which is really required
378   DoSomething();     // maybe, enough
379   DoSomethingMore(); // again
380 }
381 ~~~~~
382
383 ### Early return statement
384
385 Use an early return condition rather than collect indentations.
386
387 Write like this:
388
389 ~~~~~{.cpp}
390 Standard_Integer ComputeSumm (const Standard_Integer* theArray,
391                               const Standard_Size     theSize)
392 {
393   Standard_Integer aSumm = 0;
394   if (theArray == NULL || theSize == 0)
395   {
396     return 0;
397   }
398
399   ... computing summ ...
400   return aSumm;
401 }
402 ~~~~~
403
404 Rather than:
405
406 ~~~~~{.cpp}
407 Standard_Integer ComputeSumm (const Standard_Integer* theArray,
408                               const Standard_Size     theSize)
409 {
410   Standard_Integer aSumm = 0;
411   if (theArray != NULL && theSize != 0)
412   {
413     ... computing summ ...
414   }
415   return aSumm;
416 }
417 ~~~~~
418
419 This helps to improve readability and reduce the unnecessary indentation depth.
420
421 ### Trailing spaces
422
423 Trailing spaces should be removed whenever possible.
424 Spaces at the end of a line are useless and do not affect functionality.
425
426 ### Headers order
427
428 Split headers into groups: system headers, headers per each framework, project headers; sort the list of includes alphabetically.
429
430 This rule improves readability, allows detecting useless multiple header inclusions and makes 3rd-party dependencies clearly visible.
431
432 ~~~~~{.cpp}
433 // system headers
434 #include <iostream>
435 #include <windows.h>
436
437 // Qt headers
438 #include <QDataStream>
439 #include <QString>
440
441 // OCCT headers
442 #include <gp_Pnt.hxx>
443 #include <gp_Vec.hxx>
444 #include <NCollection_List.hxx>
445 ~~~~~
446
447 @section occt_coding_rules_4 Documentation rules
448
449 The source code is one of the most important references for documentation.
450 The comments in the source code should be complete enough to allow understanding the corresponding code and to serve as basis for other documents.
451
452 The main reasons why the comments are regarded as documentation and should be maintained are:
453 - The comments are easy to reach -- they are always together with the source code;
454 - It is easy to update a description in the comment when the source is modified;
455 - The source by itself is a good context to describe various details that would require much more explanations in a separate document;
456 - As a summary, this is the most cost-effective documentation.
457
458 The comments should be compatible with Doxygen tool for automatic documentation generation (thus should use compatible tags).
459
460 ### Documenting classes [MANDATORY]
461
462 Each class should be documented in its header file (.hxx).
463 The comment should give enough details for the reader to understand the purpose of the class and the main way of work with it.
464
465 ### Documenting class methods [MANDATORY]
466
467 Each class or package method should be documented in the header file (.hxx).
468
469 The comment should explain the purpose of the method, its parameters, and returned value(s).
470 Accepted style is:
471
472 @verbatim
473 //! Method computes the square value.
474 //! @param theValue the input value
475 //! @return squared value
476 Standard_Export Standard_Real Square (Standard_Real theValue);
477 @endverbatim
478
479 ### Documenting C/C++ sources
480
481 It is very desirable to put comments in the C/C++ sources of the package/class.
482
483 They should be detailed enough to allow any person to understand what each part of code does.
484
485 It is recommended to comment all static functions (like methods in headers), and to insert at least one comment per each 10-100 lines in the function body.
486
487 There are also some rules that define how comments should be formatted, see @ref occt_coding_rules_3 "Formatting Rules".
488
489 Following these rules is important for good comprehension of the comments. Moreover, this approach  allows automatically generating user-oriented documentation directly from the commented sources.
490
491 @section occt_coding_rules_5 Application design
492
493 The following rules define the common style, which should be applied by any developer contributing to the open source.
494
495 ### Allow possible inheritance
496
497 Try to design general classes (objects) keeping possible inheritance in mind.
498 This rule means that the user who makes possible extensions of your class should not encounter problems of private implementation.
499 Try to use protected members and virtual methods wherever you expect extensions in the future.
500
501 ### Avoid friend declarations
502
503 Avoid using 'friend' classes or functions except for some specific cases (for example, iteration) 'Friend' declarations increase coupling.
504
505 ### Set/get methods
506
507 Avoid providing set/get methods for all fields of the class.
508 Intensive set/get functions break down encapsulation.
509
510 ### Hiding virtual functions [MANDATORY]
511
512 Avoid hiding a base class virtual function by a redefined function with a different signature.
513 Most of the compilers issue warning on this.
514
515 ### Avoid mixing error reporting strategies
516
517 Try not to mix different error indication/handling strategies (exceptions or returned values) on the same application level.
518
519 ### Minimize compiler warnings [MANDATORY]
520
521 When compiling the source pay attention to and try to minimize compiler warnings.
522
523 ### Avoid unnecessary inclusions
524
525 Try to minimize compilation dependencies by removing unnecessary inclusions.
526
527 @section occt_coding_rules_6 General C/C++ rules
528
529 This section defines the rules for writing a portable and maintainable C/C++ source code.
530
531 ### Wrapping of global variables [MANDATORY]
532
533 Use package or class methods returning reference to wrap global variables to reduce possible name space conflicts.
534
535 ### Avoid private members
536
537 Use *protected* members instead of *private* wherever reasonable to enable future extensions.
538 Use *private* fields if future extensions should be disabled.
539
540 ### Constants and inlines over defines [MANDATORY]
541
542 Use constant variables (const) and inline functions instead of defines (\#define).
543
544 ### Avoid explicit numerical values [MANDATORY]
545
546 Avoid usage of explicit numeric values. Use named constants and enumerations instead.
547 Numbers produce difficulties for reading and maintenance.
548
549 ### Three mandatory methods
550
551 If a class has a destructor, an assignment operator or a copy constructor, it usually needs the other two methods.
552
553 ### Virtual destructor
554
555 A class with virtual function(s) ought to have a virtual destructor.
556
557 ### Overriding virtual methods
558
559 Declaration of overriding method should contains specifiers "virtual" and "override"
560 (using Standard_OVERRIDE alias for compatibility with old compilers).
561
562 ~~~~~{.cpp}
563 class MyPackage_BaseClass
564 {
565
566 public:
567
568   Standard_EXPORT virtual Standard_Boolean Perform();
569
570 };
571
572 ~~~~~{.cpp}
573 class MyPackage_MyClass : public MyPackage_BaseClass
574 {
575
576 public:
577
578   Standard_EXPORT virtual Standard_Boolean Perform() Standard_OVERRIDE;
579
580 };
581 ~~~~~
582
583 This makes class definition more clear (virtual methods become highlighted).
584
585 Declaration of interface using pure virtual functions protects against
586 incomplete inheritance at first level, but does not help when method is overridden multiple times within nested inheritance
587 or when method in base class is intended to be optional.
588
589 And here "override" specifier introduces additional protection against situations when interface changes might be missed
590 (class might contain old methods which will be never called).
591
592 ### Default parameter value
593
594 Do not redefine a default parameter value in an inherited function.
595
596 ### Use const modifier
597
598 Use *const* modifier wherever possible (functions parameters, return values, etc.)
599
600 ### Usage of goto [MANDATORY]
601 Avoid *goto* statement unless it is really needed.
602
603 ### Declaring variable in for() header
604
605 Declare a cycle variable in the header of the *for()* statement if not used out of cycle.
606
607 ~~~~~{.cpp}
608 Standard_Real aMinDist = Precision::Infinite();
609 for (NCollection_Sequence<gp_Pnt>::Iterator aPntIter (theSequence);
610      aPntIter.More(); aPntIter.Next())
611 {
612   aMinDist = Min (aMinDist, theOrigin.Distance (aPntIter.Value()));
613 }
614 ~~~~~
615
616 ### Condition statements within zero
617
618 Avoid usage of C-style comparison for non-boolean variables:
619
620 ~~~~~{.cpp}
621 void Function (Standard_Integer theValue,
622                Standard_Real*   thePointer)
623 {
624   if (!theValue)          // bad style - ambiguous logic
625   {
626     DoSome();
627   }
628
629   if (theValue == 0)      // OK
630   {
631     DoSome();
632   }
633
634   if (thePointer != NULL) // OK, predefined NULL makes pointer comparison cleaner to reader
635   {                       // (nullptr should be used instead as soon as C++11 will be available)
636     DoSome2();
637   }
638 }
639 ~~~~~
640
641 @section occt_coding_rules_7 Portability issues
642
643 This chapter contains rules that are critical for cross-platform portability.
644
645 ### Provide code portability [MANDATORY]
646
647 The source code must be portable to all platforms listed in the official 'Technical Requirements'.
648 The term 'portable' here means 'able to be built from source'.
649
650 The C++ source code should meet C++03 standard.
651 Any usage of compiler-specific features or further language versions (for example, C++11, until all major compilers on all supported platforms implement all its features) should be optional (used only with appropriate preprocessor checks) and non-exclusive (an alternative implementation compatible with other compilers should be provided).
652
653 ### Avoid usage of global variables [MANDATORY]
654
655 Avoid usage of global variables. Usage of global variables may cause problems when accessed from another shared library.
656
657 Use global (package or class) functions that return reference to static variable local to this function instead of global variables.
658
659 Another possible problem is the order of initialization of global variables defined in various libraries that may differ depending on platform, compiler and environment.
660
661 ### Avoid explicit basic types
662
663 Avoid explicit usage of basic types (*int*, *float*, *double*, etc.), use Open CASCADE Technology types from package *Standard: Standard_Integer, Standard_Real, Standard_ShortReal, Standard_Boolean, Standard_CString* and others or a specific *typedef* instead.
664
665 ### Use *sizeof()* to calculate sizes [MANDATORY]
666
667 Do not assume sizes of types. Use *sizeof()* instead to calculate sizes.
668
669 ### Empty line at the end of file [MANDATORY]
670
671 In accordance with C++03 standard source files should be trailed by an empty line.
672 It is recommended to follow this rule for any plain text files for consistency and for correct work of git difference tools.
673
674 @section occt_coding_rules_8 Stability issues
675
676 The rules listed in this chapter are important for stability of the programs that use Open CASCADE Technology libraries.
677
678 ### Use *OSD::SetSignal()* to catch exceptions
679
680 When using Open CASCADE Technology in an application, call *OSD::SetSignal()* function when the application is initialized.
681
682 This will install C handlers for run-time interrupt signals and exceptions, so that low-level exceptions (such as access violation, division by zero, etc.) will be redirected to C++ exceptions
683 that use *try {...} catch (Standard_Failure) {...}* blocks.
684
685 The above rule is especially important for robustness of modeling algorithms.
686
687 ### Cross-referenced handles
688
689 Take care about cycling of handled references to avoid chains, which will never be freed. For this purpose, use a pointer at one (subordinate) side. 
690
691 See the following example:
692
693 ~~~~{.cpp}
694     class Slave;
695
696     class Master : public Standard_Transient
697     {
698     ...
699       void SetSlave (const Handle(Slave)& theSlave)
700       { 
701         mySlave = theSlave;
702       }
703     ...
704     private:
705       Handle(Slave) theSlave; // smart pointer
706     ...
707     }
708
709     class Slave : public Standard_Transient
710     {
711     ...
712       void SetMaster (const Handle(Master)& theMaster)
713       { 
714         myMaster = theMaster.get();
715       }
716     ...
717     private:
718       Master* theMaster; // simple pointer
719     ...
720     }
721 ~~~~
722
723 ### C++ memory allocation
724
725 In C++ use *new* and *delete* operators instead of *malloc()* and *free()*. Try not to mix different memory allocation techniques.
726
727 ### Match *new* and *delete* [MANDATORY]
728
729 Use the same form of new and delete.
730
731 ~~~~~{.cpp}
732 aPtr1 = new TypeA[n];              ... ; delete[]        aPtr1;
733 aPtr2 = new TypeB();               ... ; delete          aPtr2;
734 aPtr3 = Standard::Allocate (4096); ... ; Standard::Free (aPtr3);
735 ~~~~~
736
737 ### Methods managing dynamical allocation [MANDATORY]
738
739 Define a destructor, a copy constructor and an assignment operator for classes with dynamically allocated memory.
740
741 ### Uninitialized variables [MANDATORY]
742
743 Every variable should be initialized.
744
745 ~~~~~{.cpp}
746 Standard_Integer aTmpVar1;     // bad
747 Standard_Integer aTmpVar2 = 0; // OK
748 ~~~~~
749
750 Uninitialized variables might be kept only within performance-sensitive code blocks and only when their initialization is guaranteed by subsequent code.
751
752 ### Do not hide global *new*
753
754 Avoid hiding the global *new* operator.
755
756 ### Assignment operator
757
758 In *operator=()* assign to all data members and check for assignment to self.
759
760 ### Float comparison
761
762 Don't check floats for equality or non-equality; check for GT, GE, LT or LE.
763
764 ~~~~~{.cpp}
765 if (Abs (theFloat1 - theFloat2) < theTolerance)
766 {
767   DoSome();
768 }
769 ~~~~~
770
771 Package *Precision* provides standard values for SI units and widely adopted by existing modeling algorithms:
772
773 - *Precision::Confusion()* for lengths in meters;
774 - *Precision::Angular()* for angles in radians.
775
776 as well as definition of infinite values within normal range of double precision:
777 - *Precision::Infinite()*
778 - *Precision::IsInfinite()*
779 - *Precision::IsPositiveInfinite()*
780 - *Precision::IsNegativeInfinite()*
781
782 ### Non-indexed iteration
783
784 Avoid usage of iteration over non-indexed collections of objects.
785 If such iteration is used, make sure that the result of the algorithm does not depend on the order of iterated items.
786
787 Since the order of iteration is unpredictable in case of a non-indexed collection of objects, it frequently leads to different behavior of the application from one run to another, thus embarrassing the debugging process.
788
789 It mostly concerns mapped objects for which pointers are involved in calculating the hash function. For example, the hash function of *TopoDS_Shape* involves the address of *TopoDS_TShape* object. Thus the order of the same shape in the *TopTools_MapOfShape* will vary in different sessions of the application. 
790
791 ### Do not throw in destructors
792
793 Do not throw from within a destructor.
794
795 ### Assigning to reference [MANDATORY]
796
797 Avoid the assignment of a temporary object to a reference. This results in a different behavior for different compilers on different platforms.
798
799 @section occt_coding_rules_9 Performance issues
800
801 These rules define the ways of avoiding possible loss of performance caused by ineffective programming.
802
803 ### Class fields alignment
804
805 Declare fields of a class in the decreasing order of their size for better alignment.
806 Generally, try to reduce misaligned accesses since they impact the performance (for example, on Intel machines).
807
808 ### Fields initialization order [MANDATORY]
809
810 List class data members in the constructor's initialization list in the order they are declared.
811
812 ~~~~~{.cpp}
813 class MyPackage_MyClass
814 {
815
816 public:
817
818   MyPackage_MyClass()
819   : myPropertyA (1),
820     myPropertyB (2) {}
821
822 // NOT
823 // : myPropertyB (2),
824 //   myPropertyA (1) {}
825
826 private:
827
828   Standard_Integer myPropertyA;
829   Standard_Integer myPropertyB;
830
831 };
832 ~~~~~
833
834 ### Initialization over assignment
835
836 Prefer initialization over assignment in class constructors.
837
838 ~~~~~{.cpp}
839 MyPackage_MyClass()
840 : myPropertyA (1)  // preferred
841 {
842   myPropertyB = 2; // not recommended
843 }
844 ~~~~~
845
846 ### Optimize caching
847
848 When programming procedures with extensive memory access, try to optimize them in terms of cache behavior. Here is an example of how the cache behavior can be impacted:
849
850 On x86 this code
851
852 ~~~~~{.cpp}
853 Standard_Real anArray[4096][2];
854 for (Standard_Integer anIter = 0; anIter < 4096; ++anIter)
855 {
856   anArray[anIter][0] = anArray[anIter][1];
857 }
858 ~~~~~
859
860 is more efficient then
861
862 ~~~~~{.cpp}
863 Standard_Real anArray[2][4096];
864 for (Standard_Integer anIter = 0; anIter < 4096; ++anIter)
865 {
866   anArray[0][anIter] = anArray[1][anIter];
867 }
868 ~~~~~
869
870 since linear access does not invalidate cache too often.
871
872 @section occt_coding_rules_10 Draw Harness command
873
874 Draw Harness provides TCL interface for OCCT algorithms.
875
876 There is no TCL wrapper over OCCT C++ classes, instead interface is provided through the set of TCL commands implemented in C++.
877
878 There is a list of common rules which should be followed to implement well-formed Draw Harness command.
879
880 ### Return value
881
882 Command should return 0 in most cases even if the executed algorithm has failed. Returning 1 would lead to a TCL exception, thus should be used in case of a command line syntax error and similar issues.
883
884 ### Validate input parameters
885
886 Command arguments should be validated before usage. The user should see a human-readable error description instead of a runtime exception from the executed algorithm.
887
888 ### Validate the number of input parameters
889
890 Command should warn the user about unknown arguments, including cases when extra parameters have been pushed for the command with a fixed number of arguments.
891
892 ~~~~~{.cpp}
893   if (theArgsNb != 3)
894   {
895     std::cout << "Syntax error - wrong number of arguments!\n";
896     return 1;
897   }
898
899   Standard_Integer anArgIter  = 1;
900   Standard_CString aResName   = theArgVec[anArgIter++];
901   Standard_CString aFaceName  = theArgVec[anArgIter++];
902   TopoDS_Shape     aFaceShape = DBRep::Get (aFaceName);
903   if (aFaceShape.IsNull()
904    || aFaceShape.ShapeType() != TopAbs_FACE)
905   {
906     std::cout << "Shape " << aFaceName << " is empty or not a Face!\n";
907     return 1;
908   }
909   DBRep::Set (aResName, aFaceShape);
910   return 0;
911 ~~~~~
912
913 ### Message printing
914
915 Informative messages should be printed into standard output *std::cout*, whilst command results (if any) -- into Draw Interpreter.
916
917 Information printed into Draw Interpreter should be well-structured to allow usage in TCL script.
918
919 ### Long list of arguments
920
921 Any command with a long list of obligatory parameters should be considered as ill-formed by design.
922 Optional parameters should start with flag name (with '-' prefix) and followed by its values:
923
924 ~~~~~{.tcl}
925 myCommand -flag1 value1 value2 -flag2 value3
926 ~~~~~
927
928 ### Arguments parser
929
930 - Integer values should be read using *Draw::Atoi()* function.
931 - Real values should be read using *Draw::Atof()* function.
932 - Flags names should be checked in case insensitive manner.
933
934 Functions *Draw::Atof()* and *Draw::Atoi()* support expressions and read values in C-locale.
935
936 ~~~~~{.cpp}
937   Standard_Real aPosition[3] = {0.0, 0.0, 0.0};
938   for (Standard_Integer anArgIter = 1; anArgIter < theArgsNb; ++anArgIter)
939   {
940     Standard_CString anArg = theArgVec[anArgIter];
941     TCollection_AsciiString aFlag (anArg);
942     aFlag.LowerCase(); //!< for case insensitive comparison
943     if (aFlag == "position")
944     {
945       if ((anArgIt + 3) >= theArgsNb)
946       {
947         std::cerr << "Wrong syntax at argument '" << anArg << "'!\n";
948         return 1;
949       }
950       aPosition[0] = Draw::Atof (theArgVec[++anArgIt]);
951       aPosition[1] = Draw::Atof (theArgVec[++anArgIt]);
952       aPosition[2] = Draw::Atof (theArgVec[++anArgIt]);
953     }
954     else
955     {
956       std::cout << "Syntax error! Unknown flag '" << anArg << "'\n";
957       return 1;
958     }
959   }
960 ~~~~~
961
962 @section occt_coding_rules_11 Examples
963
964 ### Sample documented class
965
966 @verbatim
967 class Package_Class
968 {
969
970 public: //! @name public methods
971
972   //! Method computes the square value.
973   //! @param theValue the input value
974   //! @return squared value
975   Standard_Export Standard_Real Square (const Standard_Real theValue);
976
977 private: //! \@name private methods
978
979   //! Auxiliary method
980   void increment();
981
982 private: //! \@name private fields
983
984   Standard_Integer myCounter; //!< usage counter
985
986 };
987
988
989 @endverbatim
990
991 ~~~~~
992 #include <Package_Class.hxx>
993 // ==========================================================
994 // function : Square
995 // purpose  : Method computes the square value
996 // ==========================================================
997 Standard_Real Package_Class::Square (const Standard_Real theValue)
998 {
999   increment();
1000   return theValue * theValue;
1001 }
1002
1003 // ==========================================================
1004 // function : increment
1005 // purpose  :
1006 // ==========================================================
1007 void Package_Class::increment()
1008 {
1009   ++myCounter;
1010 }
1011 ~~~~~
1012
1013 ### TCL script for Draw Harness
1014
1015 ~~~~~{.tcl}
1016 # show fragments (solids) in shading with different colors
1017 proc DisplayColored {theShape} {
1018   set aSolids [uplevel #0 explode $theShape so]
1019   set aColorIter 0
1020   set THE_COLORS {red green blue1 magenta1 yellow cyan1 brown}
1021   foreach aSolIter $aSolids {
1022     uplevel #0 vdisplay         $aSolIter
1023     uplevel #0 vsetcolor        $aSolIter [lindex $THE_COLORS [expr [incr aColorIter] % [llength $THE_COLORS]]]
1024     uplevel #0 vsetdispmode     $aSolIter 1
1025     uplevel #0 vsetmaterial     $aSolIter plastic
1026     uplevel #0 vsettransparency $aSolIter 0.5
1027   }
1028 }
1029
1030 # load modules
1031 pload MODELING VISUALIZATION
1032
1033 # create boxes
1034 box bc  0 0 0 1 1 1
1035 box br  1 0 0 1 1 2
1036 compound bc br c
1037
1038 # show fragments (solids) in shading with different colors
1039 vinit View1
1040 vclear
1041 vaxo
1042 vzbufftrihedron
1043 DisplayColored c
1044 vfit
1045 vdump $imagedir/${casename}.png 512 512
1046 ~~~~~
1047
1048 ### GLSL program:
1049 ~~~~~{.fs}
1050 vec3 Ambient;  //!< Ambient  contribution of light sources
1051 vec3 Diffuse;  //!< Diffuse  contribution of light sources
1052 vec3 Specular; //!< Specular contribution of light sources
1053
1054 //! Computes illumination from light sources
1055 vec4 ComputeLighting (in vec3 theNormal,
1056                       in vec3 theView,
1057                       in vec4 thePoint)
1058 {
1059   // clear the light intensity accumulators
1060   Ambient  = occLightAmbient.rgb;
1061   Diffuse  = vec3 (0.0);
1062   Specular = vec3 (0.0);
1063   vec3 aPoint = thePoint.xyz / thePoint.w;
1064   for (int anIndex = 0; anIndex < occLightSourcesCount; ++anIndex)
1065   {
1066     int aType = occLight_Type (anIndex);
1067     if (aType == OccLightType_Direct)
1068     {
1069       directionalLight (anIndex, theNormal, theView);
1070     }
1071     else if (aType == OccLightType_Point)
1072     {
1073       pointLight (anIndex, theNormal, theView, aPoint);
1074     }
1075   }
1076
1077   return vec4 (Ambient,  1.0) * occFrontMaterial_Ambient()
1078        + vec4 (Diffuse,  1.0) * occFrontMaterial_Diffuse()
1079        + vec4 (Specular, 1.0) * occFrontMaterial_Specular();
1080 }
1081
1082 //! Entry point to the Fragment Shader
1083 void main()
1084 {
1085   gl_FragColor = computeLighting (normalize (Normal),
1086                                   normalize (View),
1087                                   Position);
1088 }
1089 ~~~~~