kotlin中不提倡将=当作表达式
错误代码
StringBuilder body = new StringBuilder();
String nextLine;
while ((nextLine = bufferReader.readLine()) != null) {
body.append(nextLine);
body.append('\n');
}
修改后的正确代码
将判断和赋值语句分开
StringBuilder body = new StringBuilder();
String nextLine;
while ((nextLine = bufferReader.readLine()) != null) {
body.append(nextLine);
body.append('\n');
}
本文介绍了Kotlin编程中的一项最佳实践,即不建议在表达式中直接使用等号进行赋值和判断。通过示例展示了如何将赋值和判断分开,以提高代码可读性和维护性。修正后的代码更符合Kotlin的语法规则,避免了混淆。
1105

被折叠的 条评论
为什么被折叠?



