This has really anoyed me

This is a discussion on This has really anoyed me within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Originally Posted by two31d Well... It's just as easy to write int = int + 1, instead of int++... What's ...

  1. #16
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Quote Originally Posted by two31d
    Well... It's just as easy to write int = int + 1, instead of int++... What's the big deal?
    Code:
    char *v = "this is a C style string";
    int sc = 0;
    
    while (*v++)
    {
        ++s;
    }
    vs.
    Code:
    char *v = "this is a C style string";
    int sc = 0;
    
    while (*v)
    {
        v = v + 1;
        s = s + 1;
    }
    Although the two are computationally equivilent, the first is far more concise. If the instructors don't want you to use shorthand, why the heck are they teaching C++ in the first place? Do they skip over the use of "cout << variable" because students might confuse it with left shift operations?

    The example is trivial, but I have other things to do.
    Insert obnoxious but pithy remark here

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Try this one:
    Code:
    std::list< int > ints;
    ints.push_back(1); ints.push_back(2); ints.push_back(3);
    
    for(std::list< int >::iterator it = ints.begin(); it != ints.end(); ++it) {
      std::cout << *it << ' ';
    }
    as opposed to the (supposedly) equivalent
    Code:
    std::list< int > ints;
    ints.push_back(1); ints.push_back(2); ints.push_back(3);
    
    for(std::list< int >::iterator it = ints.begin(); it != ints.end(); it = it + 1) {
      std::cout << *it << ' ';
    }
    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

  3. #18
    www.entropysink.com
    Join Date
    Feb 2002
    Posts
    605
    Hi chrismax2

    You'll be doing MT262 like me then.

    edit>> Bear in mind that this is a low-level course. I don't even think we're going to hit pointers. I'm only doing it for the credits.
    Last edited by RobR; 02-03-2006 at 11:15 AM.
    Visit entropysink.com - It's what your PC is made for!

  4. #19
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Coding standards are a fact of life. If you are going to whine that you can't use "insert favourite construct here" when your house standard says you can't, you'll be out of a job.
    Also no programmer would be caught dead using x = x + 1; when you have things like ++ and x += 1;
    Yes they would if they worked in a multi language code shop where the coding standards had been written to ensure that the majority of code written by the majority of coders in the majority of languages would understand what was going on.

    x = x + 1;

    Is pretty unambiguous regardless of your background.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #20
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by CornedBee
    It's interesting that you claim the linked list standard container, as well as instream and outstream iterators are poorly designed. Personally, I don't think so, but I'm sure you know what you're about.
    That reminds me of a story I once heard.

    One day, Bob decided, "If a man and woman marry, his cat better be the same color as the woman's, or else they wouldn't match, and that would be unfashionable." At that point in time, Mr. Uppity was riding by, and he was feeling desparate for a victory, so he stopped to answer.

    "You enjoy your delusion!" Mr. Uppity answered. "The Fionas are the most fashionable couple in town, and Mr. Fiona's cat is not the same color as Mrs. Fiona's!"

    To which Bob replied, "What are you talking about? Mrs. Fiona doesn't have a cat."

  6. #21
    Registered User
    Join Date
    Feb 2003
    Posts
    62
    Hey Robr

    Good to hear from you, I will look out for you on the course site.

    I got enough books, and as for adrianxw , I am not in a job and I don't have to agree with them if I dont want to. I just feel it's the wrong thing to do. Do you really agree with them yourself?

    Yeah they dont even talk about pointers in the course, but then I got 8 different C++ books, so I can learn that through them. I kind of know them
    Last edited by chrismax2; 02-03-2006 at 04:15 PM.

  7. #22
    Registered User
    Join Date
    Feb 2003
    Posts
    62
    Quote Originally Posted by filker0
    Do they skip over the use of "cout << variable" because students might confuse it with left shift operations?

    The example is trivial, but I have other things to do.
    Guess what? Yes they do skip cout <<
    and cin >> as well!!!!

  8. #23
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Quote Originally Posted by adrianxw
    x = x + 1;

    Is pretty unambiguous regardless of your background.
    There are places where post increment and post decrement are very valuable, and not using them requires the addition of a temporary variable to hold the old value of the variable.

    Also, with pointers, though it's equivilent, which looks clearer?
    Code:
    struct foo *ptr;
    
    ptr=get_FirstFoo();
    while (ptr != NULL)
    {
        do_something(ptr);
        ptr++;
    }
    or
    Code:
    struct foo *ptr;
    
    ptr=get_FirstFoo();
    while (ptr != NULL)
    {
        do_something(ptr);
        ptr = ptr + 1;
    }
    More people have trouble with explicit pointer arithmetic than have trouble with post-increment.

    Coding standards, however, trump all. I've had to forego all sorts of things I like to use (function pointers, conditional expressions (cond?v1:v2), #defines (yes, #define, in C, is banned one place that I worked. Don't ask), and arrays (in C++). You must adhere to the coding standards set by your employer, even if you think they're stupid.

    [edit]
    I'm currently working at a place where the C coding standard requires casts wherever a "safe" type promotion might occur. There are some folks in the C programming forum who believe that an explicit type cast is an indication that the programmer is less than competant. (I'm not one of them.)
    [/edit]
    Last edited by filker0; 02-03-2006 at 07:59 PM. Reason: fix formatting.
    Insert obnoxious but pithy remark here

  9. #24
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    There is quite a difference in using x = x+1 and x++
    Guess the output :P
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int x;
    	x = 5;
    	cout << (x++) << endl;
    	x = 5;
    	cout << (x = x+1) << endl;
    
    	system("pause");
    
    	return 0;
    }

  10. #25
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    your example is contrived. It is never necessary to use x++, though x=x+1 might require more lines of code (which itself in many companies is a good thing because they judge programmer productivity based on the number of lines of code they produce).

  11. #26
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Quote Originally Posted by jwenting
    your example is contrived. It is never necessary to use x++, though x=x+1 might require more lines of code (which itself in many companies is a good thing because they judge programmer productivity based on the number of lines of code they produce).
    Have you tried my example yet? There are classes that quite reasonably overload ++, but not += or +. Using x=x+1 with those results in a compile error.

    yes, #define, in C, is banned one place that I worked. Don't ask
    I do ask. How do you do symbolic constants then?
    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

  12. #27
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    Have you tried my example yet? There are classes that quite reasonably overload ++, but not += or +. Using x=x+1 with those results in a compile error.
    That is an error in the implementation of those classes, don't blame the operator for that...

    I do ask. How do you do symbolic constants then?
    you don't. You define some globals and hope noone changes them.

  13. #28
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Quote Originally Posted by jwenting
    That is an error in the implementation of those classes, don't blame the operator for that...
    No, it is not. It is a logical consequence of the fact that these classes, by design, do not support arbitrary jumps other than by repeatedly using increment. It makes sense, it's intuitive.
    I don't blame the operator. I blame the assumption that every class implementing ++ must implement +=.

    you don't. You define some globals and hope noone changes them.
    That's not sufficient. You can't use globals as sizes for static arrays.
    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

  14. #29
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    no class needs to use an operator for anything. Operator overloading is a luxury, not a necessity.

    You indeed can't use globals for array sizes, but you can use magic numbers.
    Some people have decided that magic numbers are better than #defines, others have decided to use #defines everywhere.
    Both decisions are flawed in that they don't take into account the inherent truth that there's a time and place for everything.

  15. #30
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    no class needs to use an operator for anything. Operator overloading is a luxury, not a necessity.
    Completely irrelevant. Whether the method is called inc() or operator ++, whether it's called add() or operator +=, the principle is that there are cases when incrementing makes sense, but adding doesn't. Thus, having a ++ operator but not a += operator makes sense, and any coding standard that says it must be written as x=x+1 is ignoring that.
    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

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

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