当前位置:网站首页>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
边栏推荐
- The new telecom "routine", my dad was tricked!
- Apache EventMesh distributed event-driven multi-runtime
- 广汽本田安全体验营:“危险”是最好的老师
- AcWing 1282. Search Keyword Problem Solution ((AC Automata) Trie+KMP)+bfs)
- 架构实战营模块八作业
- Batch (batch size, full batch, mini batch, online learning), iterations and epochs in deep learning
- npm 更改为淘宝镜像的方法[通俗易懂]
- UserAgent 解析
- 华为手机一键开启“维修模式”隐藏所有数据,让手机隐私更加安全
- Introduction to Audio Types and Encoding Formats in Unity
猜你喜欢
随机推荐
Bika LIMS 开源LIMS集—— SENAITE的使用(检测流程)
Huawei mobile phone one-click to open "maintenance mode" to hide all data and make mobile phone privacy more secure
MySQL---sort and pagination
Routing interception of WeChat applet
杰理语音芯片ic玩具芯片ic的介绍_AD14NAD15N全系列开发
Apache EventMesh 分布式事件驱动多运行时
基于WPF重复造轮子,写一款数据库文档管理工具(一)
新型电信“套路”,我爸中招了!
[TypeScript] OOP
Tkinter 入门之旅
Masterless replication system (2) - read and write quorum
Linux环境redis集群搭建「建议收藏」
ResNet的基础:残差块的原理
MySQL---聚合函数
The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]
-xms -xmx(information value)
Taobao/Tmall get Taobao password real url API
【愚公系列】2022年07月 Go教学课程 023-Go容器之列表
高通cDSP简单编程例子(实现查询高通cDSP使用率、签名),RK3588 npu使用率查询
【PIMF】OpenHarmony 啃论文俱乐部—盘点开源鸿蒙三方库【3】








