FALSE_IT
本文讲一个实用的语法糖(suger),很不错,攻克了我实际工作中的问题。
如果你写了这样一个类:
class Executor {
int step1();
void step2();
int step3();
}
#define FAIL -1
int main() {
// 使用Executor。调用顺序必须是1、2、3
Executor exec;
if (FAIL == exec.step1()) {
log('error');
} else {
exec.step2(); // 不能通过if调用,由于返回值是void
if (FAIL == exec.step3()) {
log('error');
}
}
}
就由于一个void返回,让代码一下子别扭了,丑不丑?
如果能这样写该多好:
Executor exec;
if (FAIL == exec.step1()) {
log('error');
} else if (FAIL == exec.step2()) {
log('error');
} else if (FAIL == exec.step3()) {
log('error');
}
事实上办法是有的。仅仅须要写这样一个宏:
\#define FALSE_IT(stmt) ({ (stmt); false; })
Executor exec;
if (SUCCESS == exec.step1()) {
log('error');
} else if (FALSE_IT(exec.step2()) { // 应用FALSE_IT宏
log('error'); // won't be here
} else if (FAIL == exec.step3()) {
log('error');
}
美丽不!
本方法来自阿里巴巴 OceanBase团队 @符风 同学。
FALSE_IT的更多相关文章
随机推荐
- 周赛Problem 1025: Hkhv love spent money(RMQ)
Problem 1025: Hkhv love spent money Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit interger ...
- [codevs2185]最长公共上升子序列
[codevs2185]最长公共上升子序列 试题描述 熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目.小沐沐先让奶牛研究了最长上升子序列,再让他们研究了最长公共子序列,现在又让他们要研究最长公共上升子序 ...
- UVA12206 Stammering Aliens 【SAM 或 二分 + hash】
题意 求一个串中出现至少m次的子串的最大长度,对于最大长度,求出最大的左端点 题解 本来想练哈希的,没忍住就写了一个SAM SAM拿来做就很裸了 只要检查每个节点的right集合大小是否不小于m,然后 ...
- 学习orm框架及一些看法
首先说说我对现在主流的ORM框架的一些看法: 优点: 让程序员不再关注数据库细节,专心在业务逻辑上,程序员可以不懂数据库就可以开发系统. 让数据库迁移变的非常方便,如果系统需要更改使用的数据库,直接改 ...
- [POI2006] KRA-The Disks (贪心)
题目描述 For his birthday present little Johnny has received from his parents a new plaything which cons ...
- websphere启用高速缓存导致问题
环境:websphere 7 一个流程主页,里面include了上面这个页面,内部有一个iframe: 现象:项目发布在测试环境中,打开流程主页时,里面iframe内页显示不出来: 同样的jsp页面, ...
- zoj 2760(网络流+floyed)
How Many Shortest Path Time Limit: 10 Seconds Memory Limit: 32768 KB Given a weighted directed ...
- awk 统计
命令太多,记不住,组合起来用一把…..示例文件: 1 2 3 4 5 6 7 8 9 10 11 [root@lovedan test]# cat a.txt hello good world hel ...
- The 2016 ACM-ICPC Asia China-Final Contest Promblem D
显然答案具有单调性,可以二分.问题是 我们二分出一个 堆数,该怎么判定能否达到这个堆数呢? 我们可以很简单的用调整法证明,最底下的一层的冰淇淋肯定是最小的那些,往上叠加的话我们再贪心的让较少的放在较小 ...
- 335. Self Crossing
/* * 335. Self Crossing * 2016-7-10 by Mingyang */ // Categorize the self-crossing scenarios, there ...