In this one I'm going to teach you about clean coding and how it can help you.Now, I've seen a lot of people with really crappy coding styles. They leave off semicolons, don't use double equals, still put "and" instead of "&&", etc. The fact of the matter is it's ugly and inefficient, and it can confuse you later on. Take it from me; I know. I tried looking at an old thing I made a few years ago and couldn't make heads or tails of it. In fact it's what gave me the idea for this!
Clean coding is simple and effortless if you're willing to go the extra mile to do it. You're basically just making your code easier to read. Great if you're using this just for you, fantastic if you're distributing the source. Whatever you're doing, it good to be able to make your code better to read and more efficient.
Step 1: Indentation
This is the most basic step in clean coding. It will make your life so much easier, especially when working with a lot of blocks (these things: "{}"
. Basically, whenever you start a new line after a beginning block, you hit your Tab button. Then, whenever you go to close a block, you hit backspace (once) to take away the indent.Step 2: Semicolons
Every time you don't put a semicolon after a function somebody tries to kill your closest living family member. That's how serious this is. You ALWAYS put a semicolon after a function. Why? Well, for one, you know when something ends. You're never in doubt about it. Second, it stops complications for one-line codes with multiple functions. GM would reject something like this:
varCheck = true show_message( "Hi" ) game_end( );
Lastly, it will help you in later-on programming languages that you might want to learn. Most of them require semicolons, so you'll have plenty of experience.
Step 3: Spaces
Relatively easy, just put a space before and after every parentheses. For example, this:
show_message("Hello World!"
;Becomes this:
show_message( "Hello World!" );
It's not direly important, but it will make your life a lot easier, trust me. It also makes your coding look more professional.
Step 4: Maximize Efficiency
Don't EVER use "and" or "or". They're GM demons that need to be eradicated. Instead, use "&&" for "and", and "||" for "or". They're a lot faster than the text-based commands, by a large margin. Also, when checking if a variable is a certain integer, string, etc. do NOT use one equal sign. Instead use two. So instead of this:
if ( variable = true ){...}
Do this:
if ( variable == true ){...}
Once again, this will help you in later programming languages as many will throw an error if you only use one equal sign.
Final Tips
-Please, for the love of God, don't open up blocks after you check a variable. Open it up on the same line. Adding another line to your line count is not helpful.
-Put parentheses around variable checks, as I've been doing in this article. It just makes the whole thing cleaner and more presentable.
-Practice clean coding until you do it automatically. It will pay off in the long run!
Comments
Loading comments...