Coding Style
Type names are PascalCased. For example:
Method names are snake_cased. For example:
def first_name
end
def date_of_birth
end
def homepage_url
end
Variable names are snake_cased. For example:
LUCKY_NUMBERS = [3, 7, 11]
DOCUMENTATION_URL = "http://crystal-lang.org/docs"
In class names, acronyms are all-uppercase. For example, HTTP
, and LibXML
.
In method names, acronyms are all-lowercase. For example #from_json
, #to_io
.
Lib
names are prefixed with . For example: LibC
, LibEvent2
.
/
contains a readme, any project configurations (eg, CI or editor configs), and any other project-level documentation (eg, changelog or contributing guide).src/
contains the project’s source code.spec/
contains the project’s specs, which can be run withcrystal spec
.bin/
contains any executables.
File paths match the namespace of their contents. Files are named after the class or namespace they define, with snake_case.
For example, HTTP::WebSocket
is defined in src/http/web_socket.cr
.
Indentation
Use two spaces to indent code inside namespaces, methods, blocks or other nested contexts. For example:
module Money
CURRENCIES = {
"EUR" => 1.0,
"ARS" => 10.55,
"USD" => 1.12,
"JPY" => 134.15,
}
getter :currency, :value
def initialize(@currency, @value)
end
end
class CurrencyConversion
def initialize(@amount, @target_currency)
end
def amount
# implement conversion ...
end
end