当前位置:网站首页>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
边栏推荐
- Get Douyin Video Details API
- 获取抖音视频详情 API
- [Network Communication 3] Advantech Gateway Modbus Service Settings
- leetcode 665. Non-decreasing Array
- Introduction to Audio Types and Encoding Formats in Unity
- Unity 之 音频类型和编码格式介绍
- AI 自动写代码插件 Copilot(副驾驶员)
- Jiuqi ny3p series voice chip replaces the domestic solution KT148A, which is more cost-effective and has a length of 420 seconds
- 高通cDSP简单编程例子(实现查询高通cDSP使用率、签名),RK3588 npu使用率查询
- AcWing 1282. Search Keyword Problem Solution ((AC Automata) Trie+KMP)+bfs)
猜你喜欢
TestCafe之如何进行调试
1161. Maximum Sum of Elements in Layer: Hierarchical Traversal Application Problems
MATLAB程序设计与应用 2.4 MATLAB常用内部函数
flowable工作流所有业务概念
程序员如何学习开源项目,这篇文章告诉你
How programmers learn open source projects, this article tells you
架构师04-应用服务间加密设计和实践
高通cDSP简单编程例子(实现查询高通cDSP使用率、签名),RK3588 npu使用率查询
IP protocol from 0 to 1
【NLP】什么是模型的记忆力!
随机推荐
京东按关键字搜索商品 API
Masterless replication system (2) - read and write quorum
Golang 切片删除指定元素的几种方法
Last write wins (discards concurrent writes)
npm 更改为淘宝镜像的方法[通俗易懂]
leetcode 665. Non-decreasing Array
<artifactId>ojdbc8</artifactId>「建议收藏」
Flink_CDC搭建及简单使用
Kotlin协程:续体、续体拦截器、调度器
高通cDSP简单编程例子(实现查询高通cDSP使用率、签名),RK3588 npu使用率查询
抖音根据关键词取视频列表 API
Taobao/Tmall get Taobao password real url API
leetcode 665. Non-decreasing Array 非递减数列(中等)
深度学习中的batch(batch size,full batch,mini batch, online learning)、iterations与epoch
matplotlib ax bar color Set the color, transparency, label legend of the ax bar
获取抖音视频详情 API
基于WPF重复造轮子,写一款数据库文档管理工具(一)
【AcWing】The 62nd Weekly Match 【2022.07.30】
API for JD.com to obtain historical price information of commodities
Carbon教程之 基本语法入门大全 (教程)