The beginning of the project – SpaghettiCode

I promised that in the next article we will already inject dependencies, however, I decided to do everything in turn, present the code on which we will work 🙂


I built a simple small application running in the console on which we will work through the entire dependency injection department, I specifically wrote it as if it would do a novice developer, i.e. how you can guess this code will be untestable and very unreadable, I will throw the code right away 🙂

It is an application or rather a web game prototype that first registers the user in the player database and later in this game we make our own character, I did not want to throw in some complicated example, some application that uses the map I thought that the example of the web game will be the easiest to understand 🙂

So what is the action plan?

  • First, we register the user, he must provide login and password.
  • Then we validate the login and password you entered, if the data is correct, we register the user in the database (Yes I know that you need an activation link and token to register your account, but this is not what I want it to be easy to understand)
  • Next, when we register the user in the database, we go to the step of creating characters in the game (here I have also simplified the process of character creation quite simplicity) there are four classes: barbarian, paladin, mage, thief, all classes have two attributes strength and stamina, their number depends on the character class. After choosing the class, we add again strength and stamina points to the chosen character class, their number is also dependent on the chosen class (It is known, in this case the barbarian will have the biggest statistics :))
  • And finally, we go to the game 🙂
As I see this code, it’s a hedgehog on my head … My reaction to this code is like gandalf reaction, I hope that yours is the same 🙂

Admit honestly… have you seen such a code or maybe even written?

I admit … I wrote such a code. Well, life :), but everyone learns from mistakes and mistakes must be learned.

Let’s analyze this code, according to the first SOLID rule, the class should have one responsibility and here the Gameserver class does more than one thing. Classes should also not, and in my opinion can not be inherited after a specific class, classes should inherit from interfaces or abstract classes as it is in the last solid principle “High-level modules should not depend on low-level modules – the relationships between them should result from abstraction.”

It’s difficult to understand this code, so it will be difficult to maintain. Tests can not be written here, so fear to change anything here …

And although it is still a simple case the largest is the GameServer class, which has two methods really that do something and already creates such chaos …

What would happen if it was a big project, then in the later phases of project development, it would be necessary to make sacrifices to god, in the hope that another added module will work 😂😂😂

This code also breaks the second solid open-closed principle.

What if I would like to add some further attributes to character development, for example wisdom or dexterity?

Or if for example I would like to add an email to the registration panel or create a token, which confirms that the user who registers is really himself or someone else?

What if I would like to better protect the login panel from attacks?

etc etc can be exchanged in an endless way, we can not leave it that way, in the next article we will think how to write it better also with unit tests.



Link to github with the whole code from this article:

This content also you can find on medium:

As a standard, I remind you about the newsletter, which I send notifications about new entries and additional information about the IT world in general.🙂

And NECESSERILY join the DevmanCommunity community on fb, part of the community is in one place 🙂

– site on fb: Kowalski

– group on fb: DevmanCommunity

Ask, comment underneath at the end of the post, share it, rate it, whatever you want🙂.

If that post was useful for you share it with your friends :)

Post a comment

Notify about