Naming Conventions

Rory has identified the data entities and separated them into separate objects with each entity being defined as a property of the object. The next issue is what he will call these objects and properties. There are a number of things that need to be considered when choosing names.

Use Meaningful Names

The more meaningful names we use in code, the easier it is to understand what the code is doing when we return to the code months or years later (to debug or develop further). If the code is easier to understand later, it will also be more understandable for someone else who works on the code at a later date. Also, meaningful names allow the code to do its own commenting; the clearer the names, the fewer comments are required to remind ourselves what objects and properties do.

For example, when I was taught trigonometry at school, I was taught the following equation to describe the relationship between the lengths of the sides of a right angled triangle:

a2 = b2 + c2
Where a is the length of the longest side (the hypotenuse),b, and c are the lengths of the other two sides.

While writing the equation, it made it easier to simplify the length parameters into the single letter representations a, b, and c. However, when learning the equation, not only had I to remember the form of the equation, but also what each letter represented. So, remembering one equation required two memory processes: remember the equation and remember the where statement that described how each parameter was mapped to the right letter. In isolation from the where clause, it can be difficult to determine what the equation represented.

In coding, the benefit of shortening a parameter name is not as great as it is when making calculations on paper. In fact, the advantages of lengthening the name usually outweigh those of shortening it, if the lengthening is used to make the name clearer. Look at how the representation below of the same equation is easier to understand without defining a where statement:

long_side_length^2 = medium_side_length^2 + short_side_length^2

As the parameter names now describe the parameter itself, we no longer need a where clause.

The names also demonstrate how the simple use of underscores can be used to combine a series of words into a single text string in a way that makes them easy to read. An alternative is to use"camel case" where a capital letter indicates the start of each sub-word, so long_side_length becomes LongSideLength.

Use a Consistent Naming Convention

As names are often made up from a combination of words, it is always a good idea to develop a simple set of rules to help keep each name of a consistent structure. For example, if in the triangle equation the sides were labeled side_1, side_2, and three_side, someone else reading the code would have to wonder what it was about the third side that led to it being named using a different convention to the first two sides. Being haphazard in naming objects and parameters leads to unnecessary confusion.

A naming convention will also allow you to take a few shortcuts in names. For example, someone describing objects on a map of London may want to describe both Green Park (an area of London) and some parks that are green. They could use long names of green_park_the_district and park_that_is_green. However, if they used

a convention that:

  • place names always used camel case
  • descriptive text always followed the main description and was separated by an underscore they could have names of GreenPark and Park_green as simpler names.

Ruby on Rails Naming Conventions

Ruby on Rails has a set of its own naming conventions and complying with these conventions simplifies many tasks. Therefore, this is a good point to introduce some of these conventions as they can be used here to demonstrate some of the advantages of using a consistent naming convention. Also, it is probably not a good idea to describe many more naming conventions if they do not comply with the Ruby on Rails conventions and have to be abandoned later.

Constants and Classes

In Ruby, constants are written in camel case. The form and function of an object is defined by its Class. As Class names are constants, they always use camel case. So a Class that describes hairy bikers would be the HairyBiker Class.

Variables

In Ruby, variables are lowercase and use underscores to separate sub-words (for example, longest_side). Constant variables are by convention written in uppercase with underscores to separate sub-words (for example, ABSOLUTE_ZERO). Prefixes to variable names are used to define the scope of a variable with a single ampersand (@name) signifying that a variable is an instance variable and Class variables start with two ampersands (@@name)

Methods and Properties

Methods and Properties form sub-objects of a Class. They are treated as instance variables and therefore always use lowercase and underscores to separate sub-words. When used with the parent Class name, properties follow the Class name and are separated from it by a full stop. So if a bag object of the Class Bag has a property color, this would be written as bag.color.

Special Method and Property Suffixes

Some key ending words have special significance.

  • _id is used to denote a foreign key. So in a Class Named Products, the foreign key property that ties each product to a particular supplier would be called supplier_id.
  • _at and _on denote times and dates, respectively. So, sold_at is the time something was sold, and visited_on the date when a visit took place.
  • ?, a question mark at the end of a Method name signifies that the Method will only return true or false. So, if you want a Method that tests whether an item is in stock or not, you would call it in_stock? and you could then use this test:
if item.in_stock?
  • !, an exclamation mark at the end of a Method name indicates that the Method operates on the object it's raised on. If you have an array and you want a Method that sorts the array you could call the Method sort!. This usage is probably easier to demonstrate in an example:
# this method returns a copy of array "items" as a new object
sorted_items = items.sort
# whereas this method alters the object "items" itself
items.sort!

Please note that these are conventions. For example, adding a question mark to the end of a Method you create, does not automatically force the Method to only return true or false. The Method hello? shown below, will return the string Hello even though the Method name ends with a question mark.

def hello?
return "Hello"
end

Conforming to these conventions makes it easier for others to understand your code. Ruby naming conventions are not shortcuts to a particular functionality. Changing a method name is not enough to change its functionality. You must also change the underlying code.

Notice also that there are Rails Methods that do not conform to these conventions. Some Methods operate on the object that raised them, but the Method name does not end with an exclamation mark. For example, the Method model.save.

Reserved Words

The following are reserved words that cannot be used as object names:

BEGIN

class

ensure

nil

self

when

END

def

false

not

super

while

alias

defined

for

or

then

yield

and

do

if

redo

true

 

begin

else

in

rescue

undef

 

break

elsif

module

retry

unless

 

case

end

next

return

until

 

This information was obtained from http://www.ruby-doc.org/docs/ProgrammingRuby/, where further information on reserved words can be found.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.251.70