{"id":35,"date":"2017-05-31T19:16:00","date_gmt":"2017-05-31T19:16:00","guid":{"rendered":""},"modified":"2019-03-12T20:10:16","modified_gmt":"2019-03-12T20:10:16","slug":"do-not-use-boolean-parameters","status":"publish","type":"post","link":"https:\/\/ntlx.org\/de\/2017\/05\/do-not-use-boolean-parameters.html","title":{"rendered":"Do Not Use Boolean Parameters"},"content":{"rendered":"<p>I&#8217;m absolutely serious, don&#8217;t do it. &#8220;But hey&#8221;, one may ask, &#8220;what&#8217;s wrong with boolean parameters?&#8221; That&#8217;s what I got asked recently when I did a code review. And to all of you who have the same question in mind, here is my answer.<br \/>There is only one reason to use a boolean parameter in a function interface, and that&#8217;s when it&#8217;s the only parameter &#8211; in a setter function (and even there one could argue to not use it).<\/p>\n<h2>Unreadable Function Calls<\/h2>\n<p>Let&#8217;s take a look at a first example:<br \/><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"> 1<br \/> 2<br \/> 3<br \/> 4<br \/> 5<br \/> 6<br \/> 7<br \/> 8<br \/> 9<br \/>10<br \/>11<br \/>12<\/pre>\n<\/td>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #557799;\">#include &lt;iostream&gt;<\/span><br \/><span style=\"color: #557799;\">#include \"concat.h\"<\/span><br \/><span style=\"color: #557799;\">#include &lt;string&gt;<\/span><br \/><span style=\"color: #557799;\">#include &lt;vector&gt;<\/span><br \/><br \/><span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span> ()<br \/>{<br \/>    Concat concat;<br \/>    std<span style=\"color: #333333;\">::<\/span>vector<span style=\"color: #333333;\">&lt;<\/span><span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>string<span style=\"color: #333333;\">&gt;<\/span> values {<span style=\"background-color: #fff0f0;\">\"Line One\"<\/span>, <span style=\"background-color: #fff0f0;\">\"Line Two\"<\/span>, <span style=\"background-color: #fff0f0;\">\"Line Three\"<\/span>};<br \/>    std<span style=\"color: #333333;\">::<\/span>cout <span style=\"color: #333333;\">&lt;&lt;<\/span> concat.concatVector(values, <span style=\"color: #007020;\">false<\/span>, <span style=\"color: #007020;\">true<\/span>);<br \/>    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;<br \/>}<br \/><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>In line 10 you can see a call to the function concatVector which seems to be used to concat all the values of a vector to a single string. But what are those 2 boolean parameters passed? To find out what they are used for you have to take a look at the declaration of that function. (Or, depending on the IDE you are using, at least hover over the function call to see the names of that parameters, while hoping the names are speaking) Here is the corresponding header file:<br \/><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\">1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<\/pre>\n<\/td>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #557799;\">#include &lt;vector&gt;<\/span><br \/><span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Concat<\/span> {<br \/>    <span style=\"color: #997700; font-weight: bold;\">public:<\/span><br \/>        <span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>string concatVector(<span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>vector<span style=\"color: #333333;\">&lt;<\/span><span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>string<span style=\"color: #333333;\">&gt;<\/span> strings, <span style=\"color: #008800; font-weight: bold;\">const<\/span> <span style=\"color: #333399; font-weight: bold;\">bool<\/span> unixStyle, <span style=\"color: #008800; font-weight: bold;\">const<\/span> <span style=\"color: #333399; font-weight: bold;\">bool<\/span> endWithNewline);<br \/>    <span style=\"color: #997700; font-weight: bold;\">private:<\/span><br \/>        <span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>string newLine(<span style=\"color: #008800; font-weight: bold;\">const<\/span> <span style=\"color: #333399; font-weight: bold;\">bool<\/span> unixStyle);<br \/>};<br \/><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Okay. Now we know the first one is used to tell the function if it should use unix-style new lines to separate the strings or DOS-style new lines. The second one tells if the string should end with a newline. Took already some time, and it will take that time again when you want to understand it a week later, because you won&#8217;t remember at least the order of those parameters.<br \/>One first step towards better readability of that code would be to store the parameters first to local variables, so the reader of the code knows what they are used for.<br \/><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"> 1<br \/> 2<br \/> 3<br \/> 4<br \/> 5<br \/> 6<br \/> 7<br \/> 8<br \/> 9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<\/pre>\n<\/td>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #557799;\">#include &lt;iostream&gt;<\/span><br \/><span style=\"color: #557799;\">#include \"concat.h\"<\/span><br \/><span style=\"color: #557799;\">#include &lt;string&gt;<\/span><br \/><span style=\"color: #557799;\">#include &lt;vector&gt;<\/span><br \/><br \/><span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span> ()<br \/>{<br \/>    Concat concat;<br \/>    std<span style=\"color: #333333;\">::<\/span>vector<span style=\"color: #333333;\">&lt;<\/span><span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>string<span style=\"color: #333333;\">&gt;<\/span> values {<span style=\"background-color: #fff0f0;\">\"Line One\"<\/span>, <span style=\"background-color: #fff0f0;\">\"Line Two\"<\/span>, <span style=\"background-color: #fff0f0;\">\"Line Three\"<\/span>};<br \/>    <span style=\"color: #333399; font-weight: bold;\">bool<\/span> useUnixStyle <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">false<\/span>;<br \/>    <span style=\"color: #333399; font-weight: bold;\">bool<\/span> endWithNewLine <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">true<\/span>;<br \/>    std<span style=\"color: #333333;\">::<\/span>cout <span style=\"color: #333333;\">&lt;&lt;<\/span> concat.concatVector(values, useUnixStyle, endWithNewLine);<br \/>    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;<br \/>}<br \/><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Okay, now we can at least understand what the code does, without visiting the header or implementation of that function. Great.<\/p>\n<p>Even better would be to make them member of the Concat class, so the user of the class is forced to explicitly spell the usage in the client code:<br \/><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"> 1<br \/> 2<br \/> 3<br \/> 4<br \/> 5<br \/> 6<br \/> 7<br \/> 8<br \/> 9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<\/pre>\n<\/td>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #557799;\">#include &lt;iostream&gt;<\/span><br \/><span style=\"color: #557799;\">#include \"concat.h\"<\/span><br \/><span style=\"color: #557799;\">#include &lt;string&gt;<\/span><br \/><span style=\"color: #557799;\">#include &lt;vector&gt;<\/span><br \/><br \/><span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span> ()<br \/>{<br \/>    Concat concat;<br \/>    std<span style=\"color: #333333;\">::<\/span>vector<span style=\"color: #333333;\">&lt;<\/span><span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>string<span style=\"color: #333333;\">&gt;<\/span> values {<span style=\"background-color: #fff0f0;\">\"Line One\"<\/span>, <span style=\"background-color: #fff0f0;\">\"Line Two\"<\/span>, <span style=\"background-color: #fff0f0;\">\"Line Three\"<\/span>};<br \/>    concat.setUseUnixStyle(<span style=\"color: #007020;\">false<\/span>);<br \/>    concat.setEndWithNewline(<span style=\"color: #007020;\">true<\/span>);<br \/>    std<span style=\"color: #333333;\">::<\/span>cout <span style=\"color: #333333;\">&lt;&lt;<\/span> concat.concatVector(values);<br \/>    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;<br \/>}<br \/><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>That way, you even can specify suitable defaults for that boolean parameters without setting them in the function definition (which you also shouldn&#8217;t do, but that&#8217;s a separate topic).<br \/><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"> 1<br \/> 2<br \/> 3<br \/> 4<br \/> 5<br \/> 6<br \/> 7<br \/> 8<br \/> 9<br \/>10<br \/>11<\/pre>\n<\/td>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #557799;\">#include &lt;vector&gt;<\/span><br \/><span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Concat<\/span> {<br \/>    <span style=\"color: #997700; font-weight: bold;\">public:<\/span><br \/>        <span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>string concatVector(<span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>vector<span style=\"color: #333333;\">&lt;<\/span><span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>string<span style=\"color: #333333;\">&gt;&amp;<\/span> strings);<br \/>        <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">setUseUnixStyle<\/span>(<span style=\"color: #333399; font-weight: bold;\">bool<\/span> _useUnixStyle){useUnixStyle <span style=\"color: #333333;\">=<\/span> _useUnixStyle;};<br \/>        <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">setEndWithNewline<\/span>(<span style=\"color: #333399; font-weight: bold;\">bool<\/span> _endWithNewline){endWithNewline <span style=\"color: #333333;\">=<\/span> _endWithNewline;};<br \/>    <span style=\"color: #997700; font-weight: bold;\">private:<\/span><br \/>        <span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>string newLine();<br \/>        <span style=\"color: #333399; font-weight: bold;\">bool<\/span> useUnixStyle {<span style=\"color: #007020;\">false<\/span>};<br \/>        <span style=\"color: #333399; font-weight: bold;\">bool<\/span> endWithNewline {<span style=\"color: #007020;\">false<\/span>};<br \/>};<br \/><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p><\/p>\n<h2>Multi-purpose functions<\/h2>\n<p>Often, boolean parameters are used to specify the behaviour of a function. This indicates that the function in question may serve multiple purposes, which functions should never do. Hence, often it is useful to split those functions up in separate ones, each fulfilling one purpose. This results in a cleaner interface of the functions as well as easier to understand function implementations.<br \/>Here is the current implementation of the Concat class.<br \/><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"> 1<br \/> 2<br \/> 3<br \/> 4<br \/> 5<br \/> 6<br \/> 7<br \/> 8<br \/> 9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<\/pre>\n<\/td>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #557799;\">#include &lt;string&gt;<\/span><br \/><span style=\"color: #557799;\">#include &lt;sstream&gt;<\/span><br \/><span style=\"color: #557799;\">#include \"concat.h\"<\/span><br \/><br \/><span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>string Concat<span style=\"color: #333333;\">::<\/span>concatVector(<span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>vector<span style=\"color: #333333;\">&lt;<\/span><span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>string<span style=\"color: #333333;\">&gt;&amp;<\/span> strings)<br \/>{<br \/>    std<span style=\"color: #333333;\">::<\/span>stringstream str;<br \/>    <span style=\"color: #008800; font-weight: bold;\">for<\/span> (std<span style=\"color: #333333;\">::<\/span>vector<span style=\"color: #333333;\">&lt;<\/span><span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>string<span style=\"color: #333333;\">&gt;::<\/span>const_iterator it <span style=\"color: #333333;\">=<\/span> strings.begin(); it <span style=\"color: #333333;\">!=<\/span> strings.end(); <span style=\"color: #333333;\">++<\/span>it) {<br \/>        <span style=\"color: #008800; font-weight: bold;\">if<\/span> (it <span style=\"color: #333333;\">!=<\/span> strings.begin())<br \/>            str <span style=\"color: #333333;\">&lt;&lt;<\/span> newLine();<br \/>        str <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"color: #333333;\">*<\/span>it;<br \/>    }<br \/>    <span style=\"color: #008800; font-weight: bold;\">if<\/span> (endWithNewline)<br \/>        str <span style=\"color: #333333;\">&lt;&lt;<\/span> newLine();<br \/>    <span style=\"color: #008800; font-weight: bold;\">return<\/span> str.str();<br \/>}<br \/><br \/><span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>string Concat<span style=\"color: #333333;\">::<\/span>newLine() {<br \/>    <span style=\"color: #008800; font-weight: bold;\">if<\/span> (useUnixStyle)<br \/>        <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"background-color: #fff0f0;\">\"<\/span><span style=\"background-color: #fff0f0; color: #666666; font-weight: bold;\">n<\/span><span style=\"background-color: #fff0f0;\">\"<\/span>;<br \/>    <span style=\"color: #008800; font-weight: bold;\">else<\/span><br \/>        <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"background-color: #fff0f0;\">\"<\/span><span style=\"background-color: #fff0f0; color: #666666; font-weight: bold;\">rn<\/span><span style=\"background-color: #fff0f0;\">\"<\/span>;<br \/>}<br \/><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>We now split it up into two separate functions concatVectorWithNewlineEnding and concatVectorWithoutNewlineEnding (one might come up with better function names).<br \/><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"> 1<br \/> 2<br \/> 3<br \/> 4<br \/> 5<br \/> 6<br \/> 7<br \/> 8<br \/> 9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/>34<br \/>35<br \/>36<br \/>37<br \/>38<br \/>39<br \/>40<\/pre>\n<\/td>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #557799;\">#include &lt;string&gt;<\/span><br \/><span style=\"color: #557799;\">#include &lt;sstream&gt;<\/span><br \/><span style=\"color: #557799;\">#include \"concat.h\"<\/span><br \/><br \/><span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>string Concat<span style=\"color: #333333;\">::<\/span>concatVector(<span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>vector<span style=\"color: #333333;\">&lt;<\/span><span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>string<span style=\"color: #333333;\">&gt;&amp;<\/span> strings)<br \/>{<br \/>    <span style=\"color: #008800; font-weight: bold;\">if<\/span> (endWithNewline)<br \/>        <span style=\"color: #008800; font-weight: bold;\">return<\/span> concatVectorWithNewlineEnding(strings);<br \/>    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0066bb; font-weight: bold;\">concatVectorWithoutNewlineEnding<\/span>(strings);<br \/>}<br \/><br \/><span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>string Concat<span style=\"color: #333333;\">::<\/span>concatVectorWithNewlineEnding(<span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>vector<span style=\"color: #333333;\">&lt;<\/span><span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>string<span style=\"color: #333333;\">&gt;&amp;<\/span> strings)<br \/>{<br \/>    <span style=\"color: #008800; font-weight: bold;\">const<\/span> <span style=\"color: #008800; font-weight: bold;\">auto<\/span><span style=\"color: #333333;\">&amp;<\/span> concatWithoutNewline <span style=\"color: #333333;\">=<\/span> concatVectorWithoutNewlineEnding(strings);<br \/>    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0066bb; font-weight: bold;\">appendNewline<\/span>(concatWithoutNewline);<br \/>}<br \/><br \/><span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>string Concat<span style=\"color: #333333;\">::<\/span>concatVectorWithoutNewlineEnding(<span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>vector<span style=\"color: #333333;\">&lt;<\/span><span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>string<span style=\"color: #333333;\">&gt;&amp;<\/span> strings)<br \/>{<br \/>    std<span style=\"color: #333333;\">::<\/span>stringstream str;<br \/>    <span style=\"color: #008800; font-weight: bold;\">for<\/span> (std<span style=\"color: #333333;\">::<\/span>vector<span style=\"color: #333333;\">&lt;<\/span><span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>string<span style=\"color: #333333;\">&gt;::<\/span>const_iterator it <span style=\"color: #333333;\">=<\/span> strings.begin(); it <span style=\"color: #333333;\">!=<\/span> strings.end(); <span style=\"color: #333333;\">++<\/span>it) {<br \/>        <span style=\"color: #008800; font-weight: bold;\">if<\/span> (it <span style=\"color: #333333;\">!=<\/span> strings.begin())<br \/>            str <span style=\"color: #333333;\">&lt;&lt;<\/span> newLine();<br \/>        str <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"color: #333333;\">*<\/span>it;<br \/>    }<br \/>    <span style=\"color: #008800; font-weight: bold;\">return<\/span> str.str();<br \/>}<br \/><br \/><span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>string Concat<span style=\"color: #333333;\">::<\/span>appendNewline(<span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>string<span style=\"color: #333333;\">&amp;<\/span> string)<br \/>{<br \/>    std<span style=\"color: #333333;\">::<\/span>string str(string);<br \/>    <span style=\"color: #008800; font-weight: bold;\">return<\/span> str.append(newLine());<br \/>}<br \/><br \/><span style=\"color: #008800; font-weight: bold;\">const<\/span> std<span style=\"color: #333333;\">::<\/span>string Concat<span style=\"color: #333333;\">::<\/span>newLine() {<br \/>    <span style=\"color: #008800; font-weight: bold;\">if<\/span> (useUnixStyle)<br \/>        <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"background-color: #fff0f0;\">\"<\/span><span style=\"background-color: #fff0f0; color: #666666; font-weight: bold;\">n<\/span><span style=\"background-color: #fff0f0;\">\"<\/span>;<br \/>    <span style=\"color: #008800; font-weight: bold;\">else<\/span><br \/>        <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"background-color: #fff0f0;\">\"<\/span><span style=\"background-color: #fff0f0; color: #666666; font-weight: bold;\">rn<\/span><span style=\"background-color: #fff0f0;\">\"<\/span>;<br \/>}<br \/><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Now we have separate functions, each serving one purpose only, which play well together. Even though we generated more code now than in the beginning, it is much more readable and every function does exactly what its name promises.<\/p>\n<h2>Summary<\/h2>\n<div>Here are the main reasons not to use boolean parameters in your functions:<\/div>\n<div>\n<ol>\n<li>Code which uses your function is easier to read if you find a different way than boolean parameters<\/li>\n<li>Boolean parameters indicate functions with more than one purpose, which you should split up whenever possible<\/li>\n<\/ol>\n<div>I used a simple example to show how you can improve such code. One can imagine, the effect gets even bigger in a huge code base with long and unreadable functions (and a high amount of function parameters).<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"I&#8217;m absolutely serious, don&#8217;t do it. &#8220;But hey&#8221;, one may ask, &#8220;what&#8217;s wrong with boolean parameters?&#8221; That&#8217;s what I got asked recently when I did a code review. And to all of you who have the same question in mind, here is my answer.There is only one reason to use a boolean parameter in a&#8230; <a class=\"view-article\" href=\"https:\/\/ntlx.org\/de\/2017\/05\/do-not-use-boolean-parameters.html\">Artikel ansehen<\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[5],"_links":{"self":[{"href":"https:\/\/ntlx.org\/de\/wp-json\/wp\/v2\/posts\/35"}],"collection":[{"href":"https:\/\/ntlx.org\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ntlx.org\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ntlx.org\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ntlx.org\/de\/wp-json\/wp\/v2\/comments?post=35"}],"version-history":[{"count":1,"href":"https:\/\/ntlx.org\/de\/wp-json\/wp\/v2\/posts\/35\/revisions"}],"predecessor-version":[{"id":71,"href":"https:\/\/ntlx.org\/de\/wp-json\/wp\/v2\/posts\/35\/revisions\/71"}],"wp:attachment":[{"href":"https:\/\/ntlx.org\/de\/wp-json\/wp\/v2\/media?parent=35"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ntlx.org\/de\/wp-json\/wp\/v2\/categories?post=35"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ntlx.org\/de\/wp-json\/wp\/v2\/tags?post=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}