当前位置:网站首页>Returns a zero-length array or empty collection, do not return null
Returns a zero-length array or empty collection, do not return null
2022-07-31 19:03:00 【y_bccl27】
1. Returns a zero-length array
private final List cheesesInStock = ...;public Cheese[] getCheeses(){if (cheesesInStock.size() == 0)return null;} It is counterintuitive to treat the situation where there is no cheese (Cheese) available as a special case.Doing so would require the client to have additional code to handle the null return value, for example:
Cheese[] cheeses = shop.getCheeses();if(cheeses != null && Arrays.asList(cheeses).contains(Cheese.STILTON)){System.out.println("Very good, just right");}Instead of this code:
if( Arrays.asList(cheeses).contains(Cheese.STILTON)){System.out.println("Very good, just right");}This is error-prone because Programmers writing client-side programs may forget to write such specialized code to handle null return values, such mistakes may go unnoticed for years.
Sometimes some people think that a null return value is better than a zero-length array because it avoids allocating an array
边栏推荐
- 多线程之锁
- Basics of ResNet: Principles of Residual Blocks
- 全平台GPU通用AI视频补帧超分教程
- matplotlib ax bar color Set the color, transparency, label legend of the ax bar
- Huawei mobile phone one-click to open "maintenance mode" to hide all data and make mobile phone privacy more secure
- idea中搜索具体的字符内容的快捷方式
- Routing interception of WeChat applet
- 基于WPF重复造轮子,写一款数据库文档管理工具(一)
- leetcode 665. Non-decreasing Array
- leetcode: 6135. The longest ring in the graph [inward base ring tree + longest ring board + timestamp]
猜你喜欢

2022年Android 面经总结(附含面试题 | 源码 | 面试资料)

35道MySQL面试必问题图解,这样也太好理解了吧

leetcode:6135. 图中的最长环【内向基环树 + 最长环板子 + 时间戳】

Introduction of Jerry voice chip ic toy chip ic_AD14NAD15N full series development

使用 Flutter 和 Firebase 制作!计数器应用程序

20.支持向量机—数学原理知识

Apache EventMesh distributed event-driven multi-runtime

Flex布局详解

Shell 脚本 快速入门到实战 -02

How programmers learn open source projects, this article tells you
随机推荐
[Network Communication 3] Advantech Gateway Modbus Service Settings
Three.js入门
Cache and Database Consistency Solutions
leetcode: 6135. The longest ring in the graph [inward base ring tree + longest ring board + timestamp]
flowable工作流所有业务概念
Architect 04 - Application Service Encryption Design and Practice
MySQL---operator
京东获取商品历史价格信息 API
ECCV 2022 华科&ETH提出首个用于伪装实例分割的一阶段Transformer的框架OSFormer!代码已开源!...
1161. Maximum Sum of Elements in Layer: Hierarchical Traversal Application Problems
ThreadLocal
BOW/DOM(上)
Apache EventMesh distributed event-driven multi-runtime
【码蹄集新手村600题】不通过字符数组来合并俩个数字
华为手机一键开启“维修模式”隐藏所有数据,让手机隐私更加安全
移动web开发02
Combinatorics Notes (6) Associative Algebra of Locally Finite Partially Ordered Sets, Möbius Inversion Formula
mysql的备份表的几种方法
leetcode 665. Non-decreasing Array
Poker Game in C# -- Introduction and Code Implementation of Blackjack Rules