case1:
#define FEATURE_NOT_ENABLE
int main(int argc, char* argv[])
{
#ifndef FEATURE_NOT_ENABLE
do_feature_enable();
#else
do_feature_disable();
#endif
return 0;
}
case2:
int main(int argc, char* argv[])
{
bool b_support = false;
get_feature_support(&b_support);
if (!b_support)
{
do_feature_disable();
}
else
{
do_feature_enable();
}
return 0;
}
也许上面的两个案例你看起来感觉并没有什么问题,
实际上在代码量很大时总是会让人觉得不爽,
经常会需要在大脑中进行一次又一次的取反的运算过程。
为什么就不能让代码简单简洁一点呢?
#define FEATURE_ENABLE
int main(int argc, char* argv[])
{
#ifdef FEATURE_ENABLE
do_feature_enable();
#else
do_feature_disable();
#endif
return 0;
}
int main(int argc, char* argv[])
{
bool b_support = false;
get_feature_support(&b_support);
if (b_support)
{
do_feature_enable();
}
else
{
do_feature_disable();
}
return 0;
}
简单明了的代码能够提高代码的可读性,对代码维护是有好处的。