I found a function in c# like this:
private Dictionary<string, Func<string>> ObtenerExtraCfgCampo(MsgDefCamp camp)
{
var extra = new Dictionary<string, Func<string>>();
extra.Add("Oddity", () => camp.Order % 2 == 0 ? "Even" : "Odd");
extra.Add("Odd", () => camp.Order % 2 == 0 ? "Odd" : string.Empty);
extra.Add("Even", () => camp.Order % 2 != 0 ? "Even" : string.Empty);
extra.Add("MsgPath", () => campo.MsgPath);
return extra;
}
Is there any utility on this? wouldn't it be better like this
private Dictionary<string, string> ObtenerExtraCfgCampo(MsgDefCamp camp)
{
var extra = new Dictionary<string, string>();
extra.Add("Oddity", camp.Order % 2 == 0 ? "Even" : "Odd");
extra.Add("Odd", camp.Order % 2 == 0 ? "Odd" : string.Empty);
extra.Add("Even", camp.Order % 2 != 0 ? "Even" : string.Empty);
extra.Add("MsgPath", campo.MsgPath);
return extra;
}
I undestand that changing camp would change the behavior of the functions. But isn't it too riski and difficult to reason about? I wonder if I'm missing the point of this
Func<T>
as a function that returns a T, instead mentally think of it asComputedOnDemand<T>
. A string is a string. AComputedOnDemand<string>
is a thing that can compute a string when asked. Those are different!