Skip to content

Instantly share code, notes, and snippets.

@awwsmm
Created November 29, 2017 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awwsmm/276efa4ec58bdf249ba74d200a4349e8 to your computer and use it in GitHub Desktop.
Save awwsmm/276efa4ec58bdf249ba74d200a4349e8 to your computer and use it in GitHub Desktop.
My Scala syntax conventions
////////////////////////////////////////////////////////////////////////////////
///
/// MY SCALA SYNTAX CONVENTIONS
///
///
/// - "_=" convention for getters, _-prepended class constructor arguments, __-prepended local variables
/// - lowerCamelCase for variable/method names, UpperCamelCase for class names
/// - all local variables are private by default
/// - setter arguments use newValue format (lowerCamelCase with prepended "new")
/// - all classes have __className immutable with String name of class, along with className getter
/// - all var/val/def/class have explicit types or "T"
/// - all def use brackets and are spread out over minimum four lines (see def myName)
/// - spaces between "def", methodName, method arguments, and return type
/// - NO tabs. 2 spaces between levels
/// - "//////" section dividers for different classes defined within the same file
/// - "///===" section dividers for local variables, getters, setters, extending out to exactly 80 characters
/// - "///---" for lower-level dividers
/// - "///..." for all other dividers
/// - ALL def's (except getters) should have an "if (trace)" block for debugging purposes
/// - all classes and setters should return TRUE if successful, FALSE if not
/// - "///" for highlighted comments, "//" for grey comments
///
///
/// minimal example:
/// (trace and tracer should be defined globally for all classes in package)
val trace = false // set to true to see method trace during execution
def tracer (methodName: String, className: String, inputValues: List[Any], outputValues: List[Any]) = {
if (methodName == "") { // classes
print(f" + $className( $inputValues )\n")
} else { // methods
print(f" | $className.$methodName: $inputValues => $outputValues\n")
}
}
////////////////////////////////////////////////////////////////////////////////
///
/// MyClass | 29.11.17 | Author Name
///
/// Short description of class. Date above should be *created* date, not last
/// updated date. Github will keep track of last updated date.
///
////////////////////////////////////////////////////////////////////////////////
class MyClass (_myName: String, _myType: String) {
private val __className: String = "MyClass"
if (trace) tracer("", className, List(_myName, _myType), List())
///===========================================================================
/// variables
///===========================================================================
private var __myName: String = _myName
private var __myType: String = _myType
///===========================================================================
/// getters
///===========================================================================
def className: String = {
// don't put a tracer() in className() unless you want an infinite loop
__className
}
def myName: String = {
if (trace) tracer("myName", className, List(), List(__myName))
__myName
}
def myType: String = {
if (trace) tracer("myType", className, List(), List(__myType))
__myType
}
///===========================================================================
/// setters
///===========================================================================
def myName_= (newName: String): Boolean = {
__myName = newName
if (trace) tracer("myName_=", className, List(newName), List(true))
true
}
def myType_= (newType: String): Boolean = {
__myType = newType
if (trace) tracer("myType_=", className, List(newType), List(true))
true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment