当前位置:网站首页>What is the side effect free method? How to name it? - Mario
What is the side effect free method? How to name it? - Mario
2020-11-06 01:20:00 【On jdon】
The function name should describe everything the function does . let me put it another way , Functions should not hide side effects . This is in line with “ The principle of Least Surprise ”. however , Sometimes it's not easy . Do the following function nouns live up to their names ?

Opinions vary :
It will return one Cusomter, But if there's no available Customer, It will create a parameter free Customer. I will follow GetCustomerOrDefault() Of C# Naming style .
If the created customer is completely encapsulated , And no external code will know the difference between a new customer and an existing customer , So I think it's a good name . On the other hand , If there is no encapsulation , Maybe it should be two functions .
Bad names not only reduce readability , And it's usually a symptom of deeper quality problems . for example , If you can't find a good name for a function , The design of the function may not be good . How do you name this feature ? Its name is vague , Because the function has no cohesion .
The name TryGetCustomerOrInstantiate?
If you need side effects , I can rename it as getCustomerOrDefault() Names like that , In order to eliminate side effects . If you don't do that , An exception is thrown to the empty customer . You can even use both methods at the same time , It depends on your API How delicate it needs to be .
It does two things : Allow access to private customer fields ; If there is a lack of customers , To create a customer, I suggest avoiding this encoding !
banq: Breach of a single duty , Did two things ; Contains mutable States , With side effects , Cannot have the caller call getCustomer Method to return a Customer, The principle of functional programming without sign .
版权声明
本文为[On jdon]所创,转载请带上原文链接,感谢
边栏推荐
- Use of vuepress
- Tool class under JUC package, its name is locksupport! Did you make it?
- PHPSHE 短信插件说明
- Menu permission control configuration of hub plug-in for azure Devops extension
- Let the front-end siege division develop independently from the back-end: Mock.js
- GUI 引擎评价指标
- Python crawler actual combat details: crawling home of pictures
- Real time data synchronization scheme based on Flink SQL CDC
- 深度揭祕垃圾回收底層,這次讓你徹底弄懂她
- Deep understanding of common methods of JS array
猜你喜欢

3分钟读懂Wi-Fi 6于Wi-Fi 5的优势

业内首发车道级导航背后——详解高精定位技术演进与场景应用

阿里云Q2营收破纪录背后,云的打开方式正在重塑

Tool class under JUC package, its name is locksupport! Did you make it?

Subordination judgment in structured data

Flink的DataSource三部曲之二:内置connector

Aprelu: cross border application, adaptive relu | IEEE tie 2020 for machine fault detection

ES6学习笔记(五):轻松了解ES6的内置扩展对象

速看!互联网、电商离线大数据分析最佳实践!(附网盘链接)

直播预告 | 微服务架构学习系列直播第三期
随机推荐
htmlcss
Why do private enterprises do party building? ——Special subject study of geek state holding Party branch
合约交易系统开发|智能合约交易平台搭建
Flink on paasta: yelp's new stream processing platform running on kubernetes
比特币一度突破14000美元,即将面临美国大选考验
OPTIMIZER_ Trace details
Filecoin的经济模型与未来价值是如何支撑FIL币价格破千的
DRF JWT authentication module and self customization
阿里云Q2营收破纪录背后,云的打开方式正在重塑
Introduction to Google software testing
快快使用ModelArts,零基础小白也能玩转AI!
(2)ASP.NET Core3.1 Ocelot路由
至联云解析:IPFS/Filecoin挖矿为什么这么难?
The difference between Es5 class and ES6 class
Computer TCP / IP interview 10 even asked, how many can you withstand?
速看!互联网、电商离线大数据分析最佳实践!(附网盘链接)
How to select the evaluation index of classification model
熬夜总结了报表自动化、数据可视化和挖掘的要点,和你想的不一样
After reading this article, I understand a lot of webpack scaffolding
hadoop 命令总结