Coding Style
This style is used in the standard library. You can use it in your own project to make it familiar to other developers.
Naming
Type names are PascalCased. For example:
class ParseError < Exception
end
trait Abcd
end
interface Something
end
enum Time
end
Method names are camelCased. For example:
class Person
def firstName()
end
def dateOfBirth()
end
def homepageUrl()
end
end
Variable names are camelCased. For example:
class Greeting
def @defaultGreeting = "Hello world"
def customGreeting = null
def new(customGreeting = null)
@customGreeting = customGreeting
end
def printGreeting()
greeting = @customGreeting or @@defaultGreeting
trace(greeting)
end
end
Constants are screaming-cased. For example:
def LUCKY_NUMBERS = [3, 7, 11]
def GOOGLE_URL = "https://google.com"
Acronyms
In class names, acronyms are all-uppercase. For example, HTTP
, and LibXML
.
In method names, acronyms are camelCase. For example #fromJson
, #toIo
.
Directory and File Names
Within a project:
/
contains a readme, any project configurations (eg, CI or editor configs), and any other project-level documentation (eg, changelog or contributing guide).<your-project-name/
contains the project's source code.bin/
contains any executables.
File paths match the namespace of their contents. Files are named after the class or namespace they define.
For example, myproject.MyModule
is defined in myproject/MyModule.rx
.
Whitespace
Use two spaces to indent code inside namespaces, methods, blocks or other nested contexts. For example:
class Parser
def parse(scoreText)
try
# Parse something
catch(err : ParseError)
# handle error ...
end
end
end
Within a class, separate method definitions and constants with one newline. For example:
class Money
def @CURRENCIES = {
["EUR"] = 1.0,
["ARS"] = 10.55,
["USD"] = 1.12,
["JPY"] = 134.15,
}
def new(currency, value)
end
def amount()
# implement conversion ...
end
end