NogDog;10856788 wrote:

Not like the good(?) old days writing BASIC programs on my TRS80 with 4KB of RAM (yes, that is four (4) kilobytes), where every unnecessary space risked making my program too big. And as for comments...who had room for them! 🙂

Oh man! lol.. And here I thought I had it rough progamming on my first computer (Commodore Vic 20 complete with casette drive [can't remember the size of cassettes back then.. but I think it was larger than 4kb... not sure about the Vic 20's RAM..I'll have to look that up, just for kicks 😉 ).

On a note with code formatting (rather, cutting and pasting sections of code from Dreamweaver.. if the code snippet in DW is indented (as obviously, we deal with nested divs, for statements, if statements, etc..as such, deeper levels of nested code appears further and further away from the edge of the screen) I find that pasting code from there to these forum message windows does tend to mess up code formatting from the left-hand spacing of the screen edge quite badly.

I also noticed that if I highlight the indented / nested code, and press SHIFT + TAB (this 'back-tabs' if you will, the code) multiple times till it is right at the start of the screen, copy and paste that, the code shows up more or less quite respectably in the message forum window. This little tip can save some frustrations when dealing with keeping code indented integrity (I know this is not quite the formatting of code that this thread intended.. but it is code formatting none-the-less).

Cheers,

NRG

EDIT - Apprently, my beloved Vic 20 had 5k ram.

    3 months later

    cutting and pasting sections of code from Dreamweaver..

    I also noticed this. Tab stops in Dreamweaver, regardless of how you've set them, equal about 10 spaces in the forum message window. On the other hand, if you've indented the code yourself (by spacing twice, for example), then it stays the same.

    I'd also agree with previous statements about code formatting. Formatting like this makes me dizzy:

    while(($row = mysql_fetch_assoc($rs)) != false){
    if(($row['Actual_Incentive'] + $row['CSP_Incentive'] + $row['Manual_Adjustment']) < $row['Guarantee_Amount']){
    if($row['GM_Dollar'] >= $row['GM_Quota']){
    $incentive = 0;
    $guarantee = $row['Guarantee_Amount'];
    }else{
    $incentive = $row['Actual_Incentive'] + $row['CSP_Incentive'] + $row['Manual_Adjustment'];
    $guarantee = -1;
    }
    }else{

    Etc...That's from another person's code I'm currently dealing with.

    I've come to basically the same formatting standard as mentioned above even though nobody ever told me, "this is the way to do it." The only variation I have is that I like to indent the braces because it "looks" cleaner to me:

    while($row = mysql_fetch_assoc($rs))
      {
      extract($row);
      if($Actual_Incentive + $CSP_Incentive + $Manual_Adjustment < $Guarantee_Amount)
        {
        if($GM_Dollar'] >= $GM_Quota)
          {
          $incentive = 0;
          $guarantee = $Guarantee_Amount;
          }
        else
          {
          $incentive = $Actual_Incentive + $CSP_Incentive + $Manual_Adjustment;
          $guarantee = -1;
          }
        }
      else
        {
        $incentive = $Actual_Incentive + $CSP_Incentive + $Manual_Adjustment;
        $guarantee = 0;
        }
      }

    The condition/loop statements stick out better - for me at least. But to each his/her own, as long as it is clean.

      nrg_alpha;10888405 wrote:

      Oh man! lol.. And here I thought I had it rough progamming on my first computer (Commodore Vic 20 complete with casette drive [can't remember the size of cassettes back then.. but I think it was larger than 4kb... not sure about the Vic 20's RAM..I'll have to look that up, just for kicks 😉 ).

      On a note with code formatting (rather, cutting and pasting sections of code from Dreamweaver.. if the code snippet in DW is indented (as obviously, we deal with nested divs, for statements, if statements, etc..as such, deeper levels of nested code appears further and further away from the edge of the screen) I find that pasting code from there to these forum message windows does tend to mess up code formatting from the left-hand spacing of the screen edge quite badly.

      I also noticed that if I highlight the indented / nested code, and press SHIFT + TAB (this 'back-tabs' if you will, the code) multiple times till it is right at the start of the screen, copy and paste that, the code shows up more or less quite respectably in the message forum window. This little tip can save some frustrations when dealing with keeping code indented integrity (I know this is not quite the formatting of code that this thread intended.. but it is code formatting none-the-less).

      Cheers,

      NRG

      EDIT - Apprently, my beloved Vic 20 had 5k ram.

      PHP Parse Error:  syntax error, unexpected T_STRING in showpost.php on line 7

      Looks like a problem with nested ( ) statements 😃

        PHP Parse Error: syntax error, unexpected T_STRING in showpost.php on line 7

        -- Haha!

        I was poking around today and found out that there are actually names for the styles we are talking about! My style is called "Whitesmiths" as opposed to the "GNU/Pear" style that others here have mentioned.

        If formatting your code is just too much trouble for you, try this site: PHP Formatter

        You can paste your code into the window, select your preferred style, and click a button and it will make your code all pretty for you. Pretty cool...

          I just type Ctrl-F in my editor and it does it for me.

            ixalmida wrote:

            You can paste your code into the window, select your preferred style, and click a button and it will make your code all pretty for you. Pretty cool...

            If you don't keep your code formatted while you're writing it - pretty much on every line you write as you write it - you're missing a big chunk of the reason why you should be formatting it.

            Using that site every time you type a line seems like a little too much trouble 🙂

              Using that site every time you type a line seems like a little too much trouble

              Agreed. However, if you're having to fix someone else's code the site can be really helpful.

              I inherited a mission-critical site. The previous developer wasn't really a developer. Not only did he not format his code, but he used modular pages (not to be confused with OOP), so it is impossible to change something without potentially affecting a bunch of other pages.

              For example, the main index page loads 6 different modules (layed out with Javascript), and those modules reference or load other modules based on a user's access level. When you click a link, it loads a new module not a new page.

              It gets complicated very quickly and there is NO documentation. It is also very slow in places, doesn't take advantage of CSS ,and is difficult to optimize. So I'm pretty much looking at rebuilding from scratch a financial site that took 3 years to build.

              (Sorry folks, just had to vent a little!)

              I just type Ctrl-F in my editor and it does it for me.

              Okay...could you enlighten us on what editor you're using? Ctrl-F is mapped to the "find" function in every editor I'm aware of.

                ixalmida;10898812 wrote:

                ...
                Okay...could you enlighten us on what editor you're using? Ctrl-F is mapped to the "find" function in every editor I'm aware of.

                Oops, should've been "Ctrl-Shift-F".

                phpDesigner is the editor/IDE. I don't remember if that was the default key-mapping for the format command or if I overrode it at some point. Aptana has a similar function, so I wouldn't be surprised if some of the other Eclipse-based IDE's also do.

                  NogDog wrote:

                  Aptana has a similar function, so I wouldn't be surprised if some of the other Eclipse-based IDE's also do.

                  You'd be right; the same keybinding is the default in Zend Eclipse and Eclipse itself.

                    4 months later

                    In relation to my previous post back in 2008 regarding correctly spacing your code to make it eaasily readable, I have found many articles supporting my recommendation for using in the Allman Style syntax formatting.

                    This is where the main rules are that each new curly brace goes on a new line.

                    I have also found this on expressionengine.com about general syntax formatting.

                    I particularly like this syntax style as it makes it incredibly easy to comment out braces to enable certain flow.

                    Allman style (bsd in Emacs)

                    Any views, comments, agreements, disagreements?

                      iceomnia wrote:

                      I have found many articles supporting my recommendation for using in the Allman Style syntax formatting.

                      If you have not found many articles supporting other indent styles, then either you have not searched hard enough, or the supporters are too busy coding to evangelise 😉

                      iceomnia wrote:

                      I have also found this on expressionengine.com about general syntax formatting.

                      So what? That is just a style guideline that I am not required to follow :p

                      iceomnia wrote:

                      I particularly like this syntax style as it makes it incredibly easy to comment out braces to enable certain flow.

                      That sounds dubious: why would you be commenting out braces?

                      I like it for a different reason, related to one cited by the "Technical Spartacus" blog that you linked to: I would rather not extend code horizontally, so I try to break to a new line after about the 80th column. This new line of code would then be indented by one level. Without the opening brace being on its own line, there would then be no visual cue as to the difference between the continuation of the previous line of a control statement and the first line of the block. (But note that some style guidelines suggest a two level indentation for continuation lines instead, in which case this "problem" would no longer apply. But I dislike the extra level of indentation.)

                      Of course, I disagree with Spartacus' implication that the Allman style is the "correct" indent style.

                        Yes, that standard was referred to in the thread iceomnia referred to.

                        Other threads on the subject can be found in the Echo Lounge (which it surprised me not to find in the thread)
                        http://board.phpbuilder.com/showthread.php?t=10256498 (which is actually in Code Critique, now)
                        http://board.phpbuilder.com/showthread.php?t=10298088
                        http://board.phpbuilder.com/showthread.php?t=10344137
                        http://board.phpbuilder.com/showthread.php?t=10234388

                        And this thread might as well be merged with the original under the current name.

                          laserlight;10913171 wrote:

                          If you have not found many articles supporting other indent styles, then either you have not searched hard enough, or the supporters are too busy coding to evangelise 😉

                          No I have it's just I can find so many more supporting reasons to adopt the Allman Style formatting.

                          laserlight;10913171 wrote:

                          So what? That is just a style guideline that I am not required to follow :p

                          Ha ha - I don't think that everyone should follow one style. But It is quite irritating when you are used to one style and a fellow (colleague) programmer codes in a completely different style.

                          laserlight;10913171 wrote:

                          I like it for a different reason, related to one cited by the "Technical Spartacus" blog that you linked to: I would rather not extend code horizontally, so I try to break to a new line after about the 80th column. This new line of code would then be indented by one level. Without the opening brace being on its own line, there would then be no visual cue as to the difference between the continuation of the previous line of a control statement and the first line of the block. (But note that some style guidelines suggest a two level indentation for continuation lines instead, in which case this "problem" would no longer apply. But I dislike the extra level of indentation.)

                          Of course, I disagree with Spartacus' implication that the Allman style is the "correct" indent style.

                          I don't agree either that the Allman Style is "CORRECT". However, I have adopted it as I find it makes the code feel the neatest and it seems to flow better.

                          Also I use Zend Studio for Eclipse and I have set up my ccode formatter to use the Allman Style by default.

                          Bjom;10913174 wrote:

                          http://en.wikipedia.org/wiki/Indent_style
                          I found this following your links iceomnia - there is some variety out there as laserlight already pointed out.

                          Btw: we are coding PHP here, so imho using Zend Coding Standard seems like a relatively intuitive choice.
                          Bjom

                          I agree that the Zend syntax formatting method is pretty adoptable, but they don't tend to put " if statement braces " on a new line which would make it difficult to match up the corresponding braces.

                            ixalmida;10898749 wrote:

                            -- Haha!

                            I was poking around today and found out that there are actually names for the styles we are talking about! My style is called "Whitesmiths" as opposed to the "GNU/Pear" style that others here have mentioned.

                            If formatting your code is just too much trouble for you, try this site: PHP Formatter

                            You can paste your code into the window, select your preferred style, and click a button and it will make your code all pretty for you. Pretty cool...

                            I only just noticed that poost! - That is an amazing tool!

                            I just tried it with some really sloppy code and it formatted it perfectly (sorry - formatted it favourably).

                            Although - it does look like it may be indenting by two spaces and my adpoted standard is 4.

                              iceomnia wrote:

                              No I have it's just I can find so many more supporting reasons to adopt the Allman Style formatting.

                              I am cautious about such evaluations, because for these kind of personal preference issues, we especially tend to trump up what suits our preference and downplay what does not. Indeed, what we consider important may be considered inconsequential by others.

                              iceomnia wrote:

                              But It is quite irritating when you are used to one style and a fellow (colleague) programmer codes in a completely different style.

                              Sure, but minor issues of style are like that, so you should just get used to the variance. I have written code for a project whose core developers prefer tabs to spaces. Change my editor setting and move on. I try my best to match the style of users of this forum when replying to their posts, assuming that they actually use a consistent and reasonable style.

                              Yes, I have a hard time writing code to meet Whitesmiths style indenting, but that is because I have never been involved in a project where that was the preference, so I lack practice. Reading? No problem, as long as it is well indented.

                              iceomnia wrote:

                              I agree that the Zend syntax formatting method is pretty adoptable, but they don't tend to put " if statement braces " on a new line which would make it difficult to match up the corresponding braces.

                              There is no need "to match up the corresponding braces". For one thing, the indentation itself is enough. If not, then a good editor with code folding can be used. This is an example of "what we consider important may be considered inconsequential by others" 😉

                                iceomnia wrote:

                                I agree that the Zend syntax formatting method is pretty adoptable, but they don't tend to put " if statement braces " on a new line which would make it difficult to match up the corresponding braces.

                                Works for me. Mostly: it refuses to put "else" and "elseif" on a new line - they always end up at the end of the previous line even if the previous line isn't a "}". Either that or I just haven't found that setting (despite fiddling with every setting I could find). It's like the "Braces" settings only apply to opening braces.

                                laserlight wrote:

                                There is no need "to match up the corresponding braces". For one thing, the indentation itself is enough. If not, then a good editor with code folding can be used.

                                And that shouldn't be necessary too often, either. Below the function/class level, I think about the only time I have more than twenty lines between braces is when I'm doing a switch statement; and then there are the labels to break things up.

                                  7 months later

                                  A Zend Studio user here, too. Hot-keyed code formatting that can be customized serves nearly all required purposes, in my personal case. As others have indicated, the Zend formatter is not perfect, but it works well enough for most.

                                  The big one for me is being able to re-format entire "projects" at once. It makes dealing with other peoples' shoddy code less cumbersome in that one can "convert" an entire project to his or her own style of preference and return the "finished" code in the "project's" preferred format. Simple, elegant.

                                  A couple of screen caps of the interface are attached for those who are interested.

                                    6 months later

                                    One thing I like to do is especially mark out new sections with comment lines. While this may seem obvious, when you start writing code into the thousands of lines, you need sections to really stand out. So, at the beginning and end of major sections, I'll comment something like this:

                                    /*** Some_New_Section ***/
                                    // some code here...
                                    /*** End Some_New_Section ***/
                                    

                                    The larger or more important the section, the more asterisks I use to delineate it. That way, when I'm quickly paging through the code, I can immediately see where I'm at.

                                    Another trick I sometimes do, when I have a lot of MySQL queries, is to put a unique marker in the die statement, like this:

                                    $result=mysql($query)
                                      or die("Internal Error XQ93: MySQL Query failed with this message: ".mysql_error());
                                    

                                    That way, I can do a quick search on "XQ93" and get immediately to the query in question...

                                      jkurrle;10953029 wrote:

                                      One thing I like to do is especially mark out new sections with comment lines. While this may seem obvious, when you start writing code into the thousands of lines, you need sections to really stand out. So, at the beginning and end of major sections, I'll comment something like this:

                                      /*** Some_New_Section ***/
                                      // some code here...
                                      /*** End Some_New_Section ***/
                                      

                                      The larger or more important the section, the more asterisks I use to delineate it. That way, when I'm quickly paging through the code, I can immediately see where I'm at....

                                      I'd prefer to just modularize the code more so that none of source files even comes close to a thousand lines. 😉