All Code

  • Configure your editor for soft tabs, not hard tabs
  • Expand tabs to two spaces
  • Leave a trailing newline at the end of each file

C Code

  • No space between condition and paren. if(1), NOT if (1)
  • Put the opening brace on the same line as the function declaration or conditional
  • Always use curly braces, even if they can be left out.
  • Prefer parentheses to implicit precedence rules (within reason)
  • Alternate versions of functions should be named why they are different from the primary.

If there is a function person() and you want a version that takes the name of the person, it should be person_with_name(char *name) or person_with_details(char *name, ...). NOT person1(char *name).

Ruby Code

  • See kernel/core/array.rb for a style reference.
  • Methods: Try to keep your methods short--one screenful and try to adhere to DRY within reason. Generally common functionality should be abstracted to helper methods (which you can make private) but in some cases, particularly working with Core, sometimes trying to DRY things up is just an obstacle if you have to maneuver around several different error conditions, for example.
  • Method names: should be clear, expressive and meaningful. Avoid using underscores to 'protect' the method (__send__) with some exceptions.

Smalltalk-style method names are OK, meaning that you could have a

method SomeClass.make_from when it is intended to be invoked as

SomeClass.make_from file or SomeClass.make_from :file => name. There the parameter name 'completes' the method name and makes for more natural reading.

  • Variable names: make them clear and meaningful (with some well-known exceptions like using i for a counter.) Try to avoid shadowing method names, for example within Array use idx in favour of index because the latter is also a method name.
  • Postconditions: use postconditions only if your expression is a one-liner and you do not have many conditions.
  • Blocks: Use either do..end or {..}, spaces between the delimiters and code (foo { |arg| code }). Split long or complex expressions over multiple

lines like this:

  mapped = foo.map do |elem|
    do_something_with elem
  end

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

0.1069% complete

 

Completed 9 of 13 tickets