それはすべて、利点と欠点の両方を伴う好みの問題です。
エラーが発生した場所で文字列リテラルをハードコーディングすると、保守が難しくなる可能性がありますが、私の正直な意見では読みやすくなります。
例えば
cout << "You were unable to login. "
<< "Please check you're user name and password and try again"
<< endl;
よりもはるかに優れた意図を示します
cout << LOGIN_CREDENTIALS_ERROR << endl;
ただし、メッセージをハードコーディングしないことのプラス面 (2
との両方3
):
//Foo.cpp:
cout << DIVIDE_BY_ZERO_ERROR << endl;
//Bar.cpp
cout << DIVIDE_BY_ZERO_ERROR << endl;
// If you want to change DIVIDE_BY_ZERO_ERROR text you only have to do it once
//ErrorMessages.h (Ops grammar needs correcting)
const std:string DIVIDE_BY_ZERO_ERROR = "Dont not divide by zero";
また、エラー メッセージが変更される可能性がある場合:
// ErrorMessages.h
#ifdef LOCALIZATION_EN
const std:string FRIENDLY_ERROR = "Hello, you are doing something wrong";
#elseif LOCALIZATION_FR
const std:string FRIENDLY_ERROR = "Bonjour, ...";
...
また
// ErrorMessages.h
#ifdef DEBUG
const std:string SOME_ERROR = "More detailed error information for developers"
#else
const std:string SOME_ERROR = "Human friendly error message"
#endif