Submit Your Article


 
RPG Maker

Welcome Guest ( Log In | Register )


  Games Resources RPG Maker VX RPG Maker XP Scripts Tutorials Downloads

> [Scripting]Good Scripting Practices, Tips on good scripting practice
deadlydan
post Jan 31 2008, 05:30 PM
Post #1


Level 5
Group Icon

Group: Member
Posts: 71
Type: Event Designer
RM Skill: Masterful




Hey guys, as my first tutorial I wanted to start off with something simple, and easy to understand. This tutorial will show you a few tips of good programming/scripting practices.

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


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


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


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


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


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


As you can see it's a lot cleaner, this is another good practice. And now to my last section (Unless I update this smile.gif)

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


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


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


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


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
smile.gif


__________________________
Go to the top of the page
 
+Quote Post
   



Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 25th May 2013 - 06:06 PM
RPG RPG Revolution is an Privacy Policy and Legal
eXTReMe Tracker