<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'When Does Terse Code Become Bad?' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'When Does Terse Code Become Bad?' posted on the 'C and C++' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2012 Programmers Heaven</copyright>
    <pubDate>Thu, 09 Feb 2012 08:46:34 -0800</pubDate>
    <lastBuildDate>Thu, 09 Feb 2012 08:46:34 -0800</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>When Does Terse Code Become Bad?</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/367172/367172/when-does-terse-code-become-bad/</link>
      <description>I am thinking of writing a post on my blog soon to talk about the difference between code that is concise and code that is so terse that it becomes less readable or maintainable. My example of this would be from a while ago when I had to write a routine that checked if a string contained all hex characters. The terse version looked like this:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;bool IsHexString(char *sz) 
{ 
        while ( isxdigit(*sz) &amp;amp;&amp;amp; *++sz != NULL ) {} 
        return *sz == NULL; 
}&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Much as I liked the “smallness” of my routine, in the end I coded it as follows because I thought it was more readable:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;bool IsStringHex(char *sz) 
{ 
        char* ch = sz; 
        while ( *ch != NULL ) 
        { 
                if ( !isxdigit (*ch) ) 
                { 
                        return false; 
                } 
                ch++; 
        } 
        return true; 
} &lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Does anyone else have a view on this? Do you have examples of routines that you have written or found that were so terse they were practically indecipherable?&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/367172/367172/when-does-terse-code-become-bad/</guid>
      <pubDate>Sat, 10 Nov 2007 05:20:24 -0800</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: When Does Terse Code Become Bad?</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/367172/367173/re-when-does-terse-code-become-bad/#367173</link>
      <description>It is worth mentioning that "obfuscated code" as the one in your first example runs &lt;em&gt;exactly as fast&lt;/em&gt; and consumes &lt;em&gt;exactly as much memory&lt;/em&gt; as the cleaner version in your second example.&lt;br /&gt;
&lt;br /&gt;
The difference is HD space. The first version of the source will take up less space on your disk (that is, the source code, not the executable). Back in the dinosaur days, people had tiny disks and therefore wrote extremely compact code. So if your computer is from the mid 80s, it is motivated. Otherwise, it isn't.</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/367172/367173/re-when-does-terse-code-become-bad/#367173</guid>
      <pubDate>Sat, 10 Nov 2007 05:52:34 -0800</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: When Does Terse Code Become Bad?</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/367172/367174/re-when-does-terse-code-become-bad/#367174</link>
      <description>: &lt;pre class="sourcecode"&gt;: bool IsStringHex(char *sz) 
: { 
:         char* ch = sz; 
:         while ( *ch != NULL ) 
:         { 
:                 if ( !isxdigit (*ch) ) 
:                 { 
:                         return false; 
:                 } 
:                 ch++; 
:         } 
:         return true; 
: } &lt;/pre&gt;: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Regarding the example above: you shouldn't compare with NULL. NULL is a strange and magic C constant used only for pointers. It is usually, but not necessarily, defined as&lt;br /&gt;
&lt;br /&gt;
#define NULL ((void*)0)&lt;br /&gt;
&lt;br /&gt;
In C++, pointers are set to 0 instead.&lt;br /&gt;
&lt;br /&gt;
However, in your example you are looking for the "null termination". This should be '\0', 0 or '\x00'. The difference is that NULL may not always be 0, while null termination is always 0. To avoid mixing them up, null termination is sometimes referred to as "NUL", especially in ASCII tables.</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/367172/367174/re-when-does-terse-code-become-bad/#367174</guid>
      <pubDate>Sat, 10 Nov 2007 05:59:17 -0800</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: When Does Terse Code Become Bad?</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/367172/367178/re-when-does-terse-code-become-bad/#367178</link>
      <description>: : &lt;pre class="sourcecode"&gt;: : bool IsStringHex(char *sz) 
