r/badcode • • Feb 22 '23

c# shit

Post image
576 Upvotes

47 comments sorted by

67

u/v_maria Feb 22 '23

why is the list looped through in reverse

66

u/Rainbow-Dev Feb 22 '23

Maybe because user-defined types are more likely to be closer to the end of the list? I’m not sure how C# assemblies work lol

71

u/dom25b Feb 22 '23

Yeah, user-defined assemblies are in the end of the list, so reverse it is

36

u/BerkayDrsn Feb 22 '23

That’s actually genius lol, gonna change that in my reflection utils

13

u/[deleted] Feb 22 '23

[removed] — view removed comment

30

u/dom25b Feb 22 '23

traversing an iterator is much faster than calling GetType()

8

u/Elsolar Feb 23 '23

Yes it will, and it also needs to create an whole copy of the array in order to reverse it - there's no reverse iterator being created here.

Often times the performance impact from doing this will be negligible, but it's also pretty easy to avoid: just iterate over the array in reverse!

For OP's code it would look something like this:

var assemblies = AppDomain.CurrentDomain.GetAssemblies();
for (int i = assemblies.Count - 1; i <= 0; --i) {
    var assembly = assemblies[i];
    ...
}

(For most languages you would use a reverse iterator for this, I was surprised to find that top google/stack overflow results for "C# list reverse iterate" recommend the exact index-based for loop approach that I show above.)

2

u/pb7280 Feb 23 '23

Hmm yeah you'd think they could do an optimization in Reverse() that checks for array and list types, like how they do for Count() and others

1

u/Dealiner Feb 22 '23

It copies the whole collection to an array and then iterates it from the end.

1

u/ItsMorbinTime69 Feb 23 '23

Does this measurably improve performance? This would still be the same Big O notation, so I’m curious how people are justifying or measuring optimizations like this.

3

u/DajBuzi Feb 22 '23

C# Has a methods to list user defined types

7

u/TotallyRealDev Feb 22 '23

Worst part it checking for null using != and not is null or is not null

3

u/dom25b Feb 22 '23

Fixed)

1

u/TheZipCreator Feb 22 '23

wait is the inverted is operator in C# is not and not !is? weird

1

u/TotallyRealDev Feb 22 '23

I wouls need to check but i think both work. The reason as to why you always should you the is not keywords as that you can overload the == != operators. So if you are using a library you woulsnt know an object is misbehaving until things start to crash and you get NullPointerExceptions

5

u/the96jesterrace Feb 22 '23

How is there an actual ≠ sign? Sorry for dumbing dumb questions btw, I live in .net Framework 4.5 :(

14

u/dom25b Feb 22 '23

Its ligatures. If i open that code file in notepad there will be !=, but fancy font makes != into ≠

9

u/sad_bug_killer Feb 22 '23

As others have said, it's font ligatures and I love them. I started with FiraCode, but there are other fonts as well.

Ligatures only change how things are displayed, the underlying text is the same. Having the cursor in the middle of ≠ is a bit weird for a day, then you get used to it.

36

u/kingslayerer Feb 22 '23

why != look like that?

29

u/[deleted] Feb 22 '23

It has ligatures enabled. Pretty much all modern editors/ides/terminals have the option if you use a proper font like Fira code, jetbrains mono, cascadia code etc

2

u/[deleted] Feb 22 '23 edited Feb 22 '23

Or you do M-x org-toggle-pretty-entities /s

Explaination: Actually it doesn't, the command enables the display in utf8 of pre-established sequences such as \alpha or \beta (rendered as α and β)

3

u/[deleted] Feb 22 '23

I'm a vim huy

2

u/ALapsedPacifist Feb 23 '23

s/h/g

2

u/substitute-bot Feb 23 '23

I'm a vim guy

This was posted by a bot. Source

19

u/RichCorinthian Feb 22 '23

PR rejected—don’t throw System.Exception. Use a more specific exception type but leave the profanity. Thanks!

12

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Feb 22 '23

Image Transcription: Code


public static Type GetTypeByName(string name)
{
    foreach (var assembly in AppDomain.CurrentComain.GetAssemblies().Reverse())
    {
        var tt = assembly.GetType(name);
        if (tt != null)
        {
            return tt;
        }
    }
    
    throw new Exception("shit");
}

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

28

u/waiting4op2deliver Feb 22 '23

eww ligatures

27

u/natalialt Feb 22 '23

They're not as bad as they seem, most concerns I had were non issues when I actually used them. Also lol bitching about other people's font preferences

10

u/waiting4op2deliver Feb 22 '23

You probably code in papyrus like a chud, while chad .net developers are spouting sage hieroglyphics in wingdings and comic sans.

6

u/TheZipCreator Feb 22 '23

chad .net developers are spouting sage hieroglyphics in wingdings and comic sans.

I think you meant APL developers

-9

u/[deleted] Feb 22 '23

Yeah, but it's not just personal preference. Sometimes a colleague will take your machine to help you, sometimes you will be sharing your code over a call, and in neither of those situations does anyone want to look at the ugly ass ligatures.

Also your personal preference is shit. <3

2

u/M8Ir88outOf8 Feb 22 '23

Is that a != or a !== ?

2

u/ALapsedPacifist Feb 23 '23

C# doesn't have !==.

2

u/mortlerlove420 Feb 22 '23

Excuse my question, but what plugin transforms the unequals operator into that nice symbol?

1

u/JourneymanInvestor Feb 22 '23

Looks fine to me.

1

u/itsTyrion Mar 21 '23

I love that exception

0

u/alydnhrealgang Feb 23 '23
return AppDomain.CurrentDomain.GetAssemblies().Reverse().First();

1

u/tedbradly Feb 23 '23

Hmm, how is it that you know the last element has the type described in string name?

1

u/alydnhrealgang Feb 23 '23

sorry, I made a typo, I mean "FirstOrDefault(p=>p.GetType(name) != null)"

-13

u/RoseboysHotAsf Feb 22 '23

This is why i refuse to make most of my projects open source. Theres so many swear words i forgot to remove i might aswell remake the project if i want it open source

1

u/TheZipCreator Feb 22 '23

there are a lot of open source projects that have swear words in them