Tips When Organizing Your Ruby Code - dummies

Tips When Organizing Your Ruby Code

By Christopher Haupt

Ruby is an object-oriented programming (OOP) language. Everything in Ruby is an object that contains some data and some code that gives the object behavior. You can think of objects as the nouns of a sentence; think of the behavior, called methods, as the object’s verbs. When creating your own objects, there are some good general practices to keep in mind.

Do one thing well

When you’re planning your program, think about all the objects that are needed and how they might interact. For instance, if you’re making a card game, you might have individual playing cards, a deck that holds cards, a game itself that knows the rules of the game, and players who play the game.

It’s tempting to roll all your code into a single, large piece of code, but when you get to a certain size, that approach is really hard to change, fix, or reuse in other projects.

Instead, keep your objects focused on doing one job.

Keep it simple

In Ruby, you define the data and behavior of your objects in a class. A class is kind of like a template from which you can make one or more objects. The class contains the code that created behaviors, called methods. The class also may contain the data that is needed for the object to operate, such as the face value of a playing card.

When you write your code for a class, it’s far better to write small methods than larger ones. Each small method can be used in combination with others to create more complicated behavior.

By keeping the methods smaller, it’s easier to test each method in isolation. When tracking down bugs, it’s often easier to sort through fewer lines of code.

Another way to keep your code simple is to make good use of built-in Ruby objects and their methods. Ruby has a very rich set of built-in capabilities. You’ll save time using these methods.

Hide your data

One way that objects start to become difficult to reuse or debug is when the objects “share too much information.” If your object has a lot of data, but it’s all exposed to the outside, it’s hard to change the way you work with that data without breaking other objects that are dependent on it.

Ruby provides a number of ways to hide your object’s data. You can write your own methods that provide “getters” and “settings” to manipulate the data. You can also use Ruby’s attr_accessor methods, which create getter and setter code for you.

Both techniques give you a layer between the outside world and your object’s code and data. If you then need to make a change, you can alter your objects’ implementations while maintaining their interfaces so other programmers who use your code don’t have to change their own.

Follow Ruby conventions

Ruby has a number of naming conventions that are expected when writing Ruby code. If you follow these standard techniques, you make your code much easier to understand by other programmers.

You should always try to put just one class in a file, name a class starting with a capital letter, and use capital letters between multiple work names (CardGame). Files that hold your class are named in all lowercase using “snake case” (card_game.rb). Ruby itself expects this.

Constants are usually all UPPER_SNAKE_CASE, and local and instance variables, as well as method names are all @lower_snake_case (without the @ for local variables and method names).

Ruby doesn’t require all the extra symbols that some other languages do, so you’ll notice a lack of different kinds of parentheses, semicolons, and other unnecessary punctuation. Although you can use them if you want, don’t use them if they don’t add to the understandability of your code.

However, and this is a big one, if you end up working in someone else’s code, follow her conventions first and foremost.

Ruby’s approach is a recommendation, not always a requirement. Do what you’re comfortable with, and enjoy writing your code!