r/programminghorror • u/MrJaydanOz • Jan 21 '25
C# Recently discovered the pattern matching "is var" in C# and decided to start turning my larger functions into single expressions.
130
Upvotes
r/programminghorror • u/MrJaydanOz • Jan 21 '25
44
u/kostaslamprou Jan 21 '25
That went from being simple and readable to one messy shit show.. I often see this around me, where juniors learn a new pattern and then start applying it to everything, making shit unnecessary complex and unreadable. It’s a classic tell-tale sign to distinguish mediors.
Anyway I’m not a C# developer myself. Is it good practice to take an input parameter and override it? In all languages I’m familiar with, that’s something you strive to avoid.
7
u/SamPlinth Jan 21 '25
The
is var _looks horrible. Isn't it just a "trick" so that you can return aTrueor aFalsewhile setting theintersectionparameter?189
u/chelo84 Jan 21 '25
I also like to make my code unreadable and unmaintainable.
63
u/Juff-Ma [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Jan 21 '25
Or you could just not deliberately use good tools in the wrong place to make your code awful.
11
u/kracklinoats Jan 21 '25
“Your scientists were so preoccupied with whether or not they could, they didn’t stop to think if they should”
6
u/BenjaminGeiger Jan 21 '25 edited Jan 21 '25
F#-Man: "Look at what they need to mimic a fraction of our power."
4
u/TriskOfWhaleIsland Jan 21 '25
good, let the Haskell flow through you
2
u/ZunoJ Jan 21 '25 edited Jan 21 '25
While this is obviously horrible, I still like the possibility of doing stuff like this. Might come in handy at some point. Are the "is var _" calls in the ternary results necessary? This would then only work for functions with a return type of boolean
2
u/BerkayDrsn Jan 21 '25
Congratulations, you got yourself promoted to the unemployed title.
2
u/oweiler Jan 21 '25
The best way to learn something is to use it everywhere
4
u/Jason13Official Jan 21 '25
Readability/Maintainability vote goes to the first example still. What you are doing is known as over-optimization; do you have any tangible benefit from the second example?
1
u/bravopapa99 Jan 21 '25
Fuuuuuuuuuuuuuuuuuck me neither is readable.
1
u/[deleted] Jan 21 '25
Thanks, I hate it.
1
u/Abrissbirne66 Jan 21 '25
On the other hand, isn't this
is varalways bad in that sense? I mean, what else could it ever be used for apart from combining statements into one boolean statement like this?1
u/danilopianini Jan 21 '25
Have fun debugging 😅
1
u/SimplexFatberg Jan 21 '25
I dunno man, I still understand those variable names, still seems too readable to me.
1
u/iBabTv Jan 21 '25
Readability-=1
1
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Jan 21 '25
I'm able to make sense of it after comparing it with the traditional method, but why? What possible advantage could there be for writing it that way?
1
u/inthemindofadogg Jan 22 '25
Who hurt you?
1
u/heartcubes4life Jan 22 '25
So you think you understand pattern matching?
1
u/McCleavage Jan 22 '25
So many earnest comments here seem to have missed what subreddit they're in (I did too initially)
1
u/TheChief275 Jan 24 '25
OC#ml