Code conventions
This are the official programming standards Cherrycake follows.
Cherrycake uses the JavaScript conventions for entity naming, file naming and code structure. Of course, you can use your favorite conventions in your Cherrycake app.

Entities

  • camelCase for variables, properties and methods.
  • PascalCase for types, classes and modules.
  • UPPER_CASE_SNAKE_CASE for constants.
For example:
1
define("DEFINE_NAME", true);
2
3
$variableName = null;
4
5
class ClassName {
6
var $propertyName;
7
function methodName() {
8
[...]
9
}
10
}
11
12
$objectName = new ClassName;
Copied!

File naming

Class names, module names and directories follow the same PascalCase convention, take a look at the Files structure section for more information.
Please note that class auto-loading might not work properly if you don't follow this naming conventions.

Code structure

Feel free to use the structure methodology you prefer in your code. Cherrycake's own choice is tabs instead of spaces and a somewhat typical multi-line approach to function declarations and overall tidiness.
Here's an example code with some of this conventions in place:
1
<?php // Use the full PHP opening tag instead of the shorthand <? for compatibility
2
3
class ClassName { // Brackets on the same line
4
var $propertyName = 1 + 3; // Operators surrounded by spaces
5
// Empty lines between logical blocks are acceptable to add readability
6
/**
7
* PHPDoc is used to document code when needed.
8
* @param boolean $divisor The divisor.
9
* @return boolean True on success, false on failure.
10
*/
11
function methodName($divisor) { // No separation between the method/function name and the parenthesis
12
global $e; // The Cherrycake engine is stored globally in the $e variable
13
14
if ($this->propertyName > 100) // Control structures like if, for, while and switch are one space away from the parenthesis
15
return false; // Single-line statements without brackets are acceptable, as long as they improve readability
16
17
for ($i = 1; $i < 100; $i ++) // Space after semicolons in for control structures
18
if ($i % $divisor == 0) // Nested control structures without brackets are acceptable, as long as they improve readability
19
echo "$i is divisible by $divisor"; // Variable interpolation in strings is acceptable when it adds readability
20
21
$e->ModuleName->methodName(
22
$this->propertyName, // Multiline tabbed parameters are acceptable to add readability when the number of parameters is long
23
[
24
"id" => $this->propertyName,
25
"key" => [
26
"a" => true, // Depth of multidimensional arrays is also expressed with tabs
27
"b" => 12
28
]
29
] // Specially when functions receive complex parameters like this hash array
30
);
31
32
return true;
33
}
34
}
35
36
// No need to use the ?> PHP closing tag
Copied!
If you're contributing to the Cherrycake Engine, please follow this conventions.
Last modified 1yr ago