kemiller / rubymotion-style-guide

A community-driven RubyMotion coding style guide
18 stars 2 forks source link

Prelude

RubyMotion has some particular styling needs that don't translate perfectly from either of its parents, Ruby and Objective-C. Some are immediately obvious, like the class between methods_with_underscores in ruby, and methodsWithCapitals in Objective-C. Some are subtler, like do we prefer new/initialize() or alloc/init*?
The RubyMotion community so far seems to skew ruby-oriented, so this document is directly forked from "Ruby's", and conventions will tend to lean in that direction.

The RubyMotion Style Guide

This RubyMotion style guide recommends best practices so that real-world RubyMotion programmers can write code that can be maintained by other real-world RubyMotion programmers. A style guide that reflects real-world usage gets used, and a style guide that holds to an ideal that has been rejected by the people it is supposed to help risks not getting used at all – no matter how good it is.

From here on out, the guide will use "Ruby" and "RubyMotion" interchangeably, unless otherwise noted.

The guide is separated into several sections of related rules. I've tried to add the rationale behind the rules (if it's omitted I've assumed that is pretty obvious).

I didn't come up with all the rules out of nowhere - they are mostly based on my extensive career as a professional software engineer, feedback and suggestions from members of the Ruby community and various highly regarded Ruby programming resources, such as "Programming Ruby 1.9" and "The Ruby Programming Language".

The guide is still a work in progress - some rules are lacking examples, some rules don't have examples that illustrate them clearly enough. In due time these issues will be addressed - just keep them in mind for now.

You can generate a PDF or an HTML copy of this guide using Transmuter.

RuboCop is a code analyzer, based on this style guide.

Translations of the guide are available in the following languages:

Table of Contents

Source Code Layout

Nearly everybody is convinced that every style but their own is ugly and unreadable. Leave out the "but their own" and they're probably right...
-- Jerry Coffin (on indentation)

Syntax

Naming

The only real difficulties in programming are cache invalidation and naming things.
-- Phil Karlton

Comments

Good code is its own best documentation. As you're about to add a comment, ask yourself, "How can I improve the code so that this comment isn't needed?" Improve the code and then document it to make it even clearer.
-- Steve McConnell

Good code is like a good joke - it needs no explanation.
-- Russ Olsen

Comment Annotations

Classes & Modules

Exceptions

Collections

Strings

Regular Expressions

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
-- Jamie Zawinski

Percent Literals

Metaprogramming

Misc

Tools

Here's some tools to help you automatically check Ruby code against this guide.

RuboCop

RuboCop is a Ruby code style checker based on this style guide. RuboCop already covers a significant portion of the Guide, supports both MRI 1.9 and MRI 2.0 and has good Emacs integration.

RubyMine

RubyMine's code inspections are partially based on this guide.

Contributing

Nothing written in this guide is set in stone. It's my desire to work together with everyone interested in Ruby coding style, so that we could ultimately create a resource that will be beneficial to the entire Ruby community.

Feel free to open tickets or send pull requests with improvements. Thanks in advance for your help!

License

Creative Commons License This work is licensed under a Creative Commons Attribution 3.0 Unported License

Spread the Word

A community-driven style guide is of little use to a community that doesn't know about its existence. Tweet about the guide, share it with your friends and colleagues. Every comment, suggestion or opinion we get makes the guide just a little bit better. And we want to have the best possible guide, don't we?

Cheers,
Bozhidar