0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

coding > default parameterが嫌いな理由 > ソースリーディングしにくい

Last updated at Posted at 2016-01-04

default parametertという機能がC++にはある。

使い方

使い方としては以下。

`1. ヘッダファイルにdefault値を書く

somefile.h
void someFunc(bool isTop = true, bool isLeft= false);

`2. ソースファイルにパラメータを省略した呼び出しを書く。

somefile.cpp
someFunc(); // default parameter使用
someFunc(false); // default parameter使用
someFunc(false, true); // default parameter不使用

使いたくない理由

自分がdefault parameterを嫌いな理由は「ソースリーディング中に、デフォルト値がわからない」点。別途、ヘッダファイルをいちいち読まないといけない。

小さいソフトであればそれぞれのデフォルトパラメータを記憶することもできるが、余計な記憶をしないといけない。
Readable Codeなどで紹介されているように、以下のような書き方にしておくことで、余計な記憶が不要で、コードリーディングに専念できる。

someFunc(/* isTop=*/false, /* isLeft=*/true);

関連 pros and cons

関連して default parametersのpros and cons(賛成と反対)のリンクを貼っておく。以下では賛成理由と、上記とは別の反対理由が記載されている。

いい点もあり、悪い点もある。

場合によってはいい使い方もあるかもしれないが、自分はあまり使いたくない。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?