当前位置:网站首页>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:19:59 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]所创,转载请带上原文链接,感谢