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
static void Mutator_A(ref File file)
{
file = new File { Id = Random.Shared.Next(), Name = Path.GetRandomFileName() };
}
static void Mutator_B(File file)
{
file.Id = Random.Shared.Next();
file.Name = Path.GetRandomFileName();
}
class File
{
public int Id;
public string Name = null!;
}
static void Mutator_A(ref File file)
{
file = new File { Id = Random.Shared.Next(), Name = Path.GetRandomFileName() };
}
static void Mutator_B(File file)
{
file.Id = Random.Shared.Next();
file.Name = Path.GetRandomFileName();
}
class File
{
public int Id;
public string Name = null!;
}
20 Replies
Stroniax
Stroniax10mo ago
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 like chatgpt
i like chatgptOP10mo ago
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.
Stroniax
Stroniax10mo ago
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.
i like chatgpt
i like chatgptOP10mo ago
Right now the names came in my head, the first one with ref is StrongMutator() and the other one is WeakMutator().
Stroniax
Stroniax10mo ago
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.
public bool TryParse(string? s, out int n) {
if (/* s converts to int */) {
n = /* int value */
return true;
}
else {
n = /* some default */
return false;
}
}
public bool TryParse(string? s, out int n) {
if (/* s converts to int */) {
n = /* int value */
return true;
}
else {
n = /* some default */
return false;
}
}
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.
public static void Resize<T>(ref T[] array, int capacity) {
if (array.Length == capacity) {
// array is not reassigned
return;
}
else {
// store in a temporary variable
var newSizeArray = new T[capacity];
for (int i = 0; i < capacity; i++) {
newSizeArray[i] = array[i];
}
// reassign the ref parameter to the new value
array = newSizeArray;
}
}
public static void Resize<T>(ref T[] array, int capacity) {
if (array.Length == capacity) {
// array is not reassigned
return;
}
else {
// store in a temporary variable
var newSizeArray = new T[capacity];
for (int i = 0; i < capacity; i++) {
newSizeArray[i] = array[i];
}
// reassign the ref parameter to the new value
array = newSizeArray;
}
}
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.
i like chatgpt
i like chatgptOP10mo ago
Thank you. Let's consider a real domain.
public sealed class User
{
public long Id {get; private init;}
public string Name { get; set; } = null!;
}
public sealed class UserIn
{
public string Name { get; init; } = null!;

public void WhatIsTheProperNameForThisMethod(User user)
{
user.Name = Name;
}
}
public sealed class User
{
public long Id {get; private init;}
public string Name { get; set; } = null!;
}
public sealed class UserIn
{
public string Name { get; init; } = null!;

public void WhatIsTheProperNameForThisMethod(User user)
{
user.Name = Name;
}
}
Usage:
var request = new UserIn{Name="Anton"};
var user = GetUserById(1);
request.WhatIsTheProperNameForThisMethod(user);
var request = new UserIn{Name="Anton"};
var user = GetUserById(1);
request.WhatIsTheProperNameForThisMethod(user);
- Suggested name from others : request.MapTo(user);. - Mine: request.Fill(user); // it is read as "request fills user".
Jimmacle
Jimmacle10mo ago
your question was hard to read so i misread it fill would be fine much better than the operator shenanigans anyway
i like chatgpt
i like chatgptOP10mo ago
request.MapTo(user); is read as "request maps to user" but it is ambiguous, what does map mean here?
Stroniax
Stroniax10mo ago
I would certainly agree that MapTo, Initialize, or Assign would be appropriate for that method.
Jimmacle
Jimmacle10mo ago
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)
Stroniax
Stroniax10mo ago
In C# Map often refers to assigning values of one object to values of another object that is similar in structure.
i like chatgpt
i like chatgptOP10mo ago
Should I add ref here to declare that the method mutates the argument user clearly?
Jimmacle
Jimmacle10mo ago
no ref specifically indicates that you may change the reference itself, not change the object being referenced
i like chatgpt
i like chatgptOP10mo ago
There should be a way to indicate the passed argument is mutated inside the method or not.
Jimmacle
Jimmacle10mo ago
that's why you choose a method name that makes that obvious
i like chatgpt
i like chatgptOP10mo ago
C# should prevent us from doing the following.
static void Mutate_A(User user)
{
user = new User();
}
static void Mutate_A(User user)
{
user = new User();
}
Even though it is meaningless.
Jimmacle
Jimmacle10mo ago
or just don't do it
i like chatgpt
i like chatgptOP10mo ago
public sealed class UserIn
{
public string Name { get; init; } = null!;

public static implicit operator User(UserIn @in) =>
new User
{
Name = @in.Name
};

public static User operator >>>(UserIn @in, User user)
{
user.Name = @in.Name;
return user;
}

// wrappers
public User MapTo(User user) => this >>> user;
public User ToUser() => this;
}
public sealed class UserIn
{
public string Name { get; init; } = null!;

public static implicit operator User(UserIn @in) =>
new User
{
Name = @in.Name
};

public static User operator >>>(UserIn @in, User user)
{
user.Name = @in.Name;
return user;
}

// wrappers
public User MapTo(User user) => this >>> user;
public User ToUser() => this;
}
Stroniax
Stroniax10mo ago
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:
public record User(int Id, string Name);
public void Logon(User? user) {
if (user is null) {
user = CurrentUser;
}
DoSomething(user);
}
public void ImpersonateUser(User? user, Action work) {
var token = Logon(user);
// if user was null earlier, it's still null here
DoSomethingElse(user);
}
public record User(int Id, string Name);
public void Logon(User? user) {
if (user is null) {
user = CurrentUser;
}
DoSomething(user);
}
public void ImpersonateUser(User? user, Action work) {
var token = Logon(user);
// if user was null earlier, it's still null here
DoSomethingElse(user);
}
i like chatgpt
i like chatgptOP10mo ago
Thank you all.
Want results from more Discord servers?
Add your server