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