{"name":"Code Positivity","title":"Code Positivity","type":"topic","url":"https://community.bistudio.com/wiki/Code_Positivity","revid":376868,"updated":"2025-12-09T16:34:16Z","aliases":[],"markdown":"This page provides some insights into what it means to try and write code in a positive way, with the least negation as possible.\n\n### Overview\n\nCode 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:\n\n| SQF | Enforce Script |\n|---|---|\n| ```sqf<br>if (not alive player) then<br>{<br>    hint \"you are dead\";<br>}<br>else<br>{<br>    hint \"you are alive\";<br>};<br>``` | ```enforce<br>if (value < minValue)<br>{<br>    Print(\"Value is invalid\");<br>}<br>else<br>{<br>    Print(\"Value is valid\");<br>}<br>``` |\n\nResearch (ref: https://www.researchgate.net/publication/10733819_Effects_of_Negation_and_Situational_Presence_on_the_Accessibility_of_Text_Information)\nhas (ref: https://www.researchgate.net/publication/221438570_Some_Issues_on_Detecting_Negation_from_Text)\nshown (ref: https://medium.com/@Cuadraman/why-to-stop-writting-negavite-code-af5ffb17195)\nthat **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\n(although not in all cases (ref: https://www.psychologicalscience.org/news/releases/true-or-false-how-our-brain-processes-negative-statements.html)).\n\n### Examples\n\n| Do | Don't |\n|---|---|\n| ```sqf<br>// this is called Early Return / Bouncer pattern<br>if (isNull _control1) exitWith {};<br>if (isNull _control2) exitWith {};<br>if (isNull _control3) exitWith {};<br>// do something<br>``` | ```sqf<br>if (not isNull _control1) then<br>{<br>    if (not isNull _control2) then<br>    {<br>        if (not isNull _control3) then<br>        {<br>            // finally do something<br>            // <- this is called Hadouken code style as per<br>            // the shape drawn by the brackets<br>        };<br>    };<br>};<br>``` |\n| ```enforce<br>// whenever possible without rewriting everything<br>// or doubling the methods, of course<br>if (unit.IsAlive() && unit.IsInjured())<br>    DoHealThing();<br>```<br>```enforce<br>// alternatively<br>if (unit.IsDead())<br>    DoDeathThing();<br>else if (unit.IsPerfectHealth())<br>    DoPerfectThing();<br>else // injured<br>    DoHealThing();<br>``` | ```enforce<br>// sometimes, only negative methods are available<br>if (!unit.IsPerfectHealth() && !unit.IsDead())<br>    DoHealThing();<br>``` |\n\n### Counter Examples\n\nAs most if not all rules of development, this rule is a rule of thumb that is **not** to be enforced at all cost.\n\n| Don't | Do |\n|---|---|\n| ```sqf<br>if (isNull _control) then<br>{<br>}<br>else<br>{<br>    systemChat \"the control exists\";<br>};<br>// or, even more convoluted and less performance-friendly<br>if (str _control find \"NULL\" == -1) then<br>{<br>    systemChat \"the control exists\";<br>};<br>``` | ```sqf<br>if (not isNull _control) then // neater<br>{<br>    systemChat \"the control exists\";<br>};<br>``` |\n| ```enforce<br>if (m_aElements.Count() > 0)<br>    Print(\"There are elements!\");<br>// note that in some cases, storing the count in a variable<br>// saves further calculations in below code<br>``` | ```enforce<br>if (!m_aElements.IsEmpty())<br>    Print(\"There are elements!\");<br>``` |\n\n### See Also\n\n- Code Optimisation\n- Mission Optimisation\n- Multiplayer Scripting","sections":[{"heading":null,"level":1,"markdown":"This page provides some insights into what it means to try and write code in a positive way, with the least negation as possible."},{"heading":"Overview","level":3,"markdown":"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:\n\n| SQF | Enforce Script |\n|---|---|\n| ```sqf<br>if (not alive player) then<br>{<br>    hint \"you are dead\";<br>}<br>else<br>{<br>    hint \"you are alive\";<br>};<br>``` | ```enforce<br>if (value < minValue)<br>{<br>    Print(\"Value is invalid\");<br>}<br>else<br>{<br>    Print(\"Value is valid\");<br>}<br>``` |\n\nResearch (ref: https://www.researchgate.net/publication/10733819_Effects_of_Negation_and_Situational_Presence_on_the_Accessibility_of_Text_Information)\nhas (ref: https://www.researchgate.net/publication/221438570_Some_Issues_on_Detecting_Negation_from_Text)\nshown (ref: https://medium.com/@Cuadraman/why-to-stop-writting-negavite-code-af5ffb17195)\nthat **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\n(although not in all cases (ref: https://www.psychologicalscience.org/news/releases/true-or-false-how-our-brain-processes-negative-statements.html))."},{"heading":"Examples","level":3,"markdown":"| Do | Don't |\n|---|---|\n| ```sqf<br>// this is called Early Return / Bouncer pattern<br>if (isNull _control1) exitWith {};<br>if (isNull _control2) exitWith {};<br>if (isNull _control3) exitWith {};<br>// do something<br>``` | ```sqf<br>if (not isNull _control1) then<br>{<br>    if (not isNull _control2) then<br>    {<br>        if (not isNull _control3) then<br>        {<br>            // finally do something<br>            // <- this is called Hadouken code style as per<br>            // the shape drawn by the brackets<br>        };<br>    };<br>};<br>``` |\n| ```enforce<br>// whenever possible without rewriting everything<br>// or doubling the methods, of course<br>if (unit.IsAlive() && unit.IsInjured())<br>    DoHealThing();<br>```<br>```enforce<br>// alternatively<br>if (unit.IsDead())<br>    DoDeathThing();<br>else if (unit.IsPerfectHealth())<br>    DoPerfectThing();<br>else // injured<br>    DoHealThing();<br>``` | ```enforce<br>// sometimes, only negative methods are available<br>if (!unit.IsPerfectHealth() && !unit.IsDead())<br>    DoHealThing();<br>``` |"},{"heading":"Counter Examples","level":3,"markdown":"As most if not all rules of development, this rule is a rule of thumb that is **not** to be enforced at all cost.\n\n| Don't | Do |\n|---|---|\n| ```sqf<br>if (isNull _control) then<br>{<br>}<br>else<br>{<br>    systemChat \"the control exists\";<br>};<br>// or, even more convoluted and less performance-friendly<br>if (str _control find \"NULL\" == -1) then<br>{<br>    systemChat \"the control exists\";<br>};<br>``` | ```sqf<br>if (not isNull _control) then // neater<br>{<br>    systemChat \"the control exists\";<br>};<br>``` |\n| ```enforce<br>if (m_aElements.Count() > 0)<br>    Print(\"There are elements!\");<br>// note that in some cases, storing the count in a variable<br>// saves further calculations in below code<br>``` | ```enforce<br>if (!m_aElements.IsEmpty())<br>    Print(\"There are elements!\");<br>``` |"},{"heading":"See Also","level":3,"markdown":"- Code Optimisation\n- Mission Optimisation\n- Multiplayer Scripting"}],"categories":["Arma Scripting Tutorials"],"groups":["Scripting"]}