SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    56 Post(s)
    Tagged
    0 Thread(s)

    A hidden reason indenting is important - test code.

    Consider the following

    Code php:
    class A {
        public function foo() {
            for($i = 0; $i < 10; $i++) {
                echo 'repeat '.$i;
            }
        }
    }

    Indenting (whether vi as above or open emacs style) is used to make code more readable. Recently though I had to point out another use to a coworker that I thought was obvious - temporary test code that you intend to delete later shouldn't be indented. That makes it stick out like a sore thumb to be spotted and removed later...

    Code php:
    class A {
        public function foo() {
            for($i = 0; $i < 10; $i++) {
    if ($i == 5) { echo 'stop'; exit; }
                echo 'repeat '.$i;
            }
        }
    }

  2. #2
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    I like this idea! I don't know if it's obvious or not, but it's definitely something I'm going to start doing. Nice tip!

  3. #3
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I thought the best way was to embed test/debug code IN your code, and test for a debug flag? (The overhead is likely minimal).
    Code PHP:
    //Set debug flag status
    $debug=true;
     
    class A {
        public function foo() {
            for($i = 0; $i < 10; $i++) {
               if ($debug) {
                   if ($i == 5) { echo 'stop'; exit; }
               }
                echo 'repeat '.$i;
            }
        }
     }
    That way you can turn debug on/off as needed.
    Of course, the debug code could still be non-indented.
    Ian Anderson
    www.siteguru.co.uk

  4. #4
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    925
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Interesting tip, I'll give it a try!

    BTW, for this purpose it would be great to be able to insert a new line without indentation - for example, I couldn't find a way to insert a new line without auto-indenting in Netbeans. So I recorded a simple macro consisting of two key presses: Enter and Home, and assigned it shift+control+Enter shortcut. This should work in most other IDE's, too.

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,813
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    I've always used TODO comments that several IDEs support
    PHP Code:
    // TODO: Remove this, its used for testing 
    Edit:

    Some IDEs also support // HACK: that can be shown in a custom panel too


    Then once I'm ready to checkin my changes, I check the project for TODO comments (through the TODO panel -- both phpStorm and Visual Studio support this)

    I've also used the technique @siteguru ; suggested, except I use a constant define('DEBUG', true) and I have the constant disabled by default in Production. That way I can mimic the follow that exists in Visual Studio
    Code:
    #IF DEBUG
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  6. #6
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by siteguru View Post
    I thought the best way was to embed test/debug code IN your code, and test for a debug flag? (The overhead is likely minimal).
    Code PHP:
    //Set debug flag status
    $debug=true;
     
    class A {
        public function foo() {
            for($i = 0; $i < 10; $i++) {
               if ($debug) {
                   if ($i == 5) { echo 'stop'; exit; }
               }
                echo 'repeat '.$i;
            }
        }
     }
    That way you can turn debug on/off as needed.
    Of course, the debug code could still be non-indented.

    There's a few problems with this approach, firstly if the code is moved to a system where $debug is never set at all (to true or false) it will cause errors. Secondly, developers are lazy. They'll leave in if ($debug) blocks and turn debug off... when the next developer comes along and adds a new $debug block, turns on debug mode... they may suddenly get behaviour that didn't exist prior to turning debug on because it had been left in by a previous developer.

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,813
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TomB View Post
    There's a few problems with this approach, firstly if the code is moved to a system where $debug is never set at all (to true or false) it will cause errors. Secondly, developers are lazy. They'll leave in if ($debug) blocks and turn debug off... when the next developer comes along and adds a new $debug block, turns on debug mode... they may suddenly get behaviour that didn't exist prior to turning debug on because it had been left in by a previous developer.
    The same is true if you accidentally leave your unindented code in... Except you can't turn it off on Prod as quickly as using a constant/variable to control it. Personally, I'd even go one step further and create an Environment variable so you can use $_ENV and have the ability to turn it off across the entire server. The issue is purely the fact that the test/debug code is being left in. The real question is, which method has less of an impact if that code made it to Production.

    Unindenting it won't necessarily prevent an accidental checkin, neither will TODO or HACK comments (even if you are always checking for them before checkin) as at some point you'll get distracted and just check it in (happens to everyone). So every method has issues (to assume otherwise, is negligent).

    In all reality, the best way to introduce debug/test code is DON'T DO IT. Use xDebug, set a breakpoint, profile it, don't add random code that provides nothing to the codebase.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  8. #8
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,578
    Mentioned
    62 Post(s)
    Tagged
    3 Thread(s)
    I am currently working with the Google Youtube API and kept forgetting to upload the changed debug code...

    so once bitten, twice shy and three times fix forever

    PHP Code:

    # constants.php
      
    defined('LOCALHOST') ?: define('LOCALHOST''localhost' === $_SERVER['SERVER_NAME']);
      
    definded('iMAX')     ?: define('iMAX'LOCALHOST 99999);

    # myModel.php
     
    for($i 0$i 10$i++):
       if(  
    $i iMAX ):
          echo 
    'repeat '.$i;
       endif;
     endfor;


     
    $iCnt 0;
     foreach(
    $obj->result() as $item):
        if( 
    $iCnt++ < iMAX):
          echo 
    'repeat '.$i;
        endif; 
      endforeach; 
    Last edited by John_Betong; May 2, 2013 at 06:16. Reason: formatting

  9. #9
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    56 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by siteguru View Post
    I thought the best way was to embed test/debug code IN your code, and test for a debug flag?
    This tip is for very short term test code that you probably be deleting within the hour. For example, I was setting some $GLOBALS variables to trace my way through a recursive function to find out what was going wrong. I had no intention of making them a long term addition to the code. Since I had to place them in 9 spots in the file I wanted to be sure to be rid of them.

  10. #10
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    56 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TomB View Post
    There's a few problems with this approach, firstly if the code is moved to a system where $debug is never set at all (to true or false) it will cause errors. Secondly, developers are lazy. They'll leave in if ($debug) blocks and turn debug off... when the next developer comes along and adds a new $debug block, turns on debug mode... they may suddenly get behaviour that didn't exist prior to turning debug on because it had been left in by a previous developer.
    Indeed. I've seen this happen in large systems, which is a PITA.

    @siteguru If you're going to have a debug flag at least use setEnv to place it in the Apache Config file or htaccess file. This way you can't accidently transfer the variables status between live and dev environments since, presumably, the two servers will use different server config files. For example

    SetEnv PNL_DEBUG

    Will create $_SERVER['PNL_DEBUG']

    This way accidental check in is less of an issue.

  11. #11
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Mine was just a very rough-and-ready example to show a principle, not a proper implementation - a bit like the opening post really.

    And not all development or production environments are Linux/Apache - some people use a Windows environment (at least for development), so no .htaccess or Apache configs. But I take what you say.
    Ian Anderson
    www.siteguru.co.uk

  12. #12
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    56 Post(s)
    Tagged
    0 Thread(s)
    There are ways to set environment variables in IIS. They're obscure and hard to work with, but they're there. This is why Microsoft never made any real headway in the world of web servers - they're product is crap and they can't advertise their way out of the simple fact that IIS is a POS.

  13. #13
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,200
    Mentioned
    105 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Michael Morris View Post
    There are ways to set environment variables in IIS. They're obscure and hard to work with, but they're there. This is why Microsoft never made any real headway in the world of web servers - they're product is crap and they can't advertise their way out of the simple fact that IIS is a POS.
    They're not obscure or hard to deal with....they're very easy, as a matter of fact. The only thing I thought that IIS sucks about is the URL rewrite doesn't work OOTB.
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style

  14. #14
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Michael Morris View Post
    There are ways to set environment variables in IIS. They're obscure and hard to work with, but they're there.
    You never used setx? Or this bit: http://www.iis.net/configreference/s...nmentvariables; If you do not want a GUI to set the environment variable, then you use the web.config file which is the equivalent of .htaccess.

    This is why Microsoft never made any real headway in the world of web servers - they're product is crap and they can't advertise their way out of the simple fact that IIS is a POS.
    Clearly you never used IIS6 or 7 and are just talking out of...the simple fact of the matter. IIS is not a POS. I've worked with both IIS and Apache regularly. Both are awesome web servers.

    Dave, which version of IIS? For IIS7+ The URL Rewrite module has yet to fail me: http://www.iis.net/downloads/microsoft/url-rewrite
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  15. #15
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,813
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    setx
    +1 from me. I use it a lot! setx is by far the easiest way (in my opinion) to set an environment variable.

    Quote Originally Posted by logic_earth View Post
    Clearly you never used IIS6 or 7 and are just talking out of...the simple fact of the matter. IIS is not a POS. I've worked with both IIS and Apache regularly. Both are awesome web servers.
    Another +1 (though I'm not a huge fan of 6, now that 7 is available). In 7 they may leaps and bounds to let you control IIS from the command line, and I love that! I keep a batch script now with every project so if a server fails, I just ran the batch script on the new server and the virtual directories, app pools, sites, are all setup automatically. Just have to get the code there and its able to run.

    Microsoft has made huge steps in the web server market over the past few releases, and I applaud them for their effort (it paid off).
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  16. #16
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,200
    Mentioned
    105 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Dave, which version of IIS? For IIS7+ The URL Rewrite module has yet to fail me: http://www.iis.net/downloads/microsoft/url-rewrite
    That would be why - I'm more familiar with IIS 5/6.
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style

  17. #17
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    515
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I've been using unindented debugging code without actually thinking about it. Since it's mostly echos and print_rs leaving it in stands out like a sore thumb so there is little danger of it being left behind. With non printing debugging code there is that danger.
    Denny Schlesinger
    web services

  18. #18
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    56 Post(s)
    Tagged
    0 Thread(s)
    The last time I was *forced* to look at an IIS server it was 4.

  19. #19
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    IIS 4, released in 1998 for Windows NT 4.0. Over 15 years ago. Yeah.......clearly IIS never had any updates during that time.

    Now for the topic at hand. If you had a smarter workflow, using xDebug (or equivalent) and a smart editor that allows you to set breakpoints and step though the code, you wouldn't need to throw debugging code into your code. Thus you never have the issue of leaving stray debugging code behind.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

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