Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Why I hate c (http://www.chiefdelphi.com/forums/showthread.php?t=26085)

Alan Anderson 26-02-2004 09:16

Why I hate c
 
Code:

void User_Autonomous_Code(void)
{
  while (autonomous_mode)  /* DO NOT CHANGE! */
  {
    if (statusflag.NEW_SPI_DATA)      /* 26.2ms loop area */
    {
        Getdata(&rxdata);  /* DO NOT DELETE, or you will be stuck here forever! */
        /* Add your own autonomous code here. */
        // [state machine implementation]
        Putdata(&txdata);  /* DO NOT DELETE, or you will get no PWM outputs! */
    }
    Process_Data_From_Local_IO;
  }
}

It took me four hours last night to figure out why Process_Data_From_Local_IO never gets called.

roknjohn 26-02-2004 10:44

Re: Why I hate c
 
I totally agree. I'm a professional software engineer with 20 years experience. C is such an unforgiving language. It is difficult to read, easy to make simple errors in syntax, and most compilers won't warn you about things like:

Code:

Process_Data_From_Local_IO;
Or, the one that gets me all the time:

Code:

  if (myvar=0)
                  {
                          dosomething();
                  }


Dave Flowerday 26-02-2004 10:53

Re: Why I hate c
 
Quote:

Originally Posted by roknjohn
Code:

if (myvar=0)
{
  dosomething();
}


If you get yourself in the habit of specifying the constant first then the variable, you'll produce a compiler error if you only have 1 = sign. Our coding standard at work specifies this as a requirement to catch at least some of these types of errors. The above code would then become:
Code:

if(0 = myvar)
{
  dosomething();
}

which will give an error. Doesn't solve all the problems but it at least allows you to catch a few simple mistakes...

deltacoder1020 26-02-2004 12:14

Re: Why I hate c
 
also the reason

Code:

Process_Data_From_Local_IO;
does not give an error is because it is a valid statement. it evaluates to the memory address of the Process_Data_From_Local_IO function. Now you might ask, "well, why doesn't if give a warning at least, because the line doesn't do anything"? Well, then you wouldn't be able to use the following code without a warning either:

Code:

x++;
because technically, that line just as much of "nothing". it evaluates to the value of x, and then increments it.

Ryan M. 26-02-2004 12:19

Re: Why I hate c
 
Using a utility like Lint can help with those sorts of errors. Just run it and it will warn you of anything that you probably didn't mean to do.

Astronouth7303 26-02-2004 13:51

Re: Why I hate c
 
Code is only as readable as you make it. Make names self-explainatory (or close to it). I tought myself VB, and my naming conventions are based on that.

gnormhurst 26-02-2004 14:00

Re: Why I hate c
 
Quote:

Originally Posted by Texan
Using a utility like Lint can help with those sorts of errors. Just run it and it will warn you of anything that you probably didn't mean to do.

Does the Microchip compiler have an option like Gnu's gcc "-Wall" (warnings: All)? I really miss -Wall. It catches when printf has the wrong number or type of arguments, warns about "if ( x = 0 )", and points out unused variables, for example.

I'm going to a talk by Brian Kernighan next week -- I'll give him your regards! ;)

Mark McLeod 26-02-2004 14:06

Re: Why I hate c
 
Quote:

Originally Posted by gnormhurst
Does the Microchip compiler have an option like Gnu's gcc "-Wall" (warnings: All)? I really miss -Wall. It catches when printf has the wrong number or type of arguments, warns about "if ( x = 0 )", and points out unused variables, for example.

I'm going to a talk by Brian Kernighan next week -- I'll give him your regards! ;)

In MPLAB look under Project->Build Options...->Project
under the MPLAB C18 tab "Diagnostics level" you can chose "errors, warnings and messages" and get more dianostic info, but it won't catch it all.

Alan Anderson 26-02-2004 14:33

Re: Why I hate c
 
Quote:

Originally Posted by gnormhurst
I'm going to a talk by Brian Kernighan next week -- I'll give him your regards! ;)

If I recall correctly, K&R page 101 gives the derivation for the "canonical" string copy statement.
Code:

while(*t++ = *s++);
That's part of why I find c such an uncomfortable language to use. It wants me to think like a compiler. It encourages the use of simple shortcuts that a moderately well-designed compiler should be doing for me. My code is full of explicit tests for zero/nonzero. Although I know I could replace
Code:

if ( my_timer == 0 )
with
Code:

if ( !my_timer )
and get the same job done, I'm a programmer, and I want to be free to focus on the algorithms and let the compiler care about the details of the bits and bytes. Coding can be, and should be, done by machines.

And don't get me started on how numeric operations are performed using the smallest size that the operands will fit in, leaving me to worry about overflows even if I know the end result of an expression will always fit in the variable I'm putting it in. Oops, too late...

I apologize. It was a late night last night. And I must admit that the tool does get the job done, with little to no fuss, as long as I remember to use it correctly.

gnormhurst 26-02-2004 15:09

Re: Why I hate c
 
Quote:

Originally Posted by Alan Anderson
Code:

while(*t++ = *s++);

