Basically, the inspiration behind this is that I constantly see a lot of scripts released which are very uneasy on the eye, awkward to understand and not easy to implement extra functionality.
By improving the way you script and even the way you name your variables, you can enable a much larger amount of people to understand and extend your scripts. Sure it might take a bit more time but it's worth it in the long run.
When programming, how you use your syntax greatly affects the flow of your program, it's easy to get lost in code; which is a bit silly considering it doesn't take much effort to make it so much more visually pleasing and easy on the eyes. I'll demonstrate some techniques you can do to make your code look cleaner and more comfortable.
1. Spacing
During programming and scripting, spacing is a necessity when wanting nice clean code. A lot of people tend to have not picked up the habit of spacing when writing scripts. If anyone has read my scripts you'll see that i use spacing quite frequently; it helps to produce nice clean code.
Here's an example of code without spacing, it's hard to read and it's messy.
CODE
def TestFunction(x,y,z,string="DefaultString")
tempnumber=10
tempstring=string+tempnumber.to_s
print(tempstring)
end
tempnumber=10
tempstring=string+tempnumber.to_s
print(tempstring)
end
Here's the same code again, with spacing added, you now see how much nicer and easier on the eyes it is.
CODE
def TestFunction ( x, y, z, string = "DefaultString" )
tempnumber = 10
tempstring = string + tempnumber.to_s
print ( tempstring )
end
tempnumber = 10
tempstring = string + tempnumber.to_s
print ( tempstring )
end
It's significantly easier to read, just from adding spacing. It's really good practice to pick up spacing your syntax. As you can see, it's quite effective.
2. Parenthesis
As my roots in programming come from C/C++, I'm very keen on parenthesis, since this is optional in most cases in ruby not many people use it. But it's a great tool and really helps when reading through scripts and even scripting them. For example, take a look at the following code and compare it to the code after. Which do you think is easiest to read?
(Move away from the screen, look from a distance... Which is easier to read?)
CODE
def TestFunction ( x, y, z, string = "DefaultString" )
tempnumber = 10
result = x + y + z - tempnumber * x + y + z / 2
tempstring = string + result.to_s
print tempstring
end
tempnumber = 10
result = x + y + z - tempnumber * x + y + z / 2
tempstring = string + result.to_s
print tempstring
end
As you can see, this code is quite crowded, and uneasy on the eyes, with spacing it's a lot more pleasing, but still not quite there yet. Here's the same code with parenthesis:
CODE
def TestFunction ( x, y, z, string = "DefaultString" )
tempnumber = 10
result = ( ( ( x + y + z ) - tempnumber ) * ( ( x + y + z ) / 2 ) )
tempstring = string + result.to_s
print ( tempstring )
end
tempnumber = 10
result = ( ( ( x + y + z ) - tempnumber ) * ( ( x + y + z ) / 2 ) )
tempstring = string + result.to_s
print ( tempstring )
end
Being easy to read isn't the only advantage of parenthesis, as you should know, if you did mathematics in school, parenthesis also is good for equations. It's also easier for the ruby interpreter to understand, since it has parenthesis it can parse your script a lot easier.
3. Indentation
You've probably already noticed that I proper indent all my code, indentation is a great programmers tool, use it properly and your code will be easier to understand and a lot cleaner. Here's an example of a commonly bad indented script where someone as obviously copied some script from somewhere and left it without fixing the indentation:
CODE
def TestFunction ( x, y, z, string = "DefaultString" )
tempnumber = 10
result = ( ( ( x + y + z ) - tempnumber ) * ( ( x + y + z ) / 2 ) )
if ( result >= 20 )
print ( "Test!" )
end
tempstring = string + result.to_s
print ( tempstring )
end
tempnumber = 10
result = ( ( ( x + y + z ) - tempnumber ) * ( ( x + y + z ) / 2 ) )
if ( result >= 20 )
print ( "Test!" )
end
tempstring = string + result.to_s
print ( tempstring )
end
And here's the same code with nice indentation:
CODE
def TestFunction ( x, y, z, string = "DefaultString" )
tempnumber = 10
result = ( ( ( x + y + z ) - tempnumber ) * ( ( x + y + z ) / 2 ) )
if ( result >= 20 )
print ( "Test!" )
end
tempstring = string + result.to_s
print ( tempstring )
end
tempnumber = 10
result = ( ( ( x + y + z ) - tempnumber ) * ( ( x + y + z ) / 2 ) )
if ( result >= 20 )
print ( "Test!" )
end
tempstring = string + result.to_s
print ( tempstring )
end
As you can see it's a lot cleaner, this is another good practice. And now to my last section (Unless I update this
4. Naming
This is something a lot of people have problems with, naming variables and functions. I've seen countless times in other people's scripts variables and functions with misleading names. You should always name them in a way that it's easy to understand what it will be doing.
And an example of a variable name is this for a players step count:
CODE
@dans_thing = 2
You should never name anything like this, we have no idea what that does. A better thing would be:
CODE
@player_step_count = 2
It's now obvious what this variable is. Here's an example of a badly named function for setting a text position offset:
CODE
def TextThingy ( variable )
end
end
As you can see, this is also bad, we have no idea what this function does, and we have no idea what the parameter variable is for. A better function would be:
CODE
def TextSetOffset ( offset )
end
end
That brings me to my next point, i often see in scripts when people add variables to the RGSS $game_temp or $game_system for example, they name they're variables with generic names, when adding things in RGSS it's always best to add a prefix before your function and variable names, so that people understand what your script adds, and to prevent conflicting with other scripts. A bad example of not adding a prefix before a variable in $game_temp for a message system called DMS (Dan's Message System) for a text storage variable is:
CODE
class Game_Temp
attr_accessor :text
alias my_initialize initialize
def initialize
my_initialize
@text = ""
end
end
attr_accessor :text
alias my_initialize initialize
def initialize
my_initialize
@text = ""
end
end
A good example with prefixes is:
CODE
class Game_Temp
attr_accessor :dms_string
alias dms_initialize initialize
def initialize
dms_initialize
@dms_string = ""
end
end
attr_accessor :dms_string
alias dms_initialize initialize
def initialize
dms_initialize
@dms_string = ""
end
end
You can can clearly see that the added variables are for dms. Notice the alias named dms_initialize, always put a different prefix before your aliases to prevent conflicting with other scripts aliased functions.
Anyway, that concludes my tutorial for today, I hope you learned some things by this, and I'm glad to help. If you have any questions you can message me more post in reply to this. Remember, it's good practice to get into these habits, you'll find yourself speeding along when your code is clean and easy to understand.
Thanks,
DeadlyDan
