Skip to content
Sebastian Wilzbach edited this page Jun 29, 2016 · 7 revisions

A list of tips & tricks.

General

  • no magic constants in code like 1e-6
  • check for all types. AliasSeq can be used.
  • avoid unnecessary templates
  • avoid use of Phobos (std.traits and std.meta are ok)
  • use fully qualified local imports (std.algorithm.comparison : equal instead of std.algorithm: equal)
  • follow the D-Style

APIs

  • avoid constraints for internal API
  • don't duplicate API headers for internal functions
  • provide const guarentees for parameters
  • use scope for function parameter where it can be used (combine with const to in)
  • all non public code for testing should be in internal
  • use local imports & avoid global imports (except for needed types for methods)

Math functions

  • no ^^ -> use pow and powi (^^ isn't lowered to pow, this is an open bug in D)
  • sgn -> copysign
  • instead of std.math use mir.internal.math if possible (aliases to LLVM internals)
  • instead of a / 2 use `a * S(0.5) etc
  • use type constructor double(0.4)

Data objects

  • order structs after types (bool and enums at bottom)
  • functions after this constructor

Loops & data flow

  • infinite loops should use for (;;) instead of while (true)
  • if possible, replace if with switch
  • prefer switch with default over final switch (one jump less)
Clone this wiki locally