Thread: is debugging syntax error really this hard??

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Codeplug View Post
    >> ... so as not to interfere with readability...
    The main interference with readability is the bracing style (post #5). At a minimum, closing braces should be first on the line, and in the same column of the statement that started the block. Discerning the scope should be as easy as possible, not a bracket hunt.

    gg
    Ok, Codeplug, added to the list of people who've chastized my bracketing style.... Got that, thanks.

    Seriously though... I've been writing code that way for so long I actually find some of the samples on the forum here hard to read... In particular code like this takes careful study...
    Code:
    if (x <  10){
      Printf("Burp\n");
    }
    I will miss seeing the first brace every time and have to go back and actually look for it. To my eye, this is much clearer...
    Code:
    if (x <  10)
      { Printf("Burp\n"); }
    Last edited by CommonTater; 08-21-2011 at 08:43 AM.

  2. #17
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Here is your original example: WalkDirs()

    Converted to Allman style:
    Code:
    // recursive directory search
    void WalkDirs(PCHAR SPath)
    { 
        HANDLE HDir;
        // switch to specified direcory
        if (!SetCurrentDirectory(SPath))
        { 
            printf("Error Invalid Pathname\n");
            exit(GetLastError()); 
        }
    
        // Show Full Pathname
        GetCurrentDirectory(MAX_PATH,FPath);
        printf("--> %s\n",FPath);
    
        // begin directory scan
        HDir = FindFirstFile("*.*",&DirData);
        do
        { 
            if (DirData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            { 
                // ignore . and .. sillyness
                if ((strcmp(DirData.cFileName,".") != 0) &&
                    (strcmp(DirData.cFileName,"..") != 0))
                { 
                    // call myself to test a new directory
                    WalkDirs(DirData.cFileName);
                    // done, now back up one level
                    SetCurrentDirectory("..");
                    // Show Pathname
                    GetCurrentDirectory(MAX_PATH,FPath);
                    printf("<-- %s\n",FPath); 
                } 
            }
            else
            {
                // list filenames
                printf("\t\t%s\n",DirData.cFileName);
            }
         <------ oops  
        while (FindNextFile(HDir,&DirData) != ERROR_NO_MORE_FILES);
        FindClose(HDir); 
    }
    So not only is easy to discern scope, match "else" to its "if", etc.., but it's also easy to see that a closing bracket was missed (but your compiler should tell you that much).

    However your "eye" has been previously trained, this style should be easy to read by anyone.

    gg

  3. #18
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Codeplug View Post
    So not only is easy to discern scope, match "else" to its "if", etc.., but it's also easy to see that a closing bracket was missed (but your compiler should tell you that much).

    However your "eye" has been previously trained, this style should be easy to read by anyone.

    gg
    This is a style point and I do tend to not agree with Tater's, however to be fair I would like to point out that you did not copy all of the code Tater had posted. You missed the final if statement, which contained the closing brace for the do-while loop. Take a closer look.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #19
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    @codeplug...
    If you don't mind (and I'm betting you do) I think I'll just carry on as before. Thanks for your concern.

    Also look at my screen shot again... It is YOU that missed a brace...
    (FWIW... that screenshot was a segment of live code that runs just fine.)
    Last edited by CommonTater; 08-21-2011 at 09:44 AM.

  5. #20
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> Take a closer look.
    >> It is YOU that missed a brace...
    I provided a link - click on it. The code is directly from CommonTater's post.

    If you had taken the closer look, you may of said, "ah, but the brace isn't missing in the screenshot". Obviously it's a copy-n-paste (or line-delete) bug. This time it's just a forum post, and the compiler would let you know as well. Copy-n-paste bugs that compile, but miss-match an if-else would be much worse.

    >> If you don't mind...
    Any company with official coding standards would never allow that code to be reviewed, much less checked into source control. But since you code for yourself, you only have yourself to satisfy.
    But that's just my closed-source experience - we can all take a look at open-source. I doubt you can find any major open-source project that allows closing brackets at EOL. I've never seen it accepted anywhere - for all the reasons mentioned here and in the Wikipedia article.

    I only "mind" as a participant in this forum, and that your bad habits may propagate to newbs who don't know any better. Which is why I'm compelled to point it out.

    gg

  6. #21
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Codeplug View Post
    >> Take a closer look.
    >> It is YOU that missed a brace...
    I provided a link - click on it. The code is directly from CommonTater's post.

    If you had taken the closer look, you may of said, "ah, but the brace isn't missing in the screenshot". Obviously it's a copy-n-paste (or line-delete) bug. This time it's just a forum post, and the compiler would let you know as well. Copy-n-paste bugs that compile, but miss-match an if-else would be much worse.
    gg
    Oh, I see now. Missed that link, I just thought you took the code from the screen shot.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #22
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    @codeplug...

    Ok... CodePlug.Objection++ .... are we done now?

    (In case you are wondering why my attitude toward you is dismissive... It's largely because I never see you chip in to help anyone and the only time I do see you is to pick nits about trivial issues in other people's code.)
    Last edited by CommonTater; 08-21-2011 at 12:34 PM.

  8. #23
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    Hmm.. I feel like quite a pain in the .. Well, you know it

    I just HAD TO comment this thread, because I saw poor lonely goto being nitpicked that badly. I do agree that goto should not be used to construct a loop. But I never understood why gotos should be totally avoided. I personally really often use gotos for errorhandling. For example, something like

    Code:
    static int setRtMsgData
    (
        SNetlinkTransactRoute *_this,
        unsigned char family, 
        char *dest, 
        char *subnet,
        char *gw, 
        char *src 
    )
    {
        //int ifIndex;
        struct in_addr ipv4dst, ipv4src, ipv4gw;
        struct in6_addr ipv6dst, ipv6src, ipv6gw;
        void *bdst=NULL,*bsrc=NULL,*bgw=NULL;
        unsigned char dstlen,srclen, addrlen;
        int (*maskfunc)(char *,void *); 
        unsigned int binsize;
        int defgwsetup = 0;
        DPRINT("Initializing RTMSG header!");
        if(ENetlinkTrState_Inited!=_this->gentr.state)
        {
            EPRINT("Invalid state %u, shouldve been %u",_this->gentr.state,ENetlinkTrState_Inited);
            return KIKKULA_ERROR;
        }
        if(NULL==dest)
        {
        /* TODO: fix - This can be NULL if we set up default GW! */
          //  EPRINT("Valid destination required, dst NULL!");
          //  goto invparam_out;
            DPRINT("Handling default GW!");
            defgwsetup=1;
            dstlen=0;
        }
    
        if(AF_INET==family)
        {
            bdst=&ipv4dst;
            bsrc=&ipv4src;
            bgw=&ipv4gw;
            maskfunc=&ipv4subnet2dstlen;
            addrlen=32;
            binsize=4;
            DPRINT("Family AF_INET");
        }
        else if (AF_INET6 == family)
        {
            bdst=&ipv6dst;
            bsrc=&ipv6src;
            bgw=&ipv6gw;
            addrlen=128;
            maskfunc=&ipv6subnet2dstlen;
            binsize=16;
            DPRINT("Family AF_INET6");
        }
        else
        {
            EPRINT("Unknown family!");
            return KIKKULA_ERROR;
        }
        if(!defgwsetup)
        {
            if(1!=inet_pton(family, &dest[0], bdst))
            {
                /* Error in dst conversion */
                EPRINT
                (
                    "Failed to convert dst %s to ipaddr",
                    dest
                );
                goto invparam_out;
            }
            DPRINT("dst before masking %s",dest);
        /* NOTE! The maskfunc may change destination address! Thus the dst addr must not be added to request before we've called maskfunc */
            dstlen=(*maskfunc)(subnet,bdst);
            if((char)-1==(char)dstlen)
            {
                EPRINT("Failed to get dst lenght!");
                goto invparam_out;
            }
            {
                char tmp[100];
                DPRINT
                (
                    
                    "dst now %s",
                    inet_ntop(family,bdst,tmp,100)
                );
            }
            DPRINT("Adding dst attr (%d) size %u",RTA_DST,binsize);
            _this->gentr.addAttr(&(_this->gentr),RTA_DST,bdst,binsize);
        }
        if(NULL!=src&&'\0'!=src[0])
        {
            if(defgwsetup)
            {
                EPRINT("src addr for default GW requested!");
                goto invparam_out;
            }
            if(1!=inet_pton(family, &src[0], bsrc))
            {
                /* Error in conversion */
                EPRINT
                (
                    "Failed to convert src %s to ipaddr",
                    src
                );
                goto invparam_out;
            }
            {
                char tmp[100];
                DPRINT
                (                
                    "src %s",
                    inet_ntop(family,bsrc,tmp,100)
                );
            }
           srclen=addrlen;
            _this->gentr.addAttr(&(_this->gentr),RTA_SRC,bsrc,binsize);
        }
        else
        {
            srclen=0;
        }
    
        if(NULL!=gw && '\0'!=gw[0])
        {
            if(1!=inet_pton(family, gw,bgw))
            {
                EPRINT
                (
                    "gateway %s is not valid gw Ip address",
                    gw
                );
                goto invparam_out;
            }
            {
                char tmp[100];
                DPRINT
                (
                    
                    "gw now %s",
                    inet_ntop(family,bgw,tmp,100)
                );
            }
    
            _this->gentr.addAttr(&(_this->gentr),RTA_GATEWAY,bgw,binsize);
        }
        else
        {
            if(defgwsetup)
            {
                EPRINT("default GW operation requested, but no GW address given!");
                goto invparam_out;
            }
        }
        DPRINT
        (
            "subnet lenght (mask) set to %u bits",
            dstlen
        );
    
    
        _this->rt_msg.rtm_family=family;
        _this->rt_msg.rtm_dst_len=(unsigned char)dstlen;
        _this->rt_msg.rtm_src_len=srclen;
        _this->rt_msg.rtm_table=RT_TABLE_MAIN;
        _this->rt_msg.rtm_protocol= RTPROT_STATIC;
        _this->rt_msg.rtm_scope=RT_SCOPE_UNIVERSE;
        _this->rt_msg.rtm_type=RTN_UNICAST;
        _this->rt_msg.rtm_flags=0;
        _this->gentr.state=ENetlinkTrState_ReqFilled;
        if(0)
        {
    invparam_out:
            _this->gentr.state=ENetlinkTrState_Unknown;
            EPRINT("Invalid parameters detected!");
            return USER_ERROR;
        }
        return 0;
    }
    is not rare in my files. Not rare at all Actually, this is a sample from my C wrapper which eases route, link and address management via netlink sockets.

    And yes. I bet you're not going to find
    Code:
    if(failure_detected)
        goto errhandling;
    
    if(0)
    {
    errhandling:
      
    }
    construct from any schoolbooks :] However this is something I have used quite a lot, and which is pretty nice way to do the errorhandling. Another spot where I use goto, is when a retry for something is needed. Maybe the most common example is select() or recv call interrupted by a signal.

  9. #24
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    ... the only time I do see you is to pick nits about trivial issues in other people's code.
    Let me start by saying that your closing bracket style is anything if but trivial. It requires attention and correction.

    ... I never see you chip in to help anyone ...
    Your opinion is misinformed. Click on "Codeplug", then "View Forum Posts". Show me a thread where I did not contribute.

    gg

  10. #25
    Registered User
    Join Date
    Aug 2011
    Posts
    2
    thanks guys for all your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging error
    By vexed in forum C Programming
    Replies: 3
    Last Post: 03-08-2011, 01:44 PM
  2. Debugging error
    By vexed in forum C Programming
    Replies: 5
    Last Post: 03-08-2011, 11:46 AM
  3. error C2061: syntax error : identifier
    By maninboots in forum C++ Programming
    Replies: 4
    Last Post: 07-02-2009, 05:40 AM
  4. GCC compiler giving syntax error before 'double' error
    By dragonmint in forum Linux Programming
    Replies: 4
    Last Post: 06-02-2007, 05:38 PM
  5. Syntax too hard for computer?
    By David Somekh in forum C Programming
    Replies: 3
    Last Post: 04-10-2007, 05:16 AM

Tags for this Thread