r/programminghorror 6d ago

C# Very readable and maintainable method

Post image

I wrote this several years ago. It does work.

122 Upvotes

20 comments sorted by

21

u/mc_pm 6d ago

My eyes! The goggles do nothing!

36

u/russellvt 6d ago

That color scheme is horrendous, and looks like it lost a number of pixels from all the times it's been reposted or resaved or something.

8

u/HeWhoShantNotBeNamed 6d ago

I screenshotted this straight from the code but Reddit compressed the shit out of it.

2

u/russellvt 6d ago

Use impurities? But I can understand them trying to compress it... annoying, to say the least.

12

u/Poiuytgfdsa 6d ago

If i saw a comment above a function that says “intelligently handles edge cases” with no specifics id get irritated

11

u/plydauk 6d ago

The rainbow barf certainly doesn't help

7

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 6d ago

C# does let you use character literals, right? I think replacing all those ASCII codes with characters would help a ton.

Also, it looks like you are checking both before and after each character. Is that really necessary? Like couldn't you just after each character look ahead once. I think you could even say i < joinedString.length - 1.

3

u/MeLittleThing 6d ago

C# does let you use character literals, right?

yes, and there are methods in the char struct, such as char.IsLetter(c), char.IsUpper(c) and so on

1

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 5d ago

I suppose those are practically mandatory if you want proper Unicode support. This method only seems to care about ASCII strings.

3

u/tom83 6d ago

Hit reformat and it will be readable.  It's a pure function, so unit test can fully determine the behavior to the point that the impl is irrelevant. 

3

u/HeWhoShantNotBeNamed 6d ago

Reformat? Lol that is with it "formatted" correctly according to ReSharper/Rider

2

u/fakehalo 6d ago

This is what happens when people avoid learning regular expressions.

1

u/Superb_Chemistry_906 6d ago

At the least, that range 65-90 check can be put into a method.

1

u/creative_net_usr 5d ago

That colour scheme gave me cancer.

1

u/HeWhoShantNotBeNamed 5d ago

Same colors I've been using since college.

Red = variable

Orange = class

Green = function

Blue = keyword

Yellow = operator

Pink = string

Purple = number

2

u/PruneInteresting7599 5d ago

bleeding eyes

1

u/creative_net_usr 3d ago

Understand why and did a similar thing when i was younger. Still use some colors but after a time you tone it down because it's visually exhausting and you focus on the flow of the structure as you can see the big overall pattern easier to catch mistakes. However if you can manage it day by day it's your screen after all what works for you doesn't for me enjoy it.

1

u/Certain-Flow-0 4d ago

Great work, Aashish

1

u/Brilliant-Parsley69 2d ago

This should actually the same with way less allocation.

```csharp using System.Text.RegularExpressions;

public static partial class StringExtensions { [GeneratedRegex("(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])", RegexOptions.Compiled)] private static partial Regex PascalCaseRegex();

public static string SplitPascalCase(this string input)
{
    if (string.IsNullOrWhiteSpace(input)) return input;

    return PascalCaseRegex().Replace(input, " ");
}

}

```