CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20
  1. #16
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: C - <= and >= Problems

    Quote Originally Posted by JohnW@Wessex View Post
    A simple example

    A && B || C

    You could assume that the coder knows the operator precedence and it does what he intended.

    If he were to type A && (B || C) then it doesn't matter whether the coder remembers && or || has the higher precedence, it will work as intended.
    Now imagine a much longer and more complicated logic test.
    I stick to this principle because it alleviates having to debug silly mistakes and lets me concentrate on the bigger issues.

    If parenthesis are superfluous, then what about spaces and carriage returns?

    Code:
    #include<iostream>
    #include<string>
    int main(){std::string name;std::cout<<"Name?";std::cin>>name;std::cout<<"Hello "<<name;}
    It's not about whether things are superfluous or not, it's about readability and conciseness of meaning;
    Spaces and carriage returns are for readability. Parenthesis are there for grouping to change the natural order of evaluation. Spaces don't change logic. Parenthesis do, so when they're there, it's assumed that they're there to affect logic, at least by me. Maybe they make it more readable to you but to me they're there to change the normal order of evaluation and it seems strange to add them when they do nothing.

    In the code in this case it's a simple logical and. There's nothing confusing or ambiguous about it.

  2. #17
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: C - <= and >= Problems

    Quote Originally Posted by JohnW@Wessex View Post
    A simple example

    A && B || C
    That's a total different kind of beef than a < b && b < c.

    && and || are from a logical point of view equal types of operators, so the preference ordering in C is purely autocratic.

    < and && are different types of operators and the C language luckily follows common sense here and defines the preference ordering as one would expect.a < b && b < c should make sense to anyone without knowing the preference order by heart.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  3. #18
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: C - <= and >= Problems

    Quote Originally Posted by GCDEF View Post
    Maybe they make it more readable to you but to me they're there to change the normal order of evaluation and it seems strange to add them when they do nothing.
    My coding style preferences come after many, many years of coding and debugging (often other people's code). Over the years I have moved to a principle of coding in a style that is much more verbose. I add in 'superfluous' parentheses; I test explicitly for zero/non-zero rather that using the implicit conversion to bool + many other 'useless' things. I do all this (and wrote it into the coding style) because it all but eliminates most of the dumb mistakes people make. I've found that it's not a good idea to assume that has the same interpretation of 'obvious'.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  4. #19
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: C - <= and >= Problems

    ================================================================================
    BBCode Version 0.10 (freeware)
    Author: potatoCode on forum www.codeguru.com
    ================================================================================
    Code:
    int convert( const char Val[], int Base , int * InValue ) 
    { 
    int i; 
    int count = 0; 
    bool negFlag = 0; 
    for( i = 0 ;; i++ ) 
    { 
    if ( Val[i] == '\0' ) 
    { 
    break; 
    } 
    printf ( "&#37;i\n" , Val[i] ); 
    if (Val[i]<=90 || Val[i]>=65); 
    { 
    printf ( "%i\n" , count ); 
    count++; 
    } 
    } 
    
    i = 0; 
    int Base10Value = 0; 
    int Base10Count = count; 
    printf ( "%i\n" , count ); 
    for( count ; count >= 0 ; ) 
    { 
    if ((65<=Val[i]<=90)or(97<=Val[i]<=122)); 
    { 
    int BaseToPower = 1 ; 
    for ( Base10Count ; ( Base10Count - 1 ) > 0 ; Base10Count-- ) 
    { 
    BaseToPower *= Base; 
    } 
    if ( 65 <= Val[i] <= 90 ) 
    { 
    Base10Value += ( ((Val[i] - 'A')+10) * BaseToPower); 
    } 
    if (Val[i]<= 97 ) 
    { 
    Base10Value += ( ((Val[i] - 'a')+10) * BaseToPower); 
    } 
    count--; 
    } 
    if ( ( 48 <= Val[i] <= 57 )); 
    { 
    int BaseToPower = 1 ; 
    for ( Base10Count ; ( Base10Count - 1 ) > 0 ; Base10Count-- ) 
    { 
    BaseToPower *= Base; 
    
    } 
    Base10Value += ( (Val[i] - '0') * BaseToPower); 
    count--; 
    } 
    i++; 
    }
    -------------------------------- Report Message --------------------------------
    Total errors (1)
    Total warning (0)
    Line 0 : Unmatched Curly brackets
    --------------------------------------------------------------------------------
    Elasped time: 0.0065 second(s)
    Last edited by potatoCode; October 12th, 2009 at 11:37 PM. Reason: fixing...

  5. #20
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: C - <= and >= Problems

    Quote Originally Posted by JohnW@Wessex View Post
    My coding style preferences come after many, many years of coding and debugging (often other people's code). Over the years I have moved to a principle of coding in a style that is much more verbose. I add in 'superfluous' parentheses; I test explicitly for zero/non-zero rather that using the implicit conversion to bool + many other 'useless' things. I do all this (and wrote it into the coding style) because it all but eliminates most of the dumb mistakes people make. I've found that it's not a good idea to assume that has the same interpretation of 'obvious'.
    Couldn't agree more. Always practice defensive coding style. I would much rather have "ugly" code, than waste time debugging trivial syntax errors.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured