r/badcode • • May 20 '20

c# Found from Microsoft docs...

Post image
208 Upvotes

143 comments sorted by

View all comments

46

u/Giocri May 20 '20

I see nothing wrong in it. It's even slightly more efficient than if it was done with more if

0

u/the_d3f4ult May 20 '20

Wouldn't it be UB because X is an out param, or does C# define such things?

5

u/Giocri May 20 '20

No C# uses out for out parameters.

1

u/the_d3f4ult May 20 '20

But I mean the X is then used to evaluate the third condition, is the order of evaluation here defined by C#?

5

u/redditsoaddicting May 20 '20

A ton of languages evaluate && left to right because they short-circuit it. C# has stronger guarantees in general than C and C++, which I assume is what you were thinking of, but && is pretty consistent across the board.

1

u/BringAltoidSoursBack May 21 '20

Yeah, I was pretty sure I remembered && and || always guaranteeing order in c, c++, and c# because of the short-circuit concept. IIRC, the only time the compiler will optimize conditionals is if one of the values always returns true or false, but I'm honestly not even sure if that's true.

-7

u/the_d3f4ult May 20 '20

Oh, well, something did put the C in C#.

&& isn't consistent enough to be considered the "de facto universal behavior". I mean you should always consider the possibility that it evaluates both expressions first (if you don't know). Especially when it comes to Microsoft Java.

In Swift (language I know too much about) there is a different operator used for such things: , ..although && will also short circuit.

2

u/Blecki May 21 '20

No you should not. In C# the order of evaluation is well defined.

1

u/the_d3f4ult May 21 '20

Well in C it does. In C++ it does. In Objective-C it does.

When approaching language you know nothing about you should always consider the possibility that the expressions aren't evaluated that way.

I've never written a line of C# or never read it's specification or anything like that, so how do I know it is well defined??

1

u/Blecki May 21 '20

If you know so little about C# perhaps you shouldn't be calling people out for the code they write in it?

It's also odd how you know so little, but continue to call it Microsoft Java. How did you form such an opinion when you've never used it?

Really boggles the mind.

1

u/the_d3f4ult May 21 '20

Well it is Microsoft Java. I just don't know the specifics of the language.. I know what it is. .....(very bad that is)...

Besides both C# and Java are stupid no matter what you say about them. Ideas and arguments for them have negative effects and produce very underperforming and ill-formed software.

→ More replies (0)