Skip to content

Commit

Permalink
Getters/setters on the contribution guidelines (#54384)
Browse files Browse the repository at this point in the history
Adds recommendations about getters and setters to the contribution guidelines.
Does two minor tweaks on other topics, for clarity mostly.
Rohesie authored Oct 15, 2020
1 parent 589469c commit 68a27ed
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -95,7 +95,7 @@ The previous code made compliant:
/datum/datum1/datum2/proc/proc3()
code
/datum/datum1/datum2/proc2()
..()
. = ..()
code
```

@@ -181,7 +181,7 @@ This is clearer and enhances readability of your code! Get used to doing it!
### Control statements
(if, while, for, etc)

* All control statements must not contain code on the same line as the statement (`if (blah) return`)
* No control statement may contain code on the same line as the statement (`if (blah) return`)
* All control statements comparing a variable to a number should use the formula of `thing` `operator` `number`, not the reverse (eg: `if (count <= 10)` not `if (10 >= count)`)

### Use early return
@@ -230,6 +230,47 @@ This is good:
mob.dothing()
````

### Getters and setters

* Avoid getter procs. They are useful tools in languages with that properly enforce variable privacy and encapsulation, but DM is not one of them. The upfront cost in proc overhead is met with no benefits, and it may tempt to develop worse code.

This is bad:
```DM
/datum/datum1/proc/simple_getter()
return gotten_variable
```
Prefer to either access the variable directly or use a macro/define.


* Make usage of variables or traits, set up through condition setters, for a more maintainable alternative to compex and redefined getters.

These are bad:
```DM
/datum/datum1/proc/complex_getter()
return condition ? VALUE_A : VALUE_B
/datum/datum1/child_datum/complex_getter()
return condition ? VALUE_C : VALUE_D
```

This is good:
```DM
/datum/datum1
var/getter_turned_into_variable
/datum/datum1/proc/set_condition(new_value)
if(condition == new_value)
return
condition = new_value
on_condition_change()
/datum/datum1/proc/on_condition_change()
getter_turned_into_variable = condition ? VALUE_A : VALUE_B
/datum/datum1/child_datum/on_condition_change()
getter_turned_into_variable = condition ? VALUE_C : VALUE_D
```


### Develop Secure Code

0 comments on commit 68a27ed

Please sign in to comment.