Yeah, that's just obscene. There are still programmers who seem to have the anti-social impulse to create clever, opaque code, so they can feel smug as the rest of the world struggles to understand what the heck it does.

Ryan M. 26-02-2004 16:26

Re: Why I hate c
 
Quote:

Originally Posted by Mark McLeod
In MPLAB look under Project->Build Options...->Project
under the MPLAB C18 tab "Diagnostics level" you can chose "errors, warnings and messages" and get more dianostic info, but it won't catch it all.

Personally, I find the messages more annoying then helpful. Some people might like it, though.

Ryan M. 26-02-2004 16:30

Re: Why I hate c
 
Quote:

Originally Posted by Alan Anderson
If I recall correctly, K&R page 101 gives the derivation for the "canonical" string copy statement.
Code:

while(*t++ = *s++);
That's part of why I find c such an uncomfortable language to use. It wants me to think like a compiler. It encourages the use of simple shortcuts that a moderately well-designed compiler should be doing for me. My code is full of explicit tests for zero/nonzero. Although I know I could replace
Code:

if ( my_timer == 0 )
with
Code:

if ( !my_timer )
and get the same job done, I'm a programmer, and I want to be free to focus on the algorithms and let the compiler care about the details of the bits and bytes. Coding can be, and should be, done by machines.

And don't get me started on how numeric operations are performed using the smallest size that the operands will fit in, leaving me to worry about overflows even if I know the end result of an expression will always fit in the variable I'm putting it in. Oops, too late...

I apologize. It was a late night last night. And I must admit that the tool does get the job done, with little to no fuss, as long as I remember to use it correctly.

As C/C++ is the language I know best, I don't find a statement like that very confusing. But I agree with you that it should be clear, not "clever." Even though I could read that perfectly, if anyone else looks at my code they might not be able to. It is better to write clear, obvious code, than to have a single line that does the exact same thing.

--EDIT--
P.S. Sorry for double posting there. :)

deltacoder1020 26-02-2004 17:56

Re: Why I hate c
 
Quote:

Originally Posted by Alan Anderson
If I recall correctly, K&R page 101 gives the derivation for the "canonical" string copy statement.
Code:

while(*t++ = *s++);
That's part of why I find c such an uncomfortable language to use. It wants me to think like a compiler. It encourages the use of simple shortcuts that a moderately well-designed compiler should be doing for me.

hence the reason for the string.h functions in any standards-compliant C distro. MPLAB is far from standards, as it is basically on the bare minimum needed to program a robot (which isn't going to be doing much with strings).

Quote:

My code is full of explicit tests for zero/nonzero. Although I know I could replace
Code:

if ( my_timer == 0 )
with
Code:

if ( !my_timer )
and get the same job done, I'm a programmer, and I want to be free to focus on the algorithms and let the compiler care about the details of the bits and bytes. Coding can be, and should be, done by machines.
i'll agree with you on that - MCC18 is a fairly sucky compiler. there are, however, quite a few C compilers that do perform optimizations such as this. don't blame the language, blame the compiler.

Quote:

And don't get me started on how numeric operations are performed using the smallest size that the operands will fit in, leaving me to worry about overflows even if I know the end result of an expression will always fit in the variable I'm putting it in. Oops, too late...
again, a fault of MCC18, not C itself.

all in all, most of the faults you described are with this specific implementation of C. Chances are you would find programming for a regular computer with C much nicer.

velocipenguin 26-02-2004 19:05

Re: Why I hate c
 
C is one of my favorite languages. While it can be VERY unforgiving at first, it's not too difficult once you get used to the language. I used to make lots of simple errors (forgetting parentheses, failing to end statements with semicolons, etc.), but the frequency of that sort of error decreases significantly once correct C syntax becomes habitual. C is tremendously powerful without being nearly as arcane as assembler; it allows one to easily combine useful low-level operations with high-level procedural and algorithmic code in a syntax that's clear and human-readable. It took me a long time to get used to C, but I now find that it's my language of choice for many programming tasks. Don't blame the language because you made an error. Certain errors may look like obvious mistakes once you find them, but those errors are often similar or identical to operations that one might perform in order to obtain useful results.

For example:
Code:

if(file_handle = fopen("foo.bar","r"))
{
      herr_doktor_function();
}

That's a valid use of the single equality sign in a conditional. It checks to make sure the fopen was successful before executing herr_doktor_function. This could be written in a slightly clearer way, but it's a good example of why C has such lenient error-checking.

Alan Anderson 26-02-2004 19:41

Re: Why I hate c
 
Quote:

Originally Posted by velocipenguin
Don't blame the language because you made an error. Certain errors may look like obvious mistakes once you find them, but those errors are often similar or identical to operations that one might perform in order to obtain useful results.

Actually, that's exactly why I dislike c as much as I do. A small error in typing results in something which is both valid code and completely different from what I intended. A common and easily made error should be noticed as an error by the compiler.

Yes, c is very capable. It is also very terse. In sufficiently skilled hands, that's a powerful combination. In just slightly less skilled hands, it's a recipe for subtle errors. As for me, I prefer to use a language which is a bit less terse, with syntax that doesn't make it quite so easy to shoot yourself in the foot.


All times are GMT -5. The time now is 11:09.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi