当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Nodejs crawler captures ancient books and records, a total of 16000 pages, experience summary and project sharing
- Network security engineer Demo: the original * * is to get your computer administrator rights! 【***】
- 基於MVC的RESTFul風格API實戰
- 網路程式設計NIO:BIO和NIO
- How to select the evaluation index of classification model
- 钻石标准--Diamond Standard
- xmppmini 專案詳解:一步一步從原理跟我學實用 xmpp 技術開發 4.字串解碼祕笈與訊息包
- 10 easy to use automated testing tools
- This article will introduce you to jest unit test
- Can't be asked again! Reentrantlock source code, drawing a look together!
猜你喜欢

数字城市响应相关国家政策大力发展数字孪生平台的建设

How to demote a domain controller in Windows Server 2012 and later

Summary of common string algorithms

Computer TCP / IP interview 10 even asked, how many can you withstand?

hadoop 命令总结

Network security engineer Demo: the original * * is to get your computer administrator rights! 【***】

小程序入门到精通(二):了解小程序开发4个重要文件

Summary of common algorithms of binary tree

Network security engineer Demo: the original * * is to get your computer administrator rights! 【***】

制造和新的自动化技术是什么?
随机推荐
IPFS/Filecoin合法性:保护个人隐私不被泄露
[JMeter] two ways to realize interface Association: regular representation extractor and JSON extractor
Can't be asked again! Reentrantlock source code, drawing a look together!
Leetcode's ransom letter
This article will introduce you to jest unit test
Filecoin最新动态 完成重大升级 已实现四大项目进展!
深度揭祕垃圾回收底層,這次讓你徹底弄懂她
Every day we say we need to do performance optimization. What are we optimizing?
直播预告 | 微服务架构学习系列直播第三期
Summary of common algorithms of linked list
Basic principle and application of iptables
Just now, I popularized two unique skills of login to Xuemei
Skywalking series blog 5-apm-customize-enhance-plugin
In depth understanding of the construction of Intelligent Recommendation System
hadoop 命令总结
html
GUI 引擎评价指标
钻石标准--Diamond Standard
從小公司進入大廠,我都做對了哪些事?
前端都应懂的入门基础-github基础