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 <cassert>

 

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 <cassert>

#include <stdexcept>

 

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 HollingworthBob SwartMark CashmanPaul 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 SutterAndrei 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'.

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值