Java-Spring:在请求对象中应该处理列表中的空值吗?

huangapple go评论77阅读模式
英文:

Java- Spring: should we handle null on a list in a request-Object?

问题

应该在请求对象中处理列表中的空值。

我正在使用Spring(Java)来接受“保存新学生的请求”。

例如:

@Data
public class Student implements Serializable {

    private String name;
    
    private List<String> subjects;

}

我的问题更多是前端和后端之间的契约。

我需要的是关于是否应该允许student.subjects为空的逻辑解释/意见。

--

额外信息:

我已经参考了互联网上的多种观点。

其中最有道理的一个是:https://salesforce.stackexchange.com/a/217411

英文:

should we handle null on a list in a request-Object.

I am using Spring (java) to accept the request to save a new Student.

Eg :

@Data
public class Student implements Serializable {

    private String name;
    
    private List&lt;String&gt; subjects;

}

My question is more of a contract between front-end and backend.

What I need is : a logical-explanation/opinion on should we allow NULLs on student.subjects OR not ?

--

Extra :

i have referenced multiple opinions on the internet.

The one that makes the most sense is : https://salesforce.stackexchange.com/a/217411

答案1

得分: 0

这取决于您的项目需求,但从代码质量的角度来看,在这种情况下有两个关键原则是相关的。

  1. 不要返回NULL
  • 如果您想要返回NULL,您应该考虑抛出异常或返回一个特殊情况(特殊情况是与正常执行流程或行为不同的特定情况或条件)。

    特殊情况示例:

List<Employee> employees = getEmployees();
if (employees != null) { // 我们检查是否为null,因为getEmployees可能返回null
   for(Employee e : employees) {
      totalPay += e.getPay();
   }
}
// 如果我们更改getEmployees以返回一个空列表,
// 我们可以简化代码

List<Employee> employees = getEmployees();
for(Employee e : employees) {
   totalPay += e.getPay();
}
  1. 不要传递NULL
  • 除非您正在使用需要传递NULL的API,否则应尽量避免在代码中传递NULL
  • 如果有人传递NULL,我们无法更好地处理它,除非抛出运行时异常- 这样我们仍然会有运行时错误

这两点帮助我们理解,我们应该考虑简化客户端代码,减少业务逻辑中的混乱。

因此,从代码质量的角度来看,我们应该在尽可能的情况下避免处理代码中的NULL

参考资料: "Clean Code. A Handbook of Agile Software Craftsmanship"

英文:

It depends on your project requirements, but from a clean code standpoint, there are two key principles that are relevant in this context.

  1. <b>Do not return NULL</b> <br>
  • If you are tempted to return NULL, you should consider throwing an exception or returning a special case (a special case is a specific situation or condition that deviates from the normal flow of execution or behavior)<br><br>
    Special case example:

List&lt;Employee&gt; employees = getEmployees();
if (employees != null) { // we check for null because getEmployees can return null
   for(Employee e : employees) {
      totalPay += e.getPay();
   }
}
// If we change getEmployees so that it returns an empty list,
// we can clean up the code

List&lt;Employee&gt; employees = getEmployees();
for(Employee e : employees) {
   totalPay += e.getPay();
}
  1. <b>Do not pass NULL</b> <br>
  • you should avoid passing NULL in your code whenever possible, unless you
    are working with an API which expects you to pass NULL
  • if someone passes NULL, we could not handle it better than throwing a Runtime Exception - so we'll still have a runtime error

These two points help us understand that we should consider simplifying the client code and reducing clutter in the business logic.

Therefore, from a clean code perspective, we should <b>avoid</b> dealing with NULL in our code whenever possible.

Reference: "Clean Code. A Handbook of Agile Software Craftsmanship"

huangapple
  • 本文由 发表于 2023年7月3日 16:55:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603264.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定