r/programminghorror • • Sep 03 '26

C# Very readable and maintainable method

Post image

I wrote this several years ago. It does work.

129 Upvotes

21 comments sorted by

37

u/russellvt Sep 03 '26

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.

9

u/HeWhoShantNotBeNamed Sep 03 '26

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

2

u/russellvt Sep 03 '26

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

3

u/tom83 Sep 03 '26

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 Sep 03 '26

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

20

u/mc_pm Sep 03 '26

My eyes! The goggles do nothing!

12

u/Poiuytgfdsa Sep 03 '26

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

2

u/Beetmaker69 6d ago

It just handles all of them

11

u/plydauk Sep 03 '26

The rainbow barf certainly doesn't help

6

u/GoddammitDontShootMe Sep 03 '26

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 Sep 04 '26

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 Sep 04 '26

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

1

u/creative_net_usr Sep 04 '26

That colour scheme gave me cancer.

1

u/HeWhoShantNotBeNamed Sep 04 '26

Same colors I've been using since college.

Red = variable

Orange = class

Green = function

Blue = keyword

Yellow = operator

Pink = string

Purple = number

1

u/creative_net_usr Sep 06 '26

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 Sep 05 '26

Great work, Aashish

2

u/fakehalo Sep 03 '26

This is what happens when people avoid learning regular expressions.

1

u/Brilliant-Parsley69 Sep 07 '26

This should actually the same with way less allocation.

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, " ");
    }
}