Archive for June, 2007

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 Heart 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 8ball is this array empty?

Magical Eight Ball 8ball 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 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)

Magical Eight Ball 8ball 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!

Magic Eight Ball 8ball 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.

Magic Eight Ball 8ball 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_id is 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.

Magic Eight Ball 8ball 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 8ball 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. Heart

Out of curiosity, are you a “undisciplined_underscore_utopian” or a “CamelCaseConspirator”?

Notes:

  1. This is one of the few lines I can recall from Shakespeare’s plays
  2. Thats right, I’m looking at you Manglish speaking people.
  3. Coding conventions, do not guarantee readable code. You can still make spaghetti code following these conventions.
  4. Actually Ruby has many many ways to do one thing. This is both a pro and a con.
  5. These conventions doesn’t mean Ruby is rigid.
  6. Or other wise known as Descriptive Methods
  7. Of course its up to the person to be familiar with the vocabularly

Comments

Second Malaysia Ruby Brigade Meetup: Review

Wow, simply amazing. I’d never thought I would ever find people passionate about Ruby. It was a great meetup, and once again went between the hours of 8 to 12:30am. I would have continued longer, but there was some things to the following day.

Seven people attended this meeting. The original three and four others.

Malaysia Ruby Brigade Second Meetup
One of these are the original three.

Similarly it was a very varied discussion covering about all sorts of things:

  • Personal Background
  • Haml
  • Mash Ups
  • Capacity
  • State of education in Malaysia
  • Venture Capitalists
  • Startups
  • Lower barriers of entry
  • Web 2.0
  • The Semantic Web
  • The future of Malaysia Ruby Brigade

The last listed topic was what the reason for the meetup. But before we continued Kean asked a very important question. Why do we want to do this? (Or somewhere along those lines)

Once again, each of us have different opinions on why we should push forward for Ruby in Malaysia. Each one, influenced by their personal backgrounds.

  • Another avenue for the advancement of Free Culture (I wonder which crazy lunatic proposed this, and what juice was he drinking?)
  • Belief that Ruby is a truly great language that everyone can benefit from
  • Building local capacity for Ruby
  • Monetization

Or somewhere along those lines :P I forgots, do correct me if I’m wrong.

To me, all of these are valid points. Even the last one. But what’s more important to acknowledge is that we all benefit from each others contributions. We are all a catalyst for one another. I’ve discussed this in another post about FOSS Capacity in Malaysia. Even though our intentions may be different, our passion is still the same. Ruby.

So what were some of the ideas we came up with?

  • General presentations ala MyOSS
  • Hold Lightning Rounds: Maximum 20 slides in 5 minutes. Resulting in at least 10 presentations within an hour
  • Hackathon. Get together a group of people to spend one weekend coding a Rails application
  • Ruby/Rails Love (similar to Gnome Love) handholding in helping people contribute to Ruby/Rails)

Discussions will continue through the Malaysia Ruby Brigade mailing list. So if you are interested, but haven’t signed up, do join!

Malaysia Ruby Brigade Trivia: In Bahasa Malaysia (the official language of Malaysia), “Ruby on Rails” translates to “Landasan Delima

Comments (3)

Second Malaysia Ruby Brigade Meetup

Malaysia Ruby Brigade Smaller

There will be a second meetup tonight 8pm at The Curve Starbucks (at the Street). I know the announcement comes a little bit late, but something better than nothing.

The plan at the moment is to chat and sit around, and figure out how we can get more people to use Ruby in Malaysia. Basically figure out some concrete plans for this little growing group of ours.

One idea was for are a more presentation oriented meetup (ala MyOSS Meetups), with someone presenting on a Ruby related topic. Any ideas guys?

Following this I’ll post up for formal (at least in presentation) invitation (similar to my MyOSS Meetup Announcements) for the Meetups.

Malaysia Ruby Brigade Trivia: Rubyists come from all around Malaysia. A group of “hackers and code junkies in Sabah” calling themselves Phrozz have joined the brigade.

Comments (1)

« Previous entries · Next entries »