当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Classical dynamic programming: complete knapsack problem
- 制造和新的自动化技术是什么?
- 3分钟读懂Wi-Fi 6于Wi-Fi 5的优势
- 全球疫情加速互联网企业转型,区块链会是解药吗?
- htmlcss
- Network security engineer Demo: the original * * is to get your computer administrator rights! 【***】
- git rebase的時候捅婁子了,怎麼辦?線上等……
- How to demote a domain controller in Windows Server 2012 and later
- Subordination judgment in structured data
- I'm afraid that the spread sequence calculation of arbitrage strategy is not as simple as you think
猜你喜欢
Not long after graduation, he earned 20000 yuan from private work!
How long does it take you to work out an object-oriented programming interview question from Ali school?
采购供应商系统是什么?采购供应商管理平台解决方案
Network programming NiO: Bio and NiO
Do not understand UML class diagram? Take a look at this edition of rural love class diagram, a learn!
华为云“四个可靠”的方法论
Didi elasticsearch cluster cross version upgrade and platform reconfiguration
加速「全民直播」洪流,如何攻克延时、卡顿、高并发难题?
快快使用ModelArts,零基础小白也能玩转AI!
Don't go! Here is a note: picture and text to explain AQS, let's have a look at the source code of AQS (long text)
随机推荐
htmlcss
Summary of common string algorithms
连肝三个通宵,JVM77道高频面试题详细分析,就这?
Computer TCP / IP interview 10 even asked, how many can you withstand?
Technical director, to just graduated programmers a word - do a good job in small things, can achieve great things
至联云分享:IPFS/Filecoin值不值得投资?
Deep understanding of common methods of JS array
Skywalking series blog 1 - install stand-alone skywalking
WeihanLi.Npoi 1.11.0/1.12.0 Release Notes
如何玩转sortablejs-vuedraggable实现表单嵌套拖拽功能
DevOps是什么
After reading this article, I understand a lot of webpack scaffolding
It's so embarrassing, fans broke ten thousand, used for a year!
3分钟读懂Wi-Fi 6于Wi-Fi 5的优势
嘗試從零開始構建我的商城 (二) :使用JWT保護我們的資訊保安,完善Swagger配置
網路程式設計NIO:BIO和NIO
关于Kubernetes 与 OAM 构建统一、标准化的应用管理平台知识!(附网盘链接)
從小公司進入大廠,我都做對了哪些事?
Didi elasticsearch cluster cross version upgrade and platform reconfiguration
Analysis of ThreadLocal principle