Coding Style

    Type names are PascalCased. For example:

    Method names are snake_cased. For example:

    1. def first_name
    2. end
    3. def date_of_birth
    4. end
    5. def homepage_url
    6. end

    Variable names are snake_cased. For example:

    1. LUCKY_NUMBERS = [3, 7, 11]
    2. 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 with crystal 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:

    1. module Money
    2. CURRENCIES = {
    3. "EUR" => 1.0,
    4. "ARS" => 10.55,
    5. "USD" => 1.12,
    6. "JPY" => 134.15,
    7. }
    8. getter :currency, :value
    9. def initialize(@currency, @value)
    10. end
    11. end
    12. class CurrencyConversion
    13. def initialize(@amount, @target_currency)
    14. end
    15. def amount
    16. # implement conversion ...
    17. end
    18. end