Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering
It's important that you remain consistent in using these conventions. There’s nothing worse than looking at code that switches between tabs and spaces, or mixed and lowercase variable names.
The indentation size is 2 spaces. Don't use tabs!
Auto-indentation can be used for formating the code automatically. You can find it in the menue Code→Auto-Indent Lines (Strg+Alt+I).
Don't use parallel assignment!
Incorrect example:
x, y, z = 1, "one", 100
If an integer is higher than 100.000 you can use _ to make it more readable.
Example:
puts 120_000 == 120000
The example prints true on the screen. Ruby ignores the _ in integer of class Fixnum.
If the block fits on a single line, use curly braces.
Example:
1.upto(10) { |x| puts x }
If the block extends over multiple lines, use do and end.
Example:
1.upto(10) do |x| puts x end
Use then and end to enclose the if-block.
Example:
if x == y then puts "x equals y" end
unless x == y puts "x equals y" end
Don't use elsif with colons after tests.
Correct example:
if x == y then puts "x equals y" elsif x != y puts "x isn't equal to y" elsif x > y puts "x is greater than y" else puts "x is smaller than y" end
Always use the else-statement in order to have a default case.
Hint: case can check objects of any types that ca rspond to the equality property and/or any equivalence operators.
Example:
lang = "fr" dog = case lang when "en": "dog" when "de": "Hund" when "es": "perro" else "dog" end
Use do and end to enclose the loops.
Example:
while x <= y do puts "x is than smaller y" x += 1 end
begin puts "x is than smaller y" x += 1 end while x <= y
until x == y do puts "x is than smaller y" x += 1 end
begin puts "x is than smaller y" x += 1 end until x == y
Use do and end to enclose the loops.
Example:
for i in 1..12 do puts i.to_s end
Class and module names starts with an uppercase letter, by convention they are named using MixedCase.
Example:
MyNiceClass or MyNiceModule
Constants should be uppercase and underscored.
Example:
MY_NICE_CONSTANT
Variables should be lowercase and underscored.
Example:
my_nice_variable
Incorrect examples:
myNiceVariable or mynicevariable
The name of a method should be lowercase and underscored.
Example:
my_method_name()
The next convention is verry common but net enforced by Ruby. Methods which ends with an !, ? or = have the following functionalities.
Use always the return keyword in order to define the return value explicitly.
Methods with arguments, should include parenthesis and no unnecessary spaces!
Example:
method_name(arg1, arg2)
Incorrect examples:
method_name ( arg1, arg2) or method_name arg1 agr2
Exceptions: require, include, p and attr_* are used without parenthesis.