当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- 有关PDF417条码码制的结构介绍
- Installing the consult cluster
- Aprelu: cross border application, adaptive relu | IEEE tie 2020 for machine fault detection
- Nodejs crawler captures ancient books and records, a total of 16000 pages, experience summary and project sharing
- CCR炒币机器人:“比特币”数字货币的大佬,你不得不了解的知识
- GDB除錯基礎使用方法
- Every day we say we need to do performance optimization. What are we optimizing?
- Serilog原始碼解析——使用方法
- Do not understand UML class diagram? Take a look at this edition of rural love class diagram, a learn!
- 100元扫货阿里云是怎样的体验?
猜你喜欢
Existence judgment in structured data
至联云解析:IPFS/Filecoin挖矿为什么这么难?
Examples of unconventional aggregation
Do not understand UML class diagram? Take a look at this edition of rural love class diagram, a learn!
华为云“四个可靠”的方法论
Grouping operation aligned with specified datum
多机器人行情共享解决方案
使用 Iceberg on Kubernetes 打造新一代云原生数据湖
How long does it take you to work out an object-oriented programming interview question from Ali school?
EOS创始人BM: UE,UBI,URI有什么区别?
随机推荐
[performance optimization] Nani? Memory overflow again?! It's time to sum up the wave!!
In order to save money, I learned PHP in one day!
Network security engineer Demo: the original * * is to get your computer administrator rights! 【***】
ipfs正舵者Filecoin落地正当时 FIL币价格破千来了
Using consult to realize service discovery: instance ID customization
钻石标准--Diamond Standard
(2)ASP.NET Core3.1 Ocelot路由
使用 Iceberg on Kubernetes 打造新一代云原生数据湖
Summary of common algorithms of linked list
Save the file directly to Google drive and download it back ten times faster
ES6学习笔记(五):轻松了解ES6的内置扩展对象
合约交易系统开发|智能合约交易平台搭建
The practice of the architecture of Internet public opinion system
怎么理解Python迭代器与生成器?
TRON智能钱包PHP开发包【零TRX归集】
Using Es5 to realize the class of ES6
Skywalking series blog 2-skywalking using
Leetcode's ransom letter
Menu permission control configuration of hub plug-in for azure Devops extension
Computer TCP / IP interview 10 even asked, how many can you withstand?