メリットと落とし穴の両方が優先事項です。
エラーのサイトのハードコーディング文字列リテラルは、維持するのが難しいかもしれませんが、私の正直な意見では読みやすいです。
cout << "You were unable to login. "
<< "Please check you're user name and password and try again"
<< endl;
例えば
は、しかしながらメッセージ(両方2
と3
)をハードコーディングしないのプラス側を
cout << LOGIN_CREDENTIALS_ERROR << endl;
より意図かなり良く示す:
//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, ...";
...
OR
// 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
最初の方法は、非常にローカライズフレンドリーではありません。 – qdii
例外処理ですか? – Rapptz
... Aaaaaaand、エラーメッセージは 'cur'ではなく' cerr'に行きます。 –