What is the proper names for the following mutating methods?
What is the most proper names to replace the name of
Mutator_A
and Mutator_B
?
The names
- must be descriptive so we know what they do only by reading their names
- must not be too long
20 Replies
Is this a homework assignment?
Let me clarify why I ask. We can only see a part of the picture here. When it omes comes to nomenclature your domain is going to be best known by you, not me. What is your reasoning for asking us to name your methods for you, instead of determining the names yourself? Are you asking what is the idfference between the mutation of both methods?
I am looking for the proper adjective for these mutators, one mutator uses
ref
so it can repoint the passed argument to a new object and the other mutator without ref
that can only mutate the properties or fields of the passed argument.
It is not homework assignment.If you're sure that you're always assigning a value to the variable
file
in Mutator_A, you could reference it as an out
variable. This tells the compiler that it doesn't matter what's at that address when your method is called: you always assign it a value, regardless of its former value.Right now the names came in my head, the first one with
ref
is StrongMutator()
and the other one is WeakMutator()
.This would make your code more clear from a caller's perspective. I might Call it something like
GetFile
, CreateFile
, or ReadFile
. There's no real difference in that use vs returning the value, but more often you'll see that sort of argument in a Try...
method.
Here the Try
indicates that the method may or may not succeed, and the return value gives you the result of that attempt, while the out
parameter lets you access the result. (You must always assign to n
or any out parameter, hence giving it the default when the parse fails.)
You might see something like your signature in the following method, where you'd consider reassigning the variable but wouldn't always.
Here it's less the name indicating the type of mutation and more the intrinsic behavior of the function. I would recommend documenting in this case.
Honestly I wouldn't put any form of mutate
into your method's name. The fact that Mutator_A
takes a ref
indicates that you might reassign the pointer. That Mutator_B
doesn't indicates you only change the values. You might call that second method Initialize
since it takes an existing instance and puts it into a new state.
That's the beauty of strongly typed systems is that the types can define for you what you'll do with the value.Thank you.
Let's consider a real domain.
Usage:
- Suggested name from others :
request.MapTo(user);
.
- Mine: request.Fill(user);
// it is read as "request fills user".your question was hard to read so i misread it
fill would be fine
much better than the operator shenanigans anyway
request.MapTo(user);
is read as "request maps to user" but it is ambiguous, what does map mean here?I would certainly agree that
MapTo
, Initialize
, or Assign
would be appropriate for that method.mapping is the standard term for converting one representation of data to another
that's why you hear about mappers and some languages have specific functions like
map
that apply transformations over a set (it's called select in C# because linq things)In C#
Map
often refers to assigning values of one object to values of another object that is similar in structure.Should I add
ref
here to declare that the method mutates the argument user
clearly?no
ref
specifically indicates that you may change the reference itself, not change the object being referencedThere should be a way to indicate the passed argument is mutated inside the method or not.
that's why you choose a method name that makes that obvious
C# should prevent us from doing the following.
Even though it is meaningless.
or just don't do it
Well you can do that in case you want to use a different value for
user
, it reassigns your variable but not the caller's variable. So something like this is valid:
Thank you all.