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. 😉

                          I agree with that sentiment, but I don't always have the freedom to write as I want. I once tried creating a simple MySQL class for server/database connections and running queries and they almost had a cow! Sometimes you alter legacy code and sometimes it alters you...

                            Or you can use a language-sensitive editor that can fold code.

                              5 months later

                              Hi! I'm from London!
                              Thanks for sharing this information!

                                20 days later
                                laserlight;10856214 wrote:

                                Unless there are objections or suggestions otherwise, I am making this a sticky.

                                Note that style guidelines are always subjective. Feel free to use a different style, as long as you are consistent and keep to the spirit of what iceomnia suggested.

                                I'd like to note that, on my system at least, when I'm posting code manually here and hit Tab it skips on to the submit button, or the next field in the form, so if you're posting a sizable chunk of code manually, perhaps do it in Notepad or another simple text editor beforehand and then paste it in.

                                  ellyka112;10968973 wrote:

                                  I'd like to note that, on my system at least, when I'm posting code manually here and hit Tab it skips on to the submit button, or the next field in the form, so if you're posting a sizable chunk of code manually, perhaps do it in Notepad or another simple text editor beforehand and then paste it in.

                                  Well then why not tell your editor to replace tabs with 4 spaces.

                                  You should be using 4 spaces anyway as this is the industry standard as tabs can be inerpreted differently in different programs.

                                    iceomnia wrote:

                                    Well then why not tell your editor to replace tabs with 4 spaces.

                                    That won't solve the problem since this jump of focus by a tab is a feature of the web browser, not the editor.

                                    iceomnia wrote:

                                    You should be using 4 spaces anyway as this is the industry standard as tabs can be inerpreted differently in different programs.

                                    I am not convinced that the use of 4 spaces, or even the use of spaces rather than actual tab characters for indentation, is "industry standard". Various settings related to indentation can be configured in many editors, so the problem is largely a one of consistency within the file, and perhaps within the project or team.

                                      laserlight;10969004 wrote:

                                      Originally Posted by iceomnia
                                      Well then why not tell your editor to replace tabs with 4 spaces.

                                      That won't solve the problem since this jump of focus by a tab is a feature of the web browser, not the editor.

                                      Ha ha - well I'm talking about when code is copied and pasted inot this website.

                                      If you are just tying into the box - use 4 spaces, or 8 or 16, or whatever - just be consistent.

                                      <?php
                                      
                                      class Indented
                                      {
                                          public function indentedFourSpaces($indented)
                                          {
                                              if ($indented == true) {
                                                  return true;
                                              }
                                          }
                                      }
                                      ?>