You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: general/functions.md
+43
Original file line number
Diff line number
Diff line change
@@ -51,3 +51,46 @@ function getMessage(string $name, int $age):string {
51
51
This style is sometimes more readable with simpler data types, and usually creates shorter functions. Either way, ensure you make a decision of what style to use, and maintain consistency throughout your codebase.
52
52
53
53
## A function should only ever do one thing, as indicated by its name
54
+
55
+
The name of a function should reveal its intention. This can lead to more verbosity in your code, but that's a good thing. The length of a function's name is usually inversely proportional to the length of the function, because a more specific function name tends to do less, and a function that does less is always a cleaner decision.
56
+
57
+
Consider two scenarios. The first, a function that returns differently depending on a set of circumstances:
58
+
59
+
```php
60
+
function getMenuItems(string $constraint = null):array {
From a reader's perspective, one needs to read the whole function to understand the context and meaning of the `$constraint` variable. Compare this to the second style, where a separate function describes each constraint:
This second example immediately explains what each function is doing, and means each function can be reduced to a single line of code.
91
+
92
+
Always be as specific as possible with the name of a function, so it describes exactly what it is doing, favouring multiple, longer function names over a single function that does more than one thing.
93
+
94
+
Another rule of thumb is to be wary of words like "and" and "or" in function names, as these indicate that the function does more than one thing.
95
+
96
+
A benefit of this extra verbosity is that your code becomes extra searchable by IDEs. In the above examples, the multiple functions allow developers to search for functions containing the word "vegetarian", allowing for much faster navigation through the code.
0 commit comments