-
Notifications
You must be signed in to change notification settings - Fork 432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Use CHANGING sparingly, where suited] Is it ok to use IMPORTING REF instead of CHANGING? #331
Comments
"I'd prefer to use |
I think in general it is better to avoid mutating data in this manner altogether. In your example, is there is really a benefit to using methods add_to_parameter_name
importing parameter_name type string
returning value(result) type string. and replacing change_by_ref( ref #( variable ) ). by variable = add_to_parameter_name( variable ). ? Personally I find the latter much more readable - it is immediately clear to me that this line changes the content of |
Fully agree with @bjoern-jueliger-sap. With the added bonus that method |
@bjoern-jueliger-sap really nice way of doing that! I forgot about such variant. I completely agree that it's much cleaner in case of data types. Would you do the same for object instances, when for example variable is instance of an object? Or just importing can be enough? I guess in the case of objects the argument about mutation is not so strong, they are expected to mutate at any time, isn't it? |
For objects, the better solution is often to turn this into a method on the object. A function that takes an object instance as input and returns an object instance as output is isomorphic to a method on the object instance itself. (On the level of abstract type signatures, a method of an object type Concretely, if methods add_to_parameter_name. and we'd call it as variable->add_to_parameter_name( ). instead. |
Closing this since there seems to be agreement that the guide's recommendation against CHANGING in most situations is appropriate as is. |
Ah, yes, it is true. Thank you guys for help! I'll try to remember that way of thinking 🙂 |
Relevant sections of the style guide
Use CHANGING sparingly, where suited
Question
I recently found that I really hate CHANGING parameters, mainly because changing forces to type it with interface parameter name during method call. I got the idea that I even can use importing by reference instead of using changing. But I'm curious if it is ok.
So here is example of use of both variants:
Advantage of the ref that calling is simpler and possible easier refactor for renaming interface parameters.
But disadvantage is that inside of method need to work with ref. Both the advantage and the disadvantage are not big, so probably it is personal. What do you think?
Also another question comes from that discussion.
For example variable is already reference (e.g. class instance) and method changes the content of the variable (e.g. even via it's methods). Can be that variable passed as importing, or should go to changing? and why?
The text was updated successfully, but these errors were encountered: