请赐教:
你更倾向哪个?为什么?[可读性?内存关注?其他一些问题?]
1.
String strSomething1 = someObject.getSomeProperties1(); strSomething1 = doSomeValidation(strSomething1); String strSomething2 = someObject.getSomeProperties2(); strSomething2 = doSomeValidation(strSomething2); String strSomeResult = strSomething1 + strSomething2; someObject.setSomeProperties(strSomeResult);
2.
someObject.setSomeProperties(doSomeValidation(someObject.getSomeProperties1()) + doSomeValidation(someObject.getSomeProperties2()));
如果你以其他方式做,那会是什么?你为什么这样做?
我会去:
String strSomething1 = someObject.getSomeProperties1(); String strSomething2 = someObject.getSomeProperties2(); // clean-up spaces strSomething1 = removeTrailingSpaces(strSomething1); strSomething2 = removeTrailingSpaces(strSomething2); someObject.setSomeProperties(strSomething1 + strSomething2);
我个人的偏好是按行动组织,而不是按顺序组织.我认为它看起来更好.
我可能会介入:
String strSomething1 = doSomeValidation(someObject.getSomeProperties1()); String strSomething2 = doSomeValidation(someObject.getSomeProperties2()); someObject.setSomeProperties(strSomething1 + strSomething2);
选项#2在一行中似乎要做很多事情.它是可读的,但需要花费一点力气才能解析.在选项#1中,每一行都是非常易读且清晰的,但是当我复习时,冗长会减慢我的速度.我试图平衡上面的简洁和清晰,每行代表一个简单的代码"句子".
我更喜欢第二个.您可以通过一些格式化使其具有可读性,而无需声明额外的中间引用.
someObject.setSomeProperties( doSomeValidation( someObject.getSomeProperties1() ) + doSomeValidation( someObject.getSomeProperties2() ));
您的方法名称提供了所需的所有解释.