r/shittyprogramming • • Jun 22 '16

You can totally write unstructured code in C#

Post image
263 Upvotes

38 comments sorted by

2

u/Terreurhaas Jun 22 '16
using System;

class Program {

    static void Main() {
        int b = 0;
        string t = "";
        for(b = 99; b > 0; b--) {
            t = b.ToString() + " BOTTLE";
            if(b > 1) {
                t = t + "S"+;
            }
            t = t + " OF BEER ON THE WALL";
            Console.Write(t + ", " + t);
            Console.Write("\nTAKE ONE DOWN AND PASS IT AROUND, ");
            if(b - 1 <= 0) {
                Console.Write("NO MORE BOTTLES OF BEER ON THE WALL");
            }
            Console.WriteLine();
        ;}
    }

}  

Am I not following the goto's correctly? I'm missing labels 90 and 95...

3

u/*polhold01242 Jun 22 '16

No, I think that's right -- you can see 90 and 95 have no goto's pointing to them, as they have the squigglies.

2

u/Terreurhaas Jun 22 '16

Also, what's with the ;}? wouldn't } be enough? - I never programmed in C#, so there's that...

2

u/Lystrodom Jun 22 '16

I believe a label has to point to a statement.

3

u/Terreurhaas Jun 22 '16

A dangling ; is valid? In how many languages is that? I might have learned something today...

4

u/paranoid_twitch Jun 22 '16

It is in fact valid but it gets optimized away by the compiler.

static void Main(string[] args)
{
    Console.WriteLine("Hello World");
    ;
}

.method private hidebysig static 
	void Main (
		string[] args
	) cil managed 
{
	// Method begins at RVA 0x2050
	// Code size 14 (0xe)
	.maxstack 8
	.entrypoint

	IL_0000: nop
	IL_0001: ldstr "Hello World"
	IL_0006: call void [mscorlib]System.Console::WriteLine(string)
	IL_000b: nop
	IL_000c: nop
	IL_000d: ret
}

Versus

static void Main(string[] args)
{
    Console.WriteLine("Hello World");
}

.method private hidebysig static 
	void Main (
		string[] args
	) cil managed 
{
	// Method begins at RVA 0x2050
	// Code size 14 (0xe)
	.maxstack 8
	.entrypoint

	IL_0000: nop
	IL_0001: ldstr "Hello World"
	IL_0006: call void [mscorlib]System.Console::WriteLine(string)
	IL_000b: nop
	IL_000c: nop
	IL_000d: ret
} 

3

u/tangerinelion Jun 23 '16

Lots of C based languages allow this. It's just an empty statement, similar to pass in Python.

For example, you can easily do this by mistake:

int x = 5;;

This is technically two statements: int x = 5; and ; but we should expect ; to compile into nothing and that's exactly what /u/paranoid_twitch shows.

I had a colleague, working on a totally separate codebase, who insisted on using }; for their closing braces throughout their C++ library. In fairness... it's OK in a lot of places and makes class { }; seem not-so-unusual. So it would always be namespace { }; and if(...) { };. There'd be for(...) { }; and while(...) { };. The only time they were careful was with if(...) { } else if(...) { } since that ends up breaking the entire chain and you have a starting else clause to raise errors. Still... they could write if(...) { } else if(...) { } else { }; and then it's like the ; is terminating the entire if/else if/else block. Oh and of course int main() { };.

58

u/[deleted] Jun 22 '16

Oh my god. The language gives you so much potential for writing beautiful and well structured code and instead they made this. Aghh. The passionate programmer in me is ripping his hair out.

19

u/Conpen Jun 22 '16

Your passionate programmer must be bald given that you visit this sub.

2

u/[deleted] Jun 22 '16

Most of the time he just laughs, this was infuriating though.

34

u/thelehmanlip Jun 22 '16

Why I spent so much time copying this down is beyond me, but in case anyone else wants to run it:

_01:  int b = 0;
_05:  string t = "";
_10:  for(b = 99; b > 0; b--){
_20:  goto _100;
_30:  t = t + " OF BEER ON THE WALL";
_40:  Console.Write(t + ", " + t);
_50:  Console.Write("\nTAKE ON DOWN AND PASS IT AROUND, ");
_60:  if(b - 1 <= 0) { Console.Write("NO MORE BOTTLES OF BEER ON THE WALL"); } goto _80;
_70:  goto _200;
_75:  Console.Write(" OF BEER ON THE WALL");
_80:  Console.WriteLine();
_85:  goto _230;
_90:  Console.Write("GO TO THE STORE AND BUY SOME MORE, 99 BOTTLES OF BEER ON THE WALL");
_95:  return;
_100: t = b.ToString() + " BOTTLE"; if (b > 1) { t = t + "S"; }
_110: goto _30;
_200: Console.Write((b - 1).ToString() + " BOTTLE");
_210: if (b - 1 > 1) { Console.Write("S"); }
_220: goto _75;
_230: ;}

22

u/TheTjalian Jun 22 '16 edited Jun 22 '16

Oh my god. I compiled it and it actually works. You'll need to chuck something at the end so the window doesn't close (Console.ReadLine()) worked well enough for me), but it works. I'm a little disgusted it works, but yeah. Thanks for the transcript!

EDIT:

_240: string rl = Console.ReadLine();
_250: if (rl == "more beer") { goto _10; }

because in for a penny, in for a pound is my motto

2

u/thelehmanlip Jun 22 '16

Yeah, I had run it in linqpad so I didn't include the boilerplate stuff

14

u/YMK1234 Jun 22 '16

Doesn't line _60 always jump to line _80 no matter what? I'd imagine it should only jump in the if.

11

u/vegetablebread Jun 22 '16

That's correct. Labels _70, _75, _90, _95, _200, _210 and _220 are unreachable.

9

u/z500 Jun 22 '16

If only we had preprocessor macros so we could make all those keywords uppercase.

8

u/Lakonislate Jun 22 '16
_30:  t = t + " OF BEER";
_40:  Console.Write(t + " ON THE WALL, " + t);

Isn't that how the song goes?

7

u/wOlfLisK Jun 23 '16

I'm imagining a tutor said "as long as it works, you get full marks" and you decided to make his life hell.

5

u/tgp1994 Jun 22 '16

That actually compiles?

13

u/mnbvas Jun 22 '16

With a bunch of warnings.

4

u/mushr00m_man Jun 22 '16

This is too easy. Get rid of all the nested statements and use gotos for everything.

4

u/pier25 Jun 22 '16

10 CLS

3

u/TheTjalian Jun 22 '16

I just, just, why? Why is this even possible? Some coders just want to watch the world burn.

2

u/luckystarr Jun 22 '16 edited Jun 22 '16

This is beautiful! I have fond memories of learning the concept of a loop in a language like this (GW-BASIC). A long time ago of course.

2

u/Lystrodom Jun 22 '16

You got the lyrics wrong! It's "x bottles of beer on the wall, x bottles of beer! Take one down, pass it around, x - 1 bottles of beer on the wall!"

2

u/skulgnome Jun 23 '16

No variable$ syntax? We cannot have this.

2

u/the_king_of_sweden Jun 23 '16

This perfectly illustrates why we need a GOSUB statement in C#

1

u/ekolis Jul 09 '16

We do have a gosub statement, it's just implicit!

Console.WriteLine("im in ur sub runnin ur codez"); // implicit gosub before Console.WriteLine

1

u/cosmicr Jun 23 '16

Serious question - why does C# have a goto statement?

1

u/skulgnome Jun 23 '16

For jumping into a sibling block. Less correctly, for being lazy about transaction restarts and jumping to start-of-transaction instead of using a do-while.

1

u/acylus0 Jun 23 '16

I mean... I understand where there may be extreme edge cases (highly unlikely) to use goto but why?

1

u/ekolis Jul 09 '16

You forgot the UnstructuredCodeGeneratorFactoryFactoryConfig.

0

u/[deleted] Jun 22 '16

[deleted]

4

u/soundman10000 Jun 22 '16

i threw up a little in the back of my mouth when i saw it.

0

u/[deleted] Jun 22 '16

[deleted]

4

u/MorallyDeplorable Jun 23 '16

They're abusing labels/gotos to implement an ustructured function, akin to the classic versions of BASIC were interperted, such as GW-Basic.

It's basically an odd way to implement a dated technique in a modern environment. It produces spaghetti code that's really hard to understand without excellent comments. In this example you'd pretty much have to run a simulation of the program in your head just to figure out the basics for it, since there's no documentation.

0

u/[deleted] Jun 23 '16

[deleted]

3

u/MorallyDeplorable Jun 23 '16 edited Jun 23 '16

I find this annoying to read and I program to keep a roof over my head.

Also, GW-Basic is the predecessor to Q-Basic.

2

u/beforan Jun 23 '16

I find this code morally deplorable