当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Filecoin的经济模型与未来价值是如何支撑FIL币价格破千的
- Keyboard entry lottery random draw
- 数字城市响应相关国家政策大力发展数字孪生平台的建设
- 合约交易系统开发|智能合约交易平台搭建
- hadoop 命令总结
- Let the front-end siege division develop independently from the back-end: Mock.js
- Didi elasticsearch cluster cross version upgrade and platform reconfiguration
- Python crawler actual combat details: crawling home of pictures
- Existence judgment in structured data
- Vue 3 responsive Foundation
猜你喜欢
条码生成软件如何隐藏部分条码文字
全球疫情加速互联网企业转型,区块链会是解药吗?
How long does it take you to work out an object-oriented programming interview question from Ali school?
Computer TCP / IP interview 10 even asked, how many can you withstand?
Cos start source code and creator
直播预告 | 微服务架构学习系列直播第三期
Python自动化测试学习哪些知识?
Elasticsearch database | elasticsearch-7.5.0 application construction
CCR炒币机器人:“比特币”数字货币的大佬,你不得不了解的知识
Use of vuepress
随机推荐
Use of vuepress
100元扫货阿里云是怎样的体验?
DRF JWT authentication module and self customization
前端都应懂的入门基础-github基础
Installing the consult cluster
Skywalking series blog 5-apm-customize-enhance-plugin
人工智能学什么课程?它将替代人类工作?
Filecoin的经济模型与未来价值是如何支撑FIL币价格破千的
比特币一度突破14000美元,即将面临美国大选考验
Python自动化测试学习哪些知识?
Analysis of react high order components
你的财务报告该换个高级的套路了——财务分析驾驶舱
Real time data synchronization scheme based on Flink SQL CDC
PN8162 20W PD快充芯片,PD快充充电器方案
PLC模拟量输入和数字量输入是什么
Synchronous configuration from git to consult with git 2consul
Dapr實現分散式有狀態服務的細節
Let the front-end siege division develop independently from the back-end: Mock.js
Elasticsearch database | elasticsearch-7.5.0 application construction
This article will introduce you to jest unit test