How To NOT Write "Spaghetti Code"?

I’m surprised that no one has asked this before. (I searched and didn’t find a similar topic)

This is my Explorer,

https://gyazo.com/c09c4f37dceca2d92f9d50c5edc8f0dc

am I doing it right?


What is Spaghetti Code?

How to avoid Spaghetti Code?

Why is Spaghetti Code bad?

When is it okay to have Spaghetti Code?

Where is it okay to have Spaghetti Code?

(please don’t throw a link at my face unless it’s really short and well explained)

4 Likes

What is Spaghetti code?
Spaghetti Code is messy code. This usually results from poor design and lack of forethought. This can also happen if you fail to design your overall game structure well, which might force you to write messy code in order to interact with your game as a whole. Thus, it’s important to start with good higher-level game design.

How to avoid spaghetti code?
Well thought out game design and code design. Follow standard coding practices. Keep your code styles consistent. Try to operate under a “framework” of some sort.

Why is spaghetti code bad?
It is hard to read, thus hard to maintain. It will also lead to more-frequent bugs; plus, the bugs will be harder to catch and fix.

When is it okay to have spaghetti code?
It’s ok if you’re prototyping or learning. Typically when you’re experimenting with new stuff, your code is going to be sloppy. That’s ok and should be expected. Just make sure you clean it up when you’re done. Your “spaghetti code” should never see the light of day in the real-world. In other words, if you’re publishing models/modules/games/etc., try to make sure the code is clean and crisp.

Where is it okay to have spaghetti code?
Well, anywhere, as long as it follows the answer to the last question. Ideally, the answer here should be “nowhere,” but it’s sometimes unavoidable when you’re prototyping.

Summary
Spaghetti code = bad code; messy code; buggy code; hard-to-read code. It’s the result of coding without thinking about what you’re actually trying to accomplish. You should expect to have this sort of code when you’re prototyping/experimenting/testing new stuff/etc., but should try to avoid it anywhere else.

Wikipedia’s synopsis of spaghetti code summarizes it pretty well:


When it comes to proper game design principles, you will end up branching out to other topics other than spaghetti code. I think studying proper game design principles is the best place to start, because that will lay a foundation for writing better code.

21 Likes

I don’t usually fuss over my code if I’m the only one reading it. Trying to create a framework makes me sit for days being unproductive when I could just be throwing scripts on and getting things to work. I still want things organized of course for my own readability, but I don’t create any frameworks or anything.

Otherwise, above is your answer.

Well, the point of creating a Framework is to create it ONLY once then use it for all projects, that actually saves you a lot of time, but you are right about

I’m having trouble worrying if it’s perfect or not thus creating this thread.


That’s the important part, you can’t expect to impact the world by working alone, your goal as to impact as many people as possible to be as impactful as it can, in other words I do plan on working with my own team, so others will have to read it obviously.


I’m sure that you agree about having stuff organized is better than a mess.

1 Like

What I mean, in terms of your second quote, is if I’m the only developer. If I’m working in a team, regardless of if anyone will read it or I’m the only coder, I always be mindful of my conventions, organization, so on so forth.

I usually write a mix of spaghetti code and normal code. I never intend to write messy code, but dang, after 2 hours of bug fixes and fine-tuning I just can’t help the mess.

4 Likes

I don’t believe that there is necessarily a correct way to write code, unless you are writing it for anyone other than yourself.

Eg: If you are working on a group project that requires other programmers to change and edit your code, they should be able to read and easily find things.
However, if you are writing it for yourself, as long as you can understand the way that you have written it out, do it anyway you want.

Watch this, you’ll understand why I asked.

@RedDuck765

Wow.

I’m actually going to go back on a small thing that I said… :smile:
There is a link in exams for school where people who have nice concise notes and ‘pretty’ books get higher grades than those who don’t, meaning that yes, writing clean and neat code would probably make it easier for you to diagnose a bug and find a solution.

At the end of the day its completely up to how you like to lay out a game and if you understand how it works, how to fix it etc… then it doesn’t really matter.

ironic, phantom forces uses a framework.

two of them, containing all of the game’s code.

one for the client, and one for the server.

the client framework has more characters in it than the first harry potter book

1 Like

A few Examples would be nice :sunglasses: