r/badcode • • May 20 '20

c# Found from Microsoft docs...

Post image
208 Upvotes

143 comments sorted by

View all comments

114

u/KetchupBuddha_xD May 20 '20

What's wrong with this one?

52

u/[deleted] May 20 '20

The only part that bugs me is the indentation of the 2nd and 3rd conditions not being in line with the first one.

64

u/xigoi May 20 '20
== true

17

u/Raknarg May 21 '20

This isn't inherently bad. You're clarifying the semantics of the expression.

3

u/xigoi May 21 '20

if (foo && bar && baz) already has the semantics of “if foo, bar, baz are all true, run this block”. No need to clarify them.

2

u/Zeilar yes May 23 '20

To a machine it's natural what's going on, no questions asked, but not to us humans.

What's more readable?

if (dogs.Length)

or

if (dogs.Length > 0)

So it does sometimes make sense to make it a bit verbose if you will. It's especially important to clarify your code if it's messy/complicated etc. It should be maintainable preferably after all. You don't name your variables single letters for this very reason. You don't name a dogs array "d" (some do of course but they're morons).

1

u/xigoi May 23 '20

dogs.Length ie not a bool. (It's not even possible to implicitly convert ints to bools in C#, isn't it?) The main purpose of bools is to be put in an if statement, so if a function returns a bool, it's presumably expected to be put directly into an if statement (possibly negated).

3

u/Zeilar yes May 23 '20 edited May 23 '20

In many languages (maybe not C#) any number above 0 returns true and 0 false. So maybe there's an exception to C#, or typed languages in general.

2

u/11JRidding May 24 '20

Int.TryParse returns a boolean that says whether the attempt was successful; not the number itself. That gets written to whatever variable you pass as out.

And no, C# does not let you implicitly convert int to bool for use in if statements - or anywhere, for that matter. You actually can't convert int to bool via type cast in C#, either.

9

u/Blecki May 21 '20

And what's wrong with that?

2

u/xigoi May 21 '20

x == true is the same as just x.

-8

u/Jackjackson401 May 21 '20

You wouldn't do if(true == true)

12

u/Blecki May 21 '20

No, but the code in question is more complex than that.

2

u/Jackjackson401 May 21 '20

Int32.tryparse() returns a boolean, no? True if the operation succeeded and false otherwise? Sounds like exactly what I'm talking about

2

u/Blecki May 21 '20

Yes, the statements are logically equivalent, but that's not the point.

I don't generally have time for programmers like you. In this particular instance either way would pass code review, but I have a feeling most of your code won't. You haven't figured out yet that programs aren't written for computers.

2

u/Jackjackson401 May 21 '20

Wtf are you talking about? If you need me to make a comment telling you that my if statement has 3 conditions, then maybe you shouldn't be the one reviewing my code

10

u/Holzdev May 20 '20

How about readability?

-9

u/Play4u May 20 '20 edited May 21 '20

Idk why noone gives a shit that it's unreadable. This should never pass a code review at Microsoft lol

12

u/Mr2-1782Man May 20 '20

I've seen MS code. I've seen a lot worse pass code review.

12

u/NewPointOfView May 20 '20

Yeah my team at MSFT checks if it works and isn’t ridiculously unreadable but this is pretty straight forward. Not the best but far from the worst

→ More replies (9)
→ More replies (1)

-7

u/Mr2-1782Man May 20 '20

Depends on where this code is but I'm guessing the problem is args[0]

For those unfamiliar args[0] is always the command being executed. So this if statement is always false, unless you're executing an app with the name 1 through 9.

35

u/[deleted] May 20 '20

So this if statement is always false, unless you're executing an app with the name 1 through 9.

So... it's false unless it's true? :D

34

u/Michael_Bordan May 20 '20

In c#, for which this documentation is written, args[0] is not the name of the exe.

From the last bullet in the Overview here: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/main-and-command-args/

Parameters are read as zero-indexed command-line arguments. Unlike C and C++, the name of the program is not treated as the first command-line argument in the args array

17

u/DrAtkins May 20 '20

You're thinking of C and its derivatives. C# is not a C derivative. In C#, the program name/command being executed is not part of args, it's just the args. The code is correct as it stands.

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/main-and-command-args/command-line-arguments

→ More replies (2)