r/badcode • • Apr 25 '21

c# true&&&true

Post image
727 Upvotes

27 comments sorted by

109

u/00PT Apr 26 '21

The beginning is actually interesting - I like how they use string.empty instead of "" and boolean.truestring instead of "true". Last part is bad, though.

49

u/trevster344 depraved Apr 26 '21

If I recall correctly string.empty is the recommended approach. Never seen the Boolean bit and that last part hurts my soul.

19

u/Svizel_pritula Apr 26 '21

Why is string.empty better than ""? Does it save a memory allocation?

20

u/gnutrino Apr 26 '21

It did once upon a time, there's no difference now (since NET 2.0 iirc). The only other difference is that "" can be used in places that need a compile time constant (default parameters, attribute arguments) where String.Empty would be a compile time error.

3

u/barking_dead Apr 26 '21

Snakeoil optimization maybe?

However, the docs say:

The value of this field is the zero-length string, "".

8

u/Golden_Flame0 Apr 26 '21

string.empty is also a little bit easier to read, depending on who you ask.

16

u/Svizel_pritula Apr 26 '21

Is it? "" gets colored as a literal and you immediately see it's empty, while string.empty is an identifier and you might this for a second it comes from some variable somewhere.

10

u/Svizel_pritula Apr 26 '21

That got me thinking, wouldn't it be cool if an IDE would display the values of constants text to their references, like it's often done with colours in css?

3

u/Nielsly Apr 26 '21

I think pycharm does that in debugging mode, or at least shows the value in the debug menu, should be possible to create a plug-in that does what you said if it doesn’t already

1

u/00PT Apr 26 '21

I just find it an interesting alternative that could potentially more clear in certain situations. It might not be better, but I still like it's use.

97

u/stogle1 Apr 26 '21

Junior Dev: How do I test if two booleans are true?

Senior Dev: You just && them together

Junior Dev: Coming right up!

44

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Apr 25 '21

Image Transcription: Code


public ActionResult EditGroup(Group group)
{
        string fleetSuccess = string.Empty;
        bool success = false;
        
        if(action != null)
        {
                fleetSuccess = updateGroup(group);
        }
        else
        {
                fleetSuccess = Boolean.TrueString;
        }
        success = updateExternalGroup(group);
        
        fleetSuccess += "&&&" + success;
        if (fleetSuccess.ToLower().Equals("true&&&true"))
        {
                GetActivityDataFromService(group, false);
        }
        
        return Json(fleetSuccess, JsonRequestBehavior.AllowGet);
}

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

19

u/ekolis Apr 26 '21

Good human!

28

u/Derangedteddy Apr 26 '21

And I thought the ASP.NET apps I inherited were bad... Holy shit.

16

u/ekolis Apr 26 '21

Less verbose than XML.

4

u/Hax_Meadroom Apr 26 '21

Psh.. these are implementation details

4

u/muzzbuzz789 Apr 26 '21

Can someone explain how this would even compile with the 'action' variable not having been defined?

25

u/JJBA_Reference Apr 26 '21

This is a method within some class. action is probably a property of the same class.

5

u/muzzbuzz789 Apr 26 '21

Ah yes, of course lol. I've clearly gotten too use to broad colour coding.

2

u/lizard450 Apr 26 '21

Probably a field of the class. Maybe a property considering the code.

5

u/britreddit Apr 26 '21

Looks like some kind of API, ActionResult it typically used by ASP.NET to bundle replies to requests

2

u/decimaster321 Apr 26 '21

The worst thing about this example is an http-GET triggering a server side update.

1

u/spongebue Apr 26 '21

I one had a coworker write this in his code

if (true == bool1 == bool2)

1

u/KCGD_r Apr 26 '21

idk what's worse, the "true&&true" or group and Group being different variables

3

u/00PT Apr 26 '21 edited Apr 26 '21

I'm pretty sure that's supposed to be an instance of the "group" class. It's fairly common to name your variable the same as the class (or at least something resembling the class) if you only have one instance (you can tell it's a variable by the casing).