a correct Divide function

Go Richel Bilderbeek's blog

 

 

 

 a correct Divide function

 

This is the answer of exercise #0: a correct Divide function.

 

The lowest mark would go to the following code:

 

 

double Divide(double numerator, double denominator)

{

  return numerator / denominator;

}

 

 

There are three reasons why this function could be improved:

1) the function is not const-correct [1-4]

2) the function does not document its internal assumptions using assert [5-9]

3) the function is not exception-safe

 

This function assumes that the denominator is unequal to zero.

 

It does not impact speed in the release version if improvements (1) and (2) are satisfied.

The function below, satisfies the following improvements:

1) the function is const-correct [1-4]

2) the function does document its internal assumptions using assert [5-9]

 

 

 

#include 

 

const double Divide(const double numerator, const double denominator)

{

  assert(denominator != 0.0);

  return numerator / denominator;

}

 

 

Scott Meyers argues that in release code, one should still throw an exception [?]. This makes the Divide function as below:

 

 

#include 

#include 

 

const double Divide(const double numerator, const double denominator)

{

  assert(denominator != 0.0);

  if (denominator == 0.0) throw std::logic_error("Cannot divide by 0.0");

  return numerator / denominator;

}

 

 

 

References

[1] Bjarne Stroustrup. The C++ Programming Language (3rd edition).

    ISBN: 0-201-88954-4 7.9.3: 'Use const extensively and consistently'

[2] Scott Meyers. Effective C++ (3rd edition).ISBN: 0-321-33487-6.

    Item 3: 'Use const whenever possible'

[3] Jarrod Hollingworth, Bob Swart, Mark Cashman, Paul Gustavson.

    Sams C++ Builder 6 Developer's Guide. ISBN: 0-672-32480-6.

    Chapter 3: 'Understand and use const in your code'

[4] Jesse Liberty. Sams teach yourself C++ in 24 hours.

    ISBN: 0-672-32224-2. Hour 8, chapter 'Const member

    functions': 'Use const whenever possible.'

[5] Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines,

    and best practices. ISBN: 0-32-111358-6.

    Chapter 68: 'Assert liberally to document internal assumptions and invariants'.

[6] Bjarne Stroustrup. The C++ Programming Language (3rd edition). 1997.

    ISBN: 0-201-88954-4. Advice 24.5.18: 'Explicitly express preconditions,

    postconditions, and other assertions as assertions'.

[7] Steve McConnell. Code Complete (2nd edition). 2004. ISBN: -735619670.

    Chapter 8.2 'Assertions', paragraph 'Guidelines for using asserts':

    'Use assertions to document and verify preconditions and postconditions'.

[8] Steve McConnell. Code Complete (2nd edition). 2004. ISBN: -735619670.

    Chapter 8.2 'Assertions', paragraph 'Guidelines for using asserts':

    'Use assertions for conditions that should never occur'.

[9] Jesse Liberty. Sams teach yourself C++ in 24 hours. ISBN: 0-672-32224-2.

    Hour 24, chapter 'assert()': 'Use assert freely'.

 

你可能感兴趣的:(a correct Divide function)