The Importance of Naming
What's in a name? That which we call a rose, By any other word would smell as sweet;
Juliet, Scene II, Romeo and Juliet1
Names make it easier for us to identify both physical objects and abstract ideas and feelings.
As names are important to us, they should not be abused. The same goes with sentences. Adjectives, verbs, nouns, everything comes together to piece a cohesive sentence. Conventions are set in place to ensure that everyone can understand your sentence.2
The same goes with code. Programming languages come with conventions to make your code look readable3, beautiful, poetic, reusable, and hence save you alot of time in coding.4
Code conventions can be top down, and dictated by the Project Manager, or be recommendations from the bottom up.
This post will look at some of the stylistic conventions or syntatic sugar that Ruby5 provides to make life easier for you.
Emo Methods!6
When starting Ruby, a lot of things took me back. Especially the unique inclusion of allowing methods to be named with the exclamation and question mark.
I absoutely love
this feature of Ruby, as it gives the language expression.
When I think of this feature, I think of Newspeak, in that our ability to express oneself is only limited to our vocabulary. A larger vocabulary leads to a more expressive language7, and more expression allows one to state their intent clearly.
The Question Mark (?)
Oh Magical Eight Ball
is this array empty?
array = [] # Declaring an Array array.empty? # true
Magical Eight Ball
responds: Yes — definitely.
Methods with a question mark, are intended to "ask a question" (obviously), and are intended to only return true or false (unlike a Magic Eight Ball [image16x16:8ball] with 20 different possible variations). These methods generally involve looking at what state the object is currently in.
Because everything is an object in Ruby, the context is very clear. You are testing on an Array, and not a Numeric.
In other languages that support dynamic arrays, it may look something like this: (In PHP)
$array = array(); # Declaring an Array count($array) == 0 # true
Magical Eight Ball
responds: Reply Hazy, try again.
What am I doing? Oh poor lament souls what does count do?
The Exclamation Mark (!)
Methods ending in a exclamation mark indicates that the method is destructive (in that it will modify itself). Beware, the object will cut it self!
string = " I am protected by whitspace! " # Declare a string string.strip # "I am protected by whitespace!" string # " I am protected by whitespace! " string.strip! # "I am protected by whitespace" string # "I am protected by whitespace"
Magic Eight Ball
responds: Don't count on it.
Here we declare a string, and try to strip it of its whitespace. The second line calls the strip method where the return value's whitespaces have been removed from both the left and right side. But when you check the variable again (line 3), the dreaded whitespace still exists! You are safe for now little string. This time we specify an exclamation mark (line 4), and check the value (line 5) Destructive!
Lets go through another (more complicated) example, looking at how each object has a different id.
string = "I don't want to be MODIFIED" string.object_id # -438 lowercased = string.downcase # "i don't want to be modified" lowercased.object_id # -498 <= Freak string.object_id # -438 newstring = string.downcase! # "i don't want to be modified" newstring.object_id # -438 string.object_id # -438 newstring # "i don't want to be modified" string # "i don't want to be modified"
Magic Eight Ball
responds: My sources say no.
In this example, we will once again be using the downcase method to down/lowercase the string. Without the use of the exclamation mark (line 4), a new object is returned (see the object_id). When used with the exclamation mark (line 6), the returned object, is the object itself.
Note:
object_idis the integer identifier of the object in program. No two objects share the same object_id.- You don't need to collect it into another variable, I just put it there for demonstration pursepose.
What is the purpose of including these? It makes it easier for the programmer to judge the intention of the method.
Capital Letters Please
Ruby has a naming convention for constants and classes. These should begin with a capital letter, and follow any series of alphanumeric characters or the underscore. There are no other way to define constants. define() or #DEFINE does not exist in the syntactical world of Ruby.
MAXIMUM_PAIN = 100
Magic Eight Ball
responds: It is certain.
For word delimiters, constants use an underscore (RUBY_VERSION), and classes use camelcase (ActiveRecord).
Here is an example (done within irb) of trying to declare a class that doesn't start with an uppercase letter.
>> class terror
>> end
SyntaxError: compile error
(irb):5: class/module name must be CONSTANT
from (irb):2
from :0
Magic Eight Ball
responds: Outlook not so good.
Bad. Ruby simply complains! You are doing a bad thing! And bad things come from bad people! Therefore you are a bad person and should be condemned to the fires of SyntaxError Hell! We are not writing Manglish people.
Conclusion
Anyways, just some tips to keep your code looking beautiful throughout the years. 
Out of curiosity, are you a "undisciplined_underscore_utopian" or a "CamelCaseConspirator"?
Note:
- This is one of the few lines I can recall from Shakespeare's plays
- Thats right, I'm looking at you Manglish speaking people.
- Coding conventions, do not guarantee readable code. You can still make spaghetti code following these conventions.
- Actually Ruby has many many ways to do one thing. This is both a pro and a con.
- These conventions doesn't mean Ruby is rigid.
- Or other wise known as Descriptive Methods
- Of course its up to the person to be familiar with the vocabularly
