diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 06c16fc..9da0d9a 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -1088,9 +1088,40 @@ Comments and parameter names can help, but we could be explicit: Obviously, we cannot catch all errors through the static type system (e.g., the fact that a first argument is supposed to be a top-left point is left to convention (naming and comments)). -##### Example +##### Example, bad -??? units: time duration ??? +In the following example, it is not clear from the interface what time_to_blink means: Seconds? Milliseconds? + + void blink_led(int time_to_blink) //bad - the unit is ambiguous + { + //... + //do something with time_to_blink + //... + } + + void use() + { + blink_led(2); + } + +##### Example, good + +std::chrono::duration types introduced in C++11 helps making the unit of time duration explicit. + + template + void blink_led(duration time_to_blink) //good - accepts any unit + { + auto milliseconds_to_blink = duration_cast(time_to_blink); + //... + //do something with milliseconds_to_blink + //... + } + + void use() + { + blink_led(2s); + blink_led(1500ms); + } ##### Enforcement