Subscribe to Stuck in an Infiniteloop        RSS Feed
-----

A 'C' Myth Dispelled

Icon 14 Comments
Less then five minutes ago, the following was uttered on the forum:

Quote

[it is entirely unreadable because] the code most be in c run not c++ thats why it looks like this


WOAH!

Everyone, everywhere, this is not factual, but oddly enough, widely permeated. Yes C can be cryptic, yes C can be low level, but that does not, I repeat, does not, give one permission to write code that looks like something off of an ancient Egyptian wall.

There's this:

int i,j,x,sum=0,rat=0,s=0,f=0,q,summ=0,p,r,t,m,n,tm;
/*.......*/
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(i!=j)
{if((ku[i][j]!=ku[j][i])||(ku[i][j]==0&&ku[j][i]==0))
tm+=1;}



and then there's this:

while (*s != '\0'){
     *r++ = *s++;
}




The first is just horrible. The second could be considered equally as cryptic, but at least it is self-documenting (and hanging out with C for any extended period of time, it is easily recognizable as a string copy).


One letter variable names outside of loops are horrible. Double letter variable names mm, ii, jj, kk are worse!
Sloppy unreadable code is sloppy unreadable code, regardless of the language.

14 Comments On This Entry

Page 1 of 1

LaFayette Icon

16 May 2010 - 01:07 PM
I'm no C programmer but isn't this how you do a cool string copy:
while(*r++ = *s++);



But you are right. C code isnt inherently hard to read. It's just that there are a lot of C programmers that like to do stuff the cryptic way. Especially when naming variables and functions..
0

Locke Icon

16 May 2010 - 02:45 PM

LaFayette, on 16 May 2010 - 02:07 PM, said:

But you are right. C code isnt inherently hard to read. It's just that there are a lot of C programmers that like to do stuff the cryptic way. Especially when naming variables and functions..


The code is self-obfuscating at that point.

Not to mention a total bitch to edit...
0

alias120 Icon

16 May 2010 - 05:13 PM
Part of the reason why I hate reading C++ tutorials that use C. Half of the time even the author will write very cryptic C that makes me spend more time trying to read the code than understand it.

-alias
0

KYA Icon

16 May 2010 - 06:29 PM

LaFayette, on 16 May 2010 - 01:07 PM, said:

I'm no C programmer but isn't this how you do a cool string copy:
while(*r++ = *s++);



That way is a bit more error prone (but assuming bounds are checked elsewhere, it's more nifty then mine).
0

AdamSpeight2008 Icon

18 May 2010 - 06:44 AM
You must use some arcane usage of the term self documenting that I'm not aware of, to say that this is self documenting.
while (*s != '\0'){
     *r++ = *s++;
}

0

KYA Icon

18 May 2010 - 06:48 AM
Null terminators are only in character strings, we're iterating to the end and copying chars, must be a string copy.
0

NickDMax Icon

18 May 2010 - 07:44 AM
Well null terminators are actually a pretty common C pattern for dealing with arrays of "unknown size". Given that snippet you might be doing a string copy... it is definitely *some kind* of memory copy operation, but there is really no telling without the rest of the code.
0

KYA Icon

18 May 2010 - 09:26 AM
I've never seen the null terminator in other data type arrays.
0

WolfCoder Icon

18 May 2010 - 08:38 PM
There is no such a thing as "self documenting".
0

dorknexus Icon

19 May 2010 - 02:25 PM
When I hear people complain about the readability of C I just say "Could be worse. Could be a Lisp dialect." That usually sends them screaming.
0

depricated Icon

28 May 2010 - 03:53 PM

WolfCoder, on 18 May 2010 - 07:38 PM, said:

There is no such a thing as "self documenting".

Sure there is.

Good variable names would be self documenting.

While the earlier loop was clearly copying a string, that could be made self documenting by a few simple changes

from
while (*s != '\0'){
     *r++ = *s++;
}


to
string copyString(char *originalString)
{
     char[] copyOfString;
     while(*originalString != '\0')
     {
          copyOfString++ = originalString++;
     }
     return copyOfString;
}
(pardon I haven't used C in like a year or so. . . if context is off, sorry)
0

KYA Icon

28 May 2010 - 08:06 PM
Renaming of variables yes, using constructs nonexistent in C (strings) no. Using a non defined char array, no.
0

NickDMax Icon

17 June 2010 - 11:56 AM
@depricated, I see your apology but, before someone takes you code seriously I should point out that it is very incorrect.

First that is not how arrays are defined in C or C++, Even if it WERE you need to specify a size or initialize it to some string literal.
Then this line: copyOfString++ = originalString++; is not even close to correct. And then the return is not very self documenting at all as you said you would return a string and then returned a char[] -- which might get converted to a string but the compiler (it should actually) but that is not very "self documenting".

I understand what you are saying that if you use proper variable naming and writing the code as though it were to be "Read" is "self documenting" -- add to that the use of comments and a tool like doxigen or javadocs and you can have some pretty good code.
0

fallenreaper Icon

23 June 2010 - 07:56 AM
Did someone actually submit that? ewWWWWWWWW
0
Page 1 of 1

July 2014

S M T W T F S
  12345
678910 11 12
13141516171819
20212223242526
2728293031  

Tags

    Recent Entries

    Recent Comments

    Search My Blog

    1 user(s) viewing

    1 Guests
    0 member(s)
    0 anonymous member(s)