-
Notifications
You must be signed in to change notification settings - Fork 27
Code Formatting
This document mainly concerns itself with CoffeeScript-related formatting. For Scala code, we'll follow any rules from the NetLogo code formatting wiki page. A lot of the same rules from that page are also applicable for CoffeeScript:
- Small Commits
- Line Length Limit of 120
- Whitespace (2 space indents, no tabs)
- String Concatenation
- Naming Things
- Binding Variables for Clarity
Because CoffeeScript uses whitespace to indicate blocks of code, some code formatting is enforced automatically. We also have a CoffeeScript linter setup in both Tortoise and Galapagos that can check for a bunch of common formatting issues. When in doubt, you can always use existing code from the Tortoise engine as a reference. If something is done a certain way in that code it's likely okay (or we need a reminder to clean it up anyway!).
Always use parenthesis to wrap method calls. Good: console.log(myObj)
, bad: console.log myObj
None
The lack of parenthesis can make order of operations very confusing, especially since everything in CoffeeScript is an expression! We have a linter rule to catch this, but it's worth calling out on its own.
- File names should be all lower case. In Galapagos, they are in kebab-case (
widget-controller.coffee
), and in Tortoise, just all lowercase, no punctuation (abstractagentset.coffee
). - Event names (mostly used by Ractive components in Galapagos), should be in kebab-case, like
resize-window
. - Class names should be in PascalCase, as in
WidgetController
orAbstractAgentSet
. - Variable names and method names should be in camelCase.
- When creating a class field or method that you intend to be "private", you can prefix it with
_
to indicate it's not meant for external use, like@_values
or@_setup
.
- When creating a class field or method that you intend to be "private", you can prefix it with
There aren't many good reasons for going against the naming scheme.
Maintaining clarity in naming makes it easier to quickly understand what the code you're reading does, and what the names you're seeing refer to.