You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
hanyong edited this page Jan 17, 2018
·
1 revision
常用开发编码技巧
退出 scope 时自动清理
C++ 使用栈对象的析构函数
java
使用 try { } finally {} 语法。
缺点:代码增加缩进层次。
使用 @lombok.Cleanup。
缺点:有时需要创建对象进行封装。增加定义类和创建对象成本。
StringBuilderMarker 类定义示例:
public class StringBuilderMarker implements AutoCloseable {
public final StringBuilder sb;
public final int len;
public StringBuilderMarker(StringBuilder sb) {
this.sb = sb;
this.len = sb.length();
}
@Override
public void close() {
sb.setLength(len);
}