Code Positivity
Jump to navigation
Jump to search
This page provides some insights into what it means to try and write code in a positive way, with the least negation as possible.
Overview
Code negation is (usually) using not (in SQF/SQS) or ! (in both SQF/SQS and Enforce Script); all in all, it is about searching for the opposite of what one wants. Examples:
SQF | Enforce Script |
---|---|
if (value < minValue)
{
Print("Value is invalid");
}
else
{
Print("Value is valid");
} |
Research[1] has[2] shown[3] that negation in general (not just in code but also in reading, teaching etc) makes processing sentences more difficult for the human brain, making its message less impactful (although not in all cases[4]).
Examples
Do | Don't |
---|---|
// whenever possible without rewriting everything or doubling the methods, of course
if (unit.IsAlive() && unit.IsInjured())
DoHealThing(); // alternatively
if (unit.IsDead())
DoDeathThing();
else if (unit.IsPerfectHealth())
DoPerfectThing();
else // injured
DoHealThing(); |
// sometimes, only negative methods are available
if (!unit.IsPerfectHealth() && !unit.IsDead())
DoHealThing(); |
Counter Examples
As most if not all rules of development, this rule is a rule of thumb that is not to be enforced at all cost.
Don't | Do |
---|---|
if (m_aElements.Count() > 0)
Print("There are elements!");
// note that in some cases, storing the count in a variable
// saves further calculations in below code |
if (!m_aElements.IsEmpty())
Print("There are elements!"); |
See Also
- ↑ https://www.researchgate.net/publication/10733819_Effects_of_Negation_and_Situational_Presence_on_the_Accessibility_of_Text_Information
- ↑ https://www.researchgate.net/publication/221438570_Some_Issues_on_Detecting_Negation_from_Text
- ↑ https://medium.com/@Cuadraman/why-to-stop-writting-negavite-code-af5ffb17195
- ↑ https://www.psychologicalscience.org/news/releases/true-or-false-how-our-brain-processes-negative-statements.html