Learn How to structure your code for projects

Guide to thinking like a programmer

The TechSavvy will receive a commission if you purchase anything through the links on this page. So, please! do so to support us.

Get your tech items, back-to-school essentials, house furniture, computer, and car replacement parts at the lowest prices here.

So I've been working on my game for about a year now. You can check out my dev blogs here by the way. Everything was going great in the beginning until the moment I needed to add a new gun to the game in addition to the one I already had. The problem is, I did not structure my project in a way that could make it easy for me to add new weapons. This means I have to code all the properties of each gun every time I need to add a new one to the game which leads to a lot of repetition. I've learned a lot from this experience and I want to share some of the lessons with you.

Hi my name is Mac, a student, programmer, and Game Developer and in this article, I will show you how you can structure your coding project to avoid doing the same mistakes I did when coding my game. I did a lot of research for this article so if you can give it a like and Follow my blog I will really appreciate it. With that said, these are the various things you can do to structure your projects better.

1.Think About Classes the right way.

glen-carrie-JiSkHnWLo2o-unsplash.jpg

Photo by Glen Carrie on Unsplash

This is more of a bonus point but I think it will help you better understand the points I will formulate down below. Classes are a thing in Object Oriented Programming languages but if you are not using one it's okay, you can still continue reading the article. A lot of programmers create new classes just because they don't want to overpopulate another class with code, which is not bad, but it's not good either. There are 2 ways to look at classes. They include;

Looking at classes as Entities(Objects).

This way the different variables in the class will be the physical traits of that object while the different functions/methods will be the various actions this object can perform. For Example, let's say you want to create a class about planes. Well in this class you'll have variables like planeColor, planeSpeed,wingSize and you'll have methods like TakeOff, Land, Turn. This will also make it very easy to create children of that class. If we continue with the example of Planes, you can now create a Boeing 787 & an Airbus A380 which will all be children of the base Plane class. This is the approach I should have used when coding my gun system but I hope you don't commit the same mistakes as me.

Modules

Another way to look at classes is as modules that work together with other classes to perform a bigger goal. Let's say you want to program movement for a player. You'll will have classes like **player walk, player run ** which will be modules of the Player Movement code. This will save you in the future trust me. Now you'll think better before creating a new class.

So the most important thing to do if you want to make your code more expandable in the future is to use

2. MODULARIZATION

choong-deng-xiang-GEONQEnR_3A-unsplash.jpg

Photo by Choong Deng Xiang on Unsplash

Modularization in programming refers to the splitting of large code into various chunks or modules. This way you can work on the different features with ease. The chunks here can be viewed as classes in Object - Oriented programming languages. Here you can use the second way to think about classes to achieve modularization. And if you're not using an OOP, Just make sure you look at your development plan (if you have one of course but let's be honest you probably don't, hehe) and analyze the parts that can be separated into sections. This will make your code more readable and avoid redundances.

The concept of modularization can be paired with these two concepts below to create even more robust code. They are;

a.Loose Coupling

In Computing and System Design a loosely coupled system is one in which each component has or makes use of little information about the other separate components. This means that you should program in such a way that your module doesn't use too much information found in other modules. This will ease the modularization of your code. It will also make Code Modification very easy and Ease Code Testing.

The second concept is

b. High Cohesion

High cohesion refers to the level of connectivity between the different components in a module. A highly-cohesive module is often considered as good because it is much more readable, testable, and maintainable. So whenever you program a class, make sure your class encapsulates one piece of functionality of your large code base.

2. MAKING YOUR CODE EASY TO TEST

sander-sammy--UVPQ6FpBAA-unsplash.jpg

Photo by Sander Sammy on Unsplash

Code testing is a very important part of product development if you don't want to end like the man on the picture above.

You know what they say "Good code is one that can be easily tested"- They

Implementing a good testing method will prevent you from spending tens of hours asking yourself "How can I make this code work??" or "What the Heck is wrong with this code??". There are a lot of code testing methods you can use but the one I'm going to talk about here is the

Unit Test method

A Unit test is one that instantiates a small portion of your code and makes sure it works in the correct manner. A good unit test must be Reliable, Readable, Simple, and Fast. There are 2 types of Unit tests which are;

State-Based Unit test

Here you check whether the code produces proper results.

Interaction-Based Unit test

Here you check if the methods are invoked properly or not.

So you should continuously check your code to make sure it doesn't cause any problem down the road and trust me it will save you a lot of time.

BONUS TIPS

-Descriptive Names for variables Now imagine you have been working on a project and you used hazardous names for your variables. Then one day, your computer gets struck by lightning and it goes bad (a totally realistic scenario) so you have to take a forced break until you get a new computer. Then when you want to get back to coding your project you see a variable called bigMama 4. Yeah, not very clear. And there are no comments to indicate what the code does, you know what this means, you have to start over. So you should always use descriptive and clear names for your variables, methods, and Classes even if they may be long at times.

  • Use Comments Remember our story from above, that poor person could have been saved by commenting. If only he used comments he could have an overview of what that code did. And if you are working on a team the comments you will leave will give a general idea of what the code does to your teammates. So you should always leave brief and precise comments.

    Thank you for taking the time to read my blog post. I've put a lot of effort into research and writing and I hope you enjoyed it. Don't forget to like and Subscribe to my blog. And if you want to continue the conversation you can join my discord server here. Thanks once more and I'll see you next time.