C
C#9mo 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
x0rld9mo ago
why value is nullable ? that force you to use the null collasing operator
Cubic
CubicOP9mo ago
i want to distinguish between "no effects were found" and "there were effects, but with a value of 0"
x0rld
x0rld9mo ago
you can probably do it using aggregate ? 🤔 if you know how it works
Cubic
CubicOP9mo 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
leowest9mo 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
CubicOP9mo ago
if value starts as null then it won't do anything
leowest
leowest9mo ago
it will be just 0
Cubic
CubicOP9mo ago
null + 0 is null
leowest
leowest9mo ago
no where do u declare value ah I see
Cubic
CubicOP9mo ago
up at the top int? value = null;
leowest
leowest9mo ago
just set value to 0 instead of null you're doing that inside is 0 an effect?
Cubic
CubicOP9mo ago
again, i want to distinguish between "i found effects with a value of 0" and "nothing was found"
leowest
leowest9mo ago
ah ok there is an effect with 0
Cubic
CubicOP9mo ago
yeah
Joschi
Joschi9mo 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
CubicOP9mo 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
leowest9mo ago
he means in the sense u could have instead -1 representing non-existing as oppose to null for example
Cubic
CubicOP9mo ago
well i can have values of -1 so no can do
leowest
leowest9mo ago
that was just an example but that wouldn't change the situation either
Cubic
CubicOP9mo 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
Joschi9mo ago
You can also use the Try Pattern with an out parameter instead of calling the (possibly maybe expensive, who knows) method twice.
x0rld
x0rld9mo ago
I really like the try pattern
Cubic
CubicOP9mo ago
isn't try used for exceptions? how would i implement it here?
Joschi
Joschi9mo ago
No TryParse not a trx block.
Cubic
CubicOP9mo 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
CubicOP9mo ago
i'm gonna have to look into that lol
Joschi
Joschi9mo ago
Just take a look at the implementation of int.TryParse
Want results from more Discord servers?
Add your server