: : { 
: :         char* ch = sz; 
: :         while ( *ch != NULL ) 
: :         { 
: :                 if ( !isxdigit (*ch) ) 
: :                 { 
: :                         return false; 
: :                 } 
: :                 ch++; 
: :         } 
: :         return true; 
: : } &lt;/pre&gt;: : &lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: Blue;"&gt;&lt;br /&gt;
Not bad. However, why copy 'sz' into 'ch' - why not use 'sz' right in the while() loop?&lt;br /&gt;
&lt;br /&gt;
Also, the contents of the 'ch' pointer (I mean *ch thing) taken twice inside the loop. You have to be sure that compiler will optimize it. I would have done it like this:&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
bool IsStringHex (char *sz) 
: : { 
: :         int c; 
: :         while ( (c = *sz++) != '\0' ) 
: :         { 
: :                 if ( !isxdigit (c) ) 
: :                 { 
: :                         return false; 
: :                 } 
: :         } 
: :         return true; 
&lt;/pre&gt;&lt;br /&gt;
&lt;span style="color: Blue;"&gt;&lt;br /&gt;
From professional point of view I can say that I simply can't stand the code, which does not have any blank lines. No matter how good the code is or that it has comments, I can't help but hate this style:&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
// comment
code line
code line
code line
if ()
{
  code line
}
// comment
code line
code line
// comment
code line
code line
code line
// comment
code line
code line
// comment
code line
&lt;/pre&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/367172/367178/re-when-does-terse-code-become-bad/#367178</guid>
      <pubDate>Sat, 10 Nov 2007 17:25:45 -0800</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: When Does Terse Code Become Bad?</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/367172/367200/re-when-does-terse-code-become-bad/#367200</link>
      <description>Since we are now on the sensitive topic of coding style, it is worth mentioning that there are no absolute truths. However, the vast majority of programmers dislike code with multiple operators on one line as in the first example, as well as code written without any blank lines. &lt;br /&gt;
&lt;br /&gt;
Personally I dislike assignment inside the loop condition, or inside an if() statement. Because strictly, if() and while() should only evaluate boolean expressions and nothing else. Null termination and "false" both have the integer value 0, so the code will of course work, even though I wouldn't have written it like that. Avoiding assignment in conditions also eliminates any unintentional mix up between = and ==.&lt;br /&gt;
&lt;br /&gt;
I would have written the function like this:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;bool isStringHex (char* sz)
{
  bool result = true;

  while(*sz != '\0')
  {
    if( !isxdigit(*sz) )
    {
      result = false;
      break;
    }

    sz++;
  }

  return result;
}&lt;/pre&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/367172/367200/re-when-does-terse-code-become-bad/#367200</guid>
      <pubDate>Mon, 12 Nov 2007 01:19:42 -0800</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: When Does Terse Code Become Bad?</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/367172/367204/re-when-does-terse-code-become-bad/#367204</link>
      <description>: Since we are now on the sensitive topic of coding style, it is worth &lt;br /&gt;
: mentioning that there are no absolute truths. However, the vast &lt;br /&gt;
: majority of programmers dislike code with multiple operators on one &lt;br /&gt;
: line as in the first example, as well as code written without any &lt;br /&gt;
: blank lines. &lt;br /&gt;
: &lt;br /&gt;
: Personally I dislike assignment inside the loop condition, or inside &lt;br /&gt;
: an if() statement. Because strictly, if() and while() should only &lt;br /&gt;
: evaluate boolean expressions and nothing else. Null termination and &lt;br /&gt;
: "false" both have the integer value 0, so the code will of course &lt;br /&gt;
: work, even though I wouldn't have written it like that. Avoiding &lt;br /&gt;
: assignment in conditions also eliminates any unintentional mix up &lt;br /&gt;
: between = and ==.&lt;br /&gt;
: &lt;br /&gt;
: I would have written the function like this:&lt;br /&gt;
: &lt;br /&gt;
: &lt;pre class="sourcecode"&gt;: bool isStringHex (char* sz)
: {
:   bool result = true;
: 
:   while(*sz != '\0')
:   {
:     if( !isxdigit(*sz) )
:     {
:       result = false;
:       break;
:     }
: 
:     sz++;
:   }
: 
:   return result;
: }&lt;/pre&gt;: &lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: Blue;"&gt;&lt;br /&gt;
I agree about the assignment in statements, but only if there is only assignment. I often do that, but always together with logical operator.&lt;br /&gt;
&lt;/span&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/367172/367204/re-when-does-terse-code-become-bad/#367204</guid>
      <pubDate>Mon, 12 Nov 2007 05:52:51 -0800</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: When Does Terse Code Become Bad?</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/367172/368966/re-when-does-terse-code-become-bad/#368966</link>
      <description>Hi everyone - thank you so much for giving me your thoughts. It has taken me a long time to get around to posting about this on my blog but I finally have done in my post &lt;a href="http://outofthetriangle.wordpress.com/2008/01/27/terse-is-worse/"&gt;Terse is Worse?&lt;/a&gt; Once again thank you all for your input - I definitely learned something from it. &lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/367172/368966/re-when-does-terse-code-become-bad/#368966</guid>
      <pubDate>Sun, 27 Jan 2008 15:35:23 -0800</pubDate>
      <category>C and C++</category>
    </item>
  </channel>
</rss>
