# SCRM 2020-07

## 【Code Review】SCRM后端2020-07-11

本期Review发现及总结以下事项：

1、不应把整个不明确的$param参数传到service层， 可读性差，维护难。

2、sql单条查询结果不能直接链式toArray()，如遇空记录集时会报错。

3、语法习惯（可学习），少用if(...){很长逻辑代码}else{很长逻辑代码}嵌套代码，多用if(....){跳出}，后续再写逻辑代码。

4、语法习惯：少用if(...){xx=aaa;}else{xx=bbb;}，多用xx=bbb;if(...){xx=aaa;}，或用三目运算，让代码更简洁优雅。

5、多层for循环写库操作，应先循环到数组，最后才写库，减少数据库IO，提高效率。

6、注意锁的使用：lockForUpdate读写锁死，有可能导致前端读取阻塞。

7、返回接口的字段有些无需格式化，如时间，展示状态，交由前端处理。

8、方法名与变量名称不允许简写，命名要规范（驼峰），保证可读性。

9、try catch里的事务，应在catch里写回滚，不应在try里回滚。因为try里抛出了异常，没有执行到rollback。

10、新建表Model里必须声名主键。不声名的话，使用save方法将报错。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tech.banchengyun.com/docs/backend/codereview/202007.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
