C
C#3mo ago
Cubic

How to make an expression as short as possible

To any min maxxers out there, i am trying to shrink these few lines to inhuman extremes for.. no reason really. GetEffectsOfType(uint type, int baseValue) returns the total value of effects of type "type" or null if it finds none. I want "value" to remain null if there were no effects found in the entire foreach, otherwise it has to have the total value. This code works, i was just thinking if there was a way to go smaller, maybe making more use of the null coalition operator ??.
No description
27 Replies
x0rld
x0rld3mo ago
why value is nullable ? that force you to use the null collasing operator
Cubic
Cubic3mo ago
i want to distinguish between "no effects were found" and "there were effects, but with a value of 0"
x0rld
x0rld3mo ago
you can probably do it using aggregate ? 🤔 if you know how it works
Cubic
Cubic3mo ago
so uhh, fun fact, i do not 👍 i kinda just looked over the microsoft csharp tutorials a little and combined my highschool knowledge of c++to make this work
leowest
leowest3mo ago
can't u just do
value += effect.GetEffectsOfType(effectType, baseValue) ?? 0;
value += effect.GetEffectsOfType(effectType, baseValue) ?? 0;
and remove the if and value ??= 0 all together
Cubic
Cubic3mo ago
if value starts as null then it won't do anything
leowest
leowest3mo ago
it will be just 0
Cubic
Cubic3mo ago
null + 0 is null
leowest
leowest3mo ago
no where do u declare value ah I see
Cubic
Cubic3mo ago
up at the top int? value = null;
leowest
leowest3mo ago
just set value to 0 instead of null you're doing that inside is 0 an effect?
Cubic
Cubic3mo ago
again, i want to distinguish between "i found effects with a value of 0" and "nothing was found"
leowest
leowest3mo ago
ah ok there is an effect with 0
Cubic
Cubic3mo ago
yeah
Joschi
Joschi3mo ago
You shouldn't use a nullable primitive to represent a special state in the first place. Use an enum or wrap it some other way. But null having a special meaning, beyond it just being missing, is not good practice.
Cubic
Cubic3mo ago
well, doesn't it mean missing here? there were no effects, so there's no value to get from them. but i get what you mean
leowest
leowest3mo ago
he means in the sense u could have instead -1 representing non-existing as oppose to null for example
Cubic
Cubic3mo ago
well i can have values of -1 so no can do
leowest
leowest3mo ago
that was just an example but that wouldn't change the situation either
Cubic
Cubic3mo ago
yeah, but i still think null works best here i'm just gonna keep using this then it's not broken nor that inefficient, really
Joschi
Joschi3mo ago
You can also use the Try Pattern with an out parameter instead of calling the (possibly maybe expensive, who knows) method twice.
x0rld
x0rld3mo ago
I really like the try pattern
Cubic
Cubic3mo ago
isn't try used for exceptions? how would i implement it here?
Joschi
Joschi3mo ago
No TryParse not a trx block.
Cubic
Cubic3mo ago
also, here's the method, i'll let you judge how expensive it is [Effects has no more than 3 or 4 members and usually it's just one]
No description
Cubic
Cubic3mo ago
i'm gonna have to look into that lol
Joschi
Joschi3mo ago
Just take a look at the implementation of int.TryParse
Want results from more Discord servers?
Add your server
More Posts