Imma spaghetti coder. I don't know why, but I am. So anyway, what's spaghetti code and why is it useful? Well first, it's not exactly useful, and second, I don't actually like sauce on my spaghetti. Oh wait. That's not what I meant to say. Anyway, spaghetti code basically is the code that is written during an apocalypse whilst trying to stop Earth from exploding. With only seconds time left, the code must be written as fast as humanly possible or all is lost. Not really, but spaghetti code is pretty much code that is written quickly or carelessly and ends up in a gigantic tangle that only the programmer who wrote it can understand. Yay! |
So why write spaghetti code? Well, while it wouldn't exactly be wise to use "carelessly written" code in a security application or a team project, what about an indie-developed game? Like the drawback is that it's usually pretty hard for other programmers to understand somebody's clumsily written code, but if you're the only programmer, why not? To quote Shift 4 (by Armor Games) in the end song, "It's just a complicated mess -> that no one understands -> except it's coder. -> Code Fudged!" From that I'd guess that the game was probably written with spaghetti code. xD I know that I like to write spaghetti code, I'm sure others do to, and I'm not encouraging it more than saying why not, if it doesn't harm anybody. Well anyway, along my very twisted path (nah jk) I have managed to find a way to write code that is tangled, yet is still somewhat manageable. So if you write spaghetti code (like me) because you don't like wasting precious developing time making the code look pretty, here's a few tips (yay) for writing... wait for it... better spaghetti code! :P |
1. Quickie Loops & Flow Control |
Okay this is a fun one. There's a type of loop that you likely haven't seen demonstrated in your C# coding books. I'm sure you've probably gone through while loops, for loops, recursive loops, etc. billions of times. But what about the legendary goto loop? Never heard of it before? Waaaah! Maybe you have? Yay! So anyway, to make a goto loop, simply write a bit of text followed by a ":", then after it, write "goto [text];". Like this: start: // do stuff goto start; So when the program reaches the goto statement, it will jump to wherever "start:" is. In this case, "start:" is before the goto statement so it makes a never-ending loop. You can simply exit it with another goto statement. start: // do stuff if (condition) goto end; goto start; end: Another cool thing: goto statements can go to any label that is within their scope. (labels are the text with the ":" at the end btw) So with the loop you made above - well, you can even jump right back into it even once it has exited! start: // do stuff back_into_loop: if (condition) goto end; goto start; end: if (condition) goto back_into_loop; Confusing? Nah. Fun? Definitely! I'd say that goto statements are extremely useful if you have some very tangled spaghetti code that you need to jump in between and around. |
2. Variable Soup |
So on some of my bigger C# programs I've noticed that all of the global variables I put at the top of the game class get exhaustive. I really don't like having to cut down on global variables, because then I can't access them when or where I need to. But having billions of variables pop up when I start typing isn't much of a solution either. So I fixed it with a simple idea. I clustered them all in one place. Pretty much, for each stage of your game (I like to separate my game into stages), make an appropriately titled struct name. For example, in the level editor stage of a maze game I'm making at the time of this post, I used "EditorScreenVars" for the struct, and filled it with dozens of variables only relevant to the editor stage. Then I simply put "EditorScreenVars ESV = new EditorScreenVars();" as a global variable at the top of my game class. struct VariableSoup // struct to hold random variables { public int var1; // make sure all variables are public or you won't public int var2; // be able to access them inside the game class. public Point var3; public string var4; } VariableSoup VS = new VariableSoup(); // global var in game class And suddenly I could access a bunch of global variables just by typing a few characters, but they wouldn't come up via regular typing. Yay! Sure it's not that groundbreaking. You're probably saying "Duh, that's basic!" right now, I don't know! xD But in terms of spaghetti code organization, it works, I'll say that. :P |
3. Divide And Conquer! |
One of the very easiest ways to organize code is to minimize methods that you aren't using. But with spaghetti code this might be a bit more difficult, as often certain methods will reach very long lengths. Minimizing them would be minimizing too much code to continue working efficiently. So how do you hide the code that you don't need, and only the code that you don't need? Easy! You click the [-] button that appears to the left of methods! That minimizes the code! Oh wait. Uhh... didn't I just say why that sometimes isn't enough? Oops. Like how to minimize selective code and not the entire method anyway? Well, turns out you actually can minimize certain code chunks, you just have to use a new keyword... err... keywords: #region and #endregion. Simply mark the start of the code you want to minimize with #region, and mark the end of it with #endregion. #region Minimize_This // code you don't need to work on // code you don't need to work on // code you don't need to work on #endregion // code you're currently working on Now a [-] button will appear to the left of the #region Minimize_This and you will be able to minimize it. |
Conclusion |
Yay I'm finally done writing now I can go back to making spaghetti! xD What I meant to say is that I hope you enjoyed my blog post! Oh and now comes the time to umm... beg for moneys? :P Nah jk I would never do that to you!!! Btw if you appreciate the time and effort I put into this site, please consider subscribing to the RSS feed (at the top of the page) and/or supporting me and my site by donating a small amount. Thanks again! Have a great day! xD xD xD - Superdoggy |