Home | History | Annotate | Download | only in contrib
      1 Coding Style Guidelines
      2 =======================
      3 
      4 These conventions have evolved over time. Some of the earlier code in both
      5 projects doesn't strictly adhere to the guidelines. However, as the code evolves
      6 we hope to make the existing code conform to the guildelines.
      7 
      8 Files
      9 -----
     10 
     11 We use .cpp and .h as extensions for c++ source and header files.
     12 
     13 Headers that aren't meant for public consumption should be placed in src
     14 directories so that they aren't in a client's search path, or in
     15 include/private if they need to be used by public headers.
     16 
     17 We prefer to minimize includes. If forward declaring a name in a header is
     18 sufficient then that is preferred to an include.
     19 
     20 Forward declarations and file includes should be in alphabetical order (but we
     21 aren't very strict about it).
     22 
     23 <span id="no-define-before-sktypes"></span>
     24 Do not use #if/#ifdef before including "SkTypes.h" (directly or indirectly).
     25 Most things you'd #if on tend to not yet be decided until SkTypes.h.
     26 
     27 We use 4 spaces, not tabs.
     28 
     29 We use Unix style endlines (LF).
     30 
     31 We prefer no trailing whitespace but aren't very strict about it.
     32 
     33 We wrap lines at 100 columns unless it is excessively ugly (use your judgement).
     34 The soft line length limit was changed from 80 to 100 columns in June 2012. Thus,
     35 most files still adhere to the 80 column limit. It is not necessary or worth
     36 significant effort to promote 80 column wrapped files to 100 columns. Please
     37 don't willy-nilly insert longer lines in 80 column wrapped files. Either be
     38 consistent with the surrounding code or, if you really feel the need, promote
     39 the surrounding code to 100 column wrapping.
     40 
     41 Naming
     42 ------
     43 
     44 Both projects use a prefix to designate that they are Skia prefix for classes,
     45 enums, structs, typedefs etc is Sk. Ganesh's is Gr. Nested types should not be
     46 prefixed.
     47 
     48 <!--?prettify?-->
     49 ~~~~
     50 class SkClass {
     51 public:
     52     class HelperClass {
     53         ...
     54     };
     55 };
     56 ~~~~
     57 
     58 Data fields in structs, classes, unions begin with lowercase f and are then
     59 camel capped.
     60 
     61 <!--?prettify?-->
     62 ~~~~
     63 struct GrCar {
     64     ...
     65     float fMilesDriven;
     66     ...
     67 };
     68 ~~~~
     69 
     70 Globals variables are similar but prefixed with g and camel-capped
     71 
     72 <!--?prettify?-->
     73 ~~~~
     74 bool gLoggingEnabled
     75 Local variables begin lowercases and are camel-capped.
     76 
     77 int herdCats(const Array& cats) {
     78     int numCats = cats.count();
     79 }
     80 ~~~~
     81 
     82 Enum values are prefixed with k. Unscoped enum values are post fixed with
     83 an underscore and singular name of the enum name. The enum itself should be
     84 singular for exclusive values or plural for a bitfield. If a count is needed it
     85 is  `k<singular enum name>Count` and not be a member of the enum (see example),
     86 or a kLast member of the enum is fine too.
     87 
     88 <!--?prettify?-->
     89 ~~~~
     90 enum class SkPancakeType {
     91      kBlueberry,
     92      kPlain,
     93      kChocolateChip,
     94 };
     95 ~~~~
     96 
     97 <!--?prettify?-->
     98 ~~~~
     99 enum SkPancakeType {
    100      kBlueberry_PancakeType,
    101      kPlain_PancakeType,
    102      kChocolateChip_PancakeType,
    103 
    104      kLast_PancakeType = kChocolateChip_PancakeType
    105 };
    106 
    107 static const SkPancakeType kPancakeTypeCount = kLast_PancakeType + 1;
    108 ~~~~
    109 
    110 A bitfield:
    111 
    112 <!--?prettify?-->
    113 ~~~~
    114 enum SkSausageIngredientBits {
    115     kFennel_SuasageIngredientBit = 0x1,
    116     kBeef_SausageIngredientBit   = 0x2
    117 };
    118 ~~~~
    119 
    120 or:
    121 
    122 <!--?prettify?-->
    123 ~~~~
    124 enum SkMatrixFlags {
    125     kTranslate_MatrixFlag = 0x1,
    126     kRotate_MatrixFlag    = 0x2
    127 };
    128 ~~~~
    129 
    130 Exception: anonymous enums can be used to declare integral constants, e.g.:
    131 
    132 <!--?prettify?-->
    133 ~~~~
    134 enum { kFavoriteNumber = 7 };
    135 ~~~~
    136 
    137 Macros are all caps with underscores between words. Macros that have greater
    138 than file scope should be prefixed SK or GR.
    139 
    140 Static non-class functions in implementation files are lower case with
    141 underscores separating words:
    142 
    143 <!--?prettify?-->
    144 ~~~~
    145 static inline bool tastes_like_chicken(Food food) {
    146     return kIceCream_Food != food;
    147 }
    148 ~~~~
    149 
    150 Externed functions or static class functions are camel-capped with an initial cap:
    151 
    152 <!--?prettify?-->
    153 ~~~~
    154 bool SkIsOdd(int n);
    155 
    156 class SkFoo {
    157 public:
    158     static int FooInstanceCount();
    159 };
    160 ~~~~
    161 
    162 Macros
    163 ------
    164 
    165 Ganesh macros that are GL-specific should be prefixed GR_GL.
    166 
    167 <!--?prettify?-->
    168 ~~~~
    169 #define GR_GL_TEXTURE0 0xdeadbeef
    170 ~~~~
    171 
    172 Ganesh prefers that macros are always defined and the use of `#if MACRO` rather than
    173 `#ifdef MACRO`.
    174 
    175 <!--?prettify?-->
    176 ~~~~
    177 #define GR_GO_SLOWER 0
    178 ...
    179 #if GR_GO_SLOWER
    180     Sleep(1000);
    181 #endif
    182 ~~~~
    183 
    184 Skia tends to use `#ifdef SK_MACRO` for boolean flags.
    185 
    186 Braces
    187 ------
    188 
    189 Open braces don't get a newline. `else` and `else if` appear on same line as
    190 opening and closing braces unless preprocessor conditional compilation
    191 interferes. Braces are always used with `if`, `else`, `while`, `for`, and `do`.
    192 
    193 <!--?prettify?-->
    194 ~~~~
    195 if (...) {
    196     oneOrManyLines;
    197 }
    198 
    199 if (...) {
    200     oneOrManyLines;
    201 } else if (...) {
    202     oneOrManyLines;
    203 } else {
    204     oneOrManyLines;
    205 }
    206 
    207 for (...) {
    208     oneOrManyLines;
    209 }
    210 
    211 while (...) {
    212     oneOrManyLines;
    213 }
    214 
    215 void function(...) {
    216     oneOrManyLines;
    217 }
    218 
    219 if (!error) {
    220     proceed_as_usual();
    221 }
    222 #if HANDLE_ERROR
    223 else {
    224     freak_out();
    225 }
    226 #endif
    227 ~~~~
    228 
    229 Flow Control
    230 ------------
    231 
    232 There is a space between flow control words and parentheses and between
    233 parentheses and braces:
    234 
    235 <!--?prettify?-->
    236 ~~~~
    237 while (...) {
    238 }
    239 
    240 do {
    241 } while(...);
    242 
    243 switch (...) {
    244 ...
    245 }
    246 ~~~~
    247 
    248 Cases and default in switch statements are indented from the switch.
    249 
    250 <!--?prettify?-->
    251 ~~~~
    252 switch (color) {
    253     case kBlue:
    254         ...
    255         break;
    256     case kGreen:
    257         ...
    258         break;
    259     ...
    260     default:
    261        ...
    262        break;
    263 }
    264 ~~~~
    265 
    266 Fallthrough from one case to the next is commented unless it is trivial:
    267 
    268 <!--?prettify?-->
    269 ~~~~
    270 switch (recipe) {
    271     ...
    272     case kCheeseOmelette_Recipe:
    273         ingredients |= kCheese_Ingredient;
    274         // fallthrough
    275     case kPlainOmelette_Recipe:
    276         ingredients |= (kEgg_Ingredient | kMilk_Ingredient);
    277         break;
    278     ...
    279 }
    280 ~~~~
    281 
    282 When a block is needed to declare variables within a case follow this pattern:
    283 
    284 <!--?prettify?-->
    285 ~~~~
    286 switch (filter) {
    287     ...
    288     case kGaussian_Filter: {
    289         Bitmap srcCopy = src->makeCopy();
    290         ...
    291         break;
    292     }
    293     ...
    294 };
    295 ~~~~
    296 
    297 Classes
    298 -------
    299 
    300 Unless there is a need for forward declaring something, class declarations
    301 should be ordered `public`, `protected`, `private`. Each should be preceded by a
    302 newline. Within each visibility section (`public`, `private`), fields should not be
    303 intermixed with methods.  It's nice to keep all data fields together at the end.
    304 
    305 <!--?prettify?-->
    306 ~~~~
    307 class SkFoo {
    308 
    309 public:
    310     ...
    311 
    312 protected:
    313     ...
    314 
    315 private:
    316     void barHelper(...);
    317     ...
    318 
    319     SkBar fBar;
    320     ...
    321 };
    322 ~~~~
    323 
    324 Subclasses should have a private typedef of their super class called INHERITED:
    325 
    326 <!--?prettify?-->
    327 ~~~~
    328 class GrDillPickle : public GrPickle {
    329     ...
    330 private:
    331     typedef GrPickle INHERITED;
    332 };
    333 ~~~~
    334 
    335 Virtual functions that are overridden in derived classes should use override,
    336 and the virtual keyword should be omitted.
    337 
    338 <!--?prettify?-->
    339 ~~~~
    340 void myVirtual() override {
    341 }
    342 ~~~~
    343 
    344 All references to base-class implementations of a virtual function
    345 should be explicitly qualified:
    346 
    347 <!--?prettify?-->
    348 ~~~~
    349 void myVirtual() override {
    350     ...
    351     this->INHERITED::myVirtual();
    352     ...
    353 }
    354 ~~~~
    355 
    356 Constructor initializers should be one per line, indented, with punctuation
    357 placed before the initializer. This is a fairly new rule so much of the existing
    358 code is non-conforming. Please fix as you go!
    359 
    360 <!--?prettify?-->
    361 ~~~~
    362 GrDillPickle::GrDillPickle()
    363     : GrPickle()
    364     , fSize(kDefaultPickleSize) {
    365     ...
    366 }
    367 ~~~~
    368 
    369 Constructors that take one argument should almost always be explicit, with
    370 exceptions made only for the (rare) automatic compatibility class.
    371 
    372 <!--?prettify?-->
    373 ~~~~
    374 class Foo {
    375     explicit Foo(int x);  // Good.
    376     Foo(float y);         // Spooky implicit conversion from float to Foo.  No no no!
    377     ...
    378 };
    379 ~~~~
    380 
    381 Method calls within method calls should be prefixed with dereference of the
    382 'this' pointer. For example:
    383 
    384 <!--?prettify?-->
    385 ~~~~
    386 this->method();
    387 ~~~~
    388 
    389 Integer Types
    390 -------------
    391 
    392 We follow the Google C++ guide for ints and are slowly making older code conform to this
    393 
    394 (http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Integer_Types)
    395 
    396 Summary: Use `int` unless you have need a guarantee on the bit count, then use
    397 `stdint.h` types (`int32_t`, etc). Assert that counts, etc are not negative instead
    398 of using unsigned. Bitfields use `uint32_t` unless they have to be made shorter
    399 for packing or performance reasons.
    400 
    401 `nullptr`, 0
    402 ------------
    403 
    404 Use `nullptr` for pointers, 0 for ints. We suggest explicit `nullptr` comparisons when
    405 checking for `nullptr` pointers, as documentation:
    406 
    407 <!--?prettify?-->
    408 ~~~~
    409 if (nullptr == x) {  // slightly preferred over if (!x)
    410    ...
    411 }
    412 ~~~~
    413 
    414 When checking non-`nullptr` pointers we think implicit comparisons read better than
    415 an explicit comparison's double negative:
    416 
    417 <!--?prettify?-->
    418 ~~~~
    419 if (x) {  // slightly preferred over if (nullptr != x)
    420    ...
    421 }
    422 ~~~~
    423 
    424 Function Parameters
    425 -------------------
    426 
    427 Mandatory constant object parameters are passed to functions as const references.
    428 Optional constant object parameters are passed to functions as const pointers.
    429 Mutable object parameters are passed to functions as pointers.
    430 We very rarely pass anything by non-const reference.
    431 
    432 <!--?prettify?-->
    433 ~~~~
    434 // src and paint are optional
    435 void SkCanvas::drawBitmapRect(const SkBitmap& bitmap, const SkIRect* src,
    436                               const SkRect& dst, const SkPaint* paint = nullptr);
    437 
    438 // metrics is mutable (it is changed by the method)
    439 SkScalar SkPaint::getFontMetrics(FontMetric* metrics, SkScalar scale) const;
    440 
    441 ~~~~
    442 
    443 If function arguments or parameters do not all fit on one line, the overflowing
    444 parameters may be lined up with the first parameter on the next line
    445 
    446 <!--?prettify?-->
    447 ~~~~
    448 void drawBitmapRect(const SkBitmap& bitmap, const SkRect& dst,
    449                     const SkPaint* paint = nullptr) {
    450     this->drawBitmapRectToRect(bitmap, nullptr, dst, paint,
    451                                kNone_DrawBitmapRectFlag);
    452 }
    453 ~~~~
    454 
    455 or all parameters placed on the next line and indented eight spaces
    456 
    457 <!--?prettify?-->
    458 ~~~~
    459 void drawBitmapRect(
    460         const SkBitmap& bitmap, const SkRect& dst,
    461         const SkPaint* paint = nullptr) {
    462     this->drawBitmapRectToRect(
    463             bitmap, nullptr, dst, paint, kNone_DrawBitmapRectFlag);
    464 }
    465 ~~~~
    466 
    467 Python
    468 ------
    469 
    470 Python code follows the [Google Python Style Guide](http://google-styleguide.googlecode.com/svn/trunk/pyguide.html).
    471 
    472