当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Architecture article collection
- Synchronous configuration from git to consult with git 2consul
- Skywalking series blog 5-apm-customize-enhance-plugin
- Vue 3 responsive Foundation
- Real time data synchronization scheme based on Flink SQL CDC
- Arrangement of basic knowledge points
- This article will introduce you to jest unit test
- Swagger 3.0 天天刷屏,真的香嗎?
- Asp.Net Core學習筆記:入門篇
- Using Es5 to realize the class of ES6
猜你喜欢
PHPSHE 短信插件说明
I'm afraid that the spread sequence calculation of arbitrage strategy is not as simple as you think
Character string and memory operation function in C language
DTU连接经常遇到的问题有哪些
hadoop 命令总结
Grouping operation aligned with specified datum
容联完成1.25亿美元F轮融资
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)
IPFS/Filecoin合法性:保护个人隐私不被泄露
从海外进军中国,Rancher要执容器云市场牛耳 | 爱分析调研
随机推荐
A debate on whether flv should support hevc
ipfs正舵者Filecoin落地正当时 FIL币价格破千来了
Real time data synchronization scheme based on Flink SQL CDC
Filecoin的经济模型与未来价值是如何支撑FIL币价格破千的
Process analysis of Python authentication mechanism based on JWT
Flink on paasta: yelp's new stream processing platform running on kubernetes
Elasticsearch database | elasticsearch-7.5.0 application construction
GDB除錯基礎使用方法
Python + appium automatic operation wechat is enough
采购供应商系统是什么?采购供应商管理平台解决方案
How do the general bottom buried points do?
快快使用ModelArts,零基礎小白也能玩轉AI!
DTU连接经常遇到的问题有哪些
Wiremock: a powerful tool for API testing
容联完成1.25亿美元F轮融资
網路程式設計NIO:BIO和NIO
How to select the evaluation index of classification model
Technical director, to just graduated programmers a word - do a good job in small things, can achieve great things
合约交易系统开发|智能合约交易平台搭建
全球疫情加速互联网企业转型,区块链会是解药吗?