Jump to content
 Share

_Rocket_

Rules to Follow for Efficiency in Programming

Recommended Posts

Introduction

Some programs take hours of work to finish. This is because a programmers job is to problem solve. You will spend a lot of your time trying to fix bugs and navigate the very code you write. And eventually, you might even get your own workspace in a job where others will view the very code you write.

 

With this all being said, efficiency is key. And when you are a beginner with programming, there is likely a couple rules you do not know about that could potentially save you hours of frustration.

 

I am not an incredibly experienced programmer. But I do lots of research, have spent already countless hours working on projects, and more. I believe I have a few tips that could save a few beginners from pulling their hair out.

 

This post is going to mainly focus on methods and functions. If you do not know what these are, I would recommend you consult a video tutorial and study further into the programming language you are using before reading this post. The subjects in this post may confuse you otherwise.

 

Making a function is important to a programs functionality. Everything happens within functions. However, as a beginner you tend to misunderstand how a function should be used. Don't worry, I made the same mistakes. So let me first explain 3 key rules you want to follow when you make a function:

 

Make a Function Readable

Make a Function Clean

Make a Function With Purpose

 

Now, you may ask what these 3 rules mean. Good question. I am going to break these 3 rules down as best as I can. Listen and read carefully. These rules are likely very important, and might vastly improve your code if you are a beginner programmer.

 

 

Make a Function Readable

Methods and functions are made to preform tasks. When you call a function, that function will access your RAM to find the instructions that the method stores, and executes every instruction until the method is complete. However, the one thing people usually don't realize is... Well, method names are incredibly important.

 

A common mistake I see in code is method names being too cryptic. The common excuse is "it makes the code look cleaner". This is not a valid excuse. They will also often back this excuse up with "I have comments that tell me what the method does!". Once again, this is NOT a valid excuse. Let me tell you why.

 

When I used to make method names short, I'd find myself scavenging through my code trying to find a specific method. Not a specific method name, I was trying to find a method that did a specific thing.

 

When you make a method name cryptic, you cannot clearly tell what it does until you read it's comments. This can help you at first, but what happens if you take a break for a few days? You are going to come back and completely forget what half the methods do. So you'll spend the next few minutes trying to find one specific method. It's... Just not smart.

 

When you name a function, name it with a purpose. Make the method name say what it is made to do. Have it say it's purpose. You should rarely have to use comments. Let me provide an example.

 

int incrimentDigit(int dig)

{

   return ++dig;

}

 

This method has a name that explains it's purpose without the need of a comment. If you had to find this method in a file with 250 lines of code, it would be much easier to spot due to how obvious it is. However, what if I named it like this:

 

int incD(int d)

{

   return ++d;

}

 

There's a good chance you'll remember this method for a couple days. However, if you took a week break, or gave this code to someone else... They would have a hard time telling what the method actually does without having to find the method itself and read the code. Do NOT do this. Even if the method name gets rather long, just deal with it. It will save you from a ton of frustration later on.

 

 

Make a Function Clean

While method names can be rather long, making the code look a little less polished, it makes it much easier to read. However, that doesn't mean you lack ways to make code more elegant. I mean hell, I write code in C++, I'm pretty much contracted to write code as elegantly as possible.

 

Here's a couple ways to clean up your code, and potentially even optimize performance. And this is often done by accomplishing the same goal with less code. But how do you do this? Well, think about what it takes to be a good writer or journalist. Their goal is to get the same message across with as little words as possible. You have a very similar job as a programmer. Here's a few ways you can go about accomplishing this goal:

 

Ending a Method When it isn't Needed

This is a pretty cool one. Let me show you some code.

 

void runProcess(const UserObj& obj)

{

   if(obj.exists() && obj.hasID())

   {

      obj.incrimitDigit();

      obj.establishConn();

      obj.finishProcess(true);

   }

   else if(obj.exists())

      obj.genID();

}

 

Don't get me wrong, this method works perfectly fine. However, there's a cleaner way of writing it. If you analyze the logic here, it's a basic if else statement.

 

If both the User exists and has a generated ID, run the code in the if block. Otherwise, if the User at least exists, generate an ID for the user. The thing is, there's a way to preform these checks without an if else statement. Do you know how to do it? And no, take a moment to think how you could write this code without an if else statement if you can.

 

Did you figure it out? Well if not, here's how you'll do it.

 

void runProcess(const UserObj& obj)

{

   if(!obj.exists()) return;

   if(!obj.hasID()) obj.genID();

 

   //Do all the other stuff you saw

}

 

This has done two things. For one, you just fixed a potential but, because now the ID generates for the User, then preforms the processes necessary if the user does exist and has an ID. For two, the code is now much cleaner.

 

Pretty much what happened here is, if the player doesn't exist, nothing in the method needs to be executed. So, the very first thing you do is check for 

 

if(!obj.exists()) return;

 

When you do this, everything below this line will not execute. That is because returns always end the method outright. If a return instruction is executed, the method will ALWAYS end. No if else statement required.

 

However, if the User does exist, the code continues. The next like falls on this:

 

if(!obj.hasID()) obj.genID();

 

This will check and see if an ID exists on the user. If it doesn't, it generates the ID, then the method continues normally. And from that point, all the other instructions are executed as well. The code does the same exact thing, but looks 10x cleaner.

 

Integer Incrementing is Cool

Let me show you code:

 

if(numVal == 6)

{

   runThing();

   numVal++;

}

 

I mean, you are Incrementing the number correctly, but why not do it like this:

 

if(++numVal == 6) runThing();

 

Pretty cool huh? You can use the ++ or -- at any time, even inside arguments. It's really useful to make code a little cleaner looking. Now be careful. The position you put ++ in will change the order in which things happen. Always remember, code is read from left to right.

 

Let's say numVal is currently equal to 5.

Doing if(6 == numVal++) would return false. Why? Because you incremented numVal by 1 AFTER the boolean operator was already executed for the if statement. What you want to do is this:

if(6 == ++numVal) this will increment numVal by 1 BEFORE the boolean operator is executed. This way, the if statement goes through because the boolean operator returns true.

 

 

Make a Function With a Purpose

What the hell does this mean? Well, it's simple. Always make functions do 1 thing and 1 thing only, and make sure it does that thing well. This is the biggest mistake I made when I started out as a programmer. I'd always make functions do more than 1 thing. A function should only have 1 purpose. This makes code much easier to bug test and edit. This one rule will save you HOURS of uneeded bug finding and hair pulling.

 

If your method takes 5 different arguments, has more than 10 lines of code within it, etc. There's an extremely good chance you're function is doing more than it should be. Methods should be short, efficient, and incredibly good at preforming ONE task. This makes it much easier to understand the function. This also makes it easier to know what functions to use at what times, and what functions to edit if something goes wrong. As soon as I started following this rule, my bug fixing started taking at most 20-30 minutes instead of 3-4 hours. I am serious, PLEASE, make sure your functions only serve 1 purpose. This is CRUCIAL for your SANITY. And more importantly, it is incredibly crucial for other people's sanity when you get a programming job and other people have to read your code.

 

      

If this was confusing in any way feel free to ask questions. I can provide answers and even write actual code in an actual text editor to provide proper examples that visually demonstrates what is explained here. If I ever make a video covering this topic, I will link it.

 


I write programs and stuff.

 

If you need to contact me, here is my discord tag: Dustin#6688

 

I am a busy person. So responses may be delayed.

1840045955_Thicco(1).thumb.png.87c04f05633286f3b45b381b4acc4602.png

 

Share this post


Link to post
Share on other sites


Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×
×
  • Create New...