Bad programming practice question

This is a discussion on Bad programming practice question within the C Programming forums, part of the General Programming Boards category; I am reading a book called "C: The complete referente" and the author says that doing something like this is ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    Bad programming practice question

    I am reading a book called "C: The complete referente" and the author says that doing something like this is bad programming practice and possibly inefficient.
    Code:
    if(strcmp(a, b) == 0)
        printf("they're equal\n");
    In my opinion, writing code like that makes it easier to read. What do you guys have to say about this? Thanks.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    It's potentially inefficient, but hardly bad programming practice. How else are you going to do a lexical comparison of two strings? If you write it yourself, it would be the equivalent of strcmp. Then again, since this unnamed author is in fact Herbie Schildt, I would take anything he says with a grain of salt if I were you.

    Could you describe in detail what part of that code the book says is bad practice?
    My best code is written with the delete key.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > is bad programming practice and possibly inefficient.
    And what's his good programming practice and efficient alternative?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    that code is fine.... depends on the rest of the context in which it is inserted...

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> the author says that doing something like this is bad programming practice and possibly inefficient. <<

    Maybe the "bad programming practice" part is the lack of brackets and the "possibly inefficient" part is the use of printf instead of puts?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Quote Originally Posted by anonytmouse
    >> the author says that doing something like this is bad programming practice and possibly inefficient. <<

    Maybe the "bad programming practice" part is the lack of brackets and the "possibly inefficient" part is the use of printf instead of puts?
    That would certainly be consistent with Schildt's track record.
    My best code is written with the delete key.

  7. #7

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    Quote Originally Posted by Prelude
    Could you describe in detail what part of that code the book says is bad practice?
    Sure! He wrote a program to divide two numbers like this:
    Code:
    #include <stdio.h>
    
    void main(void) /* yes, he uses void main throughout the book! */
    {
        int a, b;
    
        printf("Type two numbers: ");
        scanf("%d%d", &a, &b);
    
        if(b) printf("%d\n", a / b);
        else printf("cannot divide by zero\n");
    }
    Right after this he wrote that if you write the if command this way,
    Code:
    if(b != 0) printf("%d\n", a / b);
    it is redundant, possibly inefficient and bad style.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    It's very outdated advice advocating premature optimization.

    http://www.eskimo.com/~scs/C-faq/q5.3.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Not mentioning that it's NOT bad style (in fact, my university deducted points for using a integer in a boolean way if it's not meant to be a boolean) and NOT inefficient (unless your compiler really sucks.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >it is redundant, possibly inefficient and bad style
    *stunned silence*
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    I've just decided something: I'm not reading this book anymore
    Luckly I can still send it back and get a refund.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I'm not reading this book anymore
    Good idea. Might I suggest "The C Programming Language" by Kernighan and Ritchie?
    My best code is written with the delete key.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    I have that one. I am completely in love with that book but the problem is that it's sometimes too advanced for me. I wanted to read a C book to, let's say, get me ready for The C Programming Language. Do you have any suggestions?

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    It's not too advanced, it just moves quickly and covers a lot of ground. If you work through it then you won't need any book to "get you ready" for K&R. But, some people need their hand held, and for them there's the ACCU. Don't settle for anything less than Highly Recommended or Recommended.
    My best code is written with the delete key.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bad and fail of steam
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 02-19-2008, 02:07 AM
  2. using atoi() bad practice?
    By Beowolf in forum C++ Programming
    Replies: 6
    Last Post: 09-24-2007, 09:35 AM
  3. Cascading bool returns, bad error handling practice?
    By indigo0086 in forum C++ Programming
    Replies: 5
    Last Post: 06-27-2007, 03:38 AM
  4. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  5. data loss bad bad bad
    By RoD in forum Tech Board
    Replies: 4
    Last Post: 05-01-2003, 12:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21