<- Your first program
About this chapter ->

Writing good programs

Too many tutorials simply teach you how to program, without ever teaching you how to make good programs.

Pick good variable names

Here are some things should consider when choosing variable names.

Meaningful names

The variable name should convey what kind of information is contained in the object. Here are some examples:

Good     Bad
age
student
name
count
sum
product
a
foo
xwy     

Multi-word names.

Don't be afraid of making variable names with more than one word. Just make sure that it's readable.

There are two conventions for doing this:

  • studentAge
  • student_age

I prefer the last one, but it's up to you.

Plese do not write 'studentage'. You would not appreciate it if I wrote this tutorial without any spaces.

Use constants

Whenever you have a value that should not change, always make it a constant. This way Ruby will help you catch some possible errors.

Some examples:


Pi = 3.14159265
Max_length = 100
Electron_mass  = 9.109e-31
Speed_of_light = 3e8
Earth_Sun_distance = 5.79e10   
                       

In general, do not write numerical values directly. Use constants to make your code more clear. For instance, the formula for the area of a circle is A=πr2. Where r is the radius. Your code should resemble this formula as much as possible.

Good   Bad

PI = 3.14159265

area = PI*radius**2
                       


area = 3.1416*radius**2

                       

It is also very typical to use all uppercase letters (as I did with PI above) to make the distinction clearer. For one, it helps you tell appart a constant from a class. If we did this, the examples above would become:


PI = 3.14159265
MAX_LENGTH  = 100
ELECTRON_MASS  = 9.109e-31
SPEED_OF_LIGHT = 3e8
EARTH_SUN_DISTANCE = 5.79e10
                       

Finally, another advantage of constants is that Ruby will produce an error if you change them. This helps you make sure that you don't accidentally change the value of a "constant".

Use interactive resources

There are several interactive resources that can help you write better programs more easily.

Use irb

The first and foremost tool is irb. This is an excellent tool for testing small portions of your code. When you program, you should keep an irb window open, and go back to it whenever you want to experiment with ideas.

irb lets you experiment, try things and explore very quickly. This is what irb was designed for. If you use it wisely, it will make you a much better programmer.

The ri program

Ruby comes with easily accessible class documentation. You can access it through the ri program. Some examples are provided below.

Note: The ri documentation assumes that you already have a fair ammount of knowledge of Ruby. It might take some time before you understand everything on it. However, you don't need to understand everything to learn a lot from it. You should start using ri as soon as possible, and if you have questions contact the mailing list.

ri can give you information about a class.


$ ri Float
------------------------------------------------------------------------
     class: Float  < Numeric
------------------------------------------------------------------------
     Float objects represent real numbers using the native
     architecture's double-precision floating point representation.

------------------------------------------------------------------------
     <=>, Arithmeticoperations, ceil, finite?, floor, infinite?, nan?,
     round, to_f, to_i, to_s
------------------------------------------------------------------------
                       

Notice that, at the end, ri gives you a list of Float methods.

ri can also give you information about a method. Notice that ri uses the same Class#method notation that I use in this book.


$ ri String#chop
------------------------------------------------------------ String#chop
     str.chop -> aString
------------------------------------------------------------------------
     Returns a new String with the last character removed. If the string
     ends with \r\n, both characters are removed. Applying chop to an
     empty string returns an empty string. String#chomp is often a safer
     alternative, as it leaves the string unchanged if it doesn't end in
     a record separator.
        "string\r\n".chop   #=> "string"
        "string\n\r".chop   #=> "string\n"
        "string\n".chop     #=> "string"
        "string".chop       #=> "strin"
        "x".chop.chop       #=> ""

                       
<- Your first program
About this chapter ->