当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Didi elasticsearch cluster cross version upgrade and platform reconfiguration
- Analysis of ThreadLocal principle
- Jmeter——ForEach Controller&Loop Controller
- Listening to silent words: hand in hand teaching you sign language recognition with modelarts
- DRF JWT authentication module and self customization
- Wiremock: a powerful tool for API testing
- Microservices: how to solve the problem of link tracing
- Classical dynamic programming: complete knapsack problem
- Architecture article collection
- Skywalking series blog 2-skywalking using
猜你喜欢
比特币一度突破14000美元,即将面临美国大选考验
合约交易系统开发|智能合约交易平台搭建
How do the general bottom buried points do?
Technical director, to just graduated programmers a word - do a good job in small things, can achieve great things
助力金融科技创新发展,ATFX走在行业最前列
在大规模 Kubernetes 集群上实现高 SLO 的方法
“颜值经济”的野望:华熙生物净利率六连降,收购案遭上交所问询
I'm afraid that the spread sequence calculation of arbitrage strategy is not as simple as you think
华为云“四个可靠”的方法论
向北京集结!OpenI/O 2020启智开发者大会进入倒计时
随机推荐
Installing the consult cluster
中小微企业选择共享办公室怎么样?
Serilog原始碼解析——使用方法
PHPSHE 短信插件说明
Python3 e-learning case 4: writing web proxy
[performance optimization] Nani? Memory overflow again?! It's time to sum up the wave!!
Troubleshooting and summary of JVM Metaspace memory overflow
Wiremock: a powerful tool for API testing
Programmer introspection checklist
直播预告 | 微服务架构学习系列直播第三期
如何玩转sortablejs-vuedraggable实现表单嵌套拖拽功能
[event center azure event hub] interpretation of error information found in event hub logs
PLC模拟量输入和数字量输入是什么
Aprelu: cross border application, adaptive relu | IEEE tie 2020 for machine fault detection
PHP应用对接Justswap专用开发包【JustSwap.PHP】
Network security engineer Demo: the original * * is to get your computer administrator rights! 【***】
Asp.Net Core learning notes: Introduction
华为云“四个可靠”的方法论
Python + appium automatic operation wechat is enough
How to demote a domain controller in Windows Server 2012 and later