Elev8;10996275 wrote:

I hate wasting lines of code with single brackets where they are not needed.

If a conditional (as well as loops) only has one statement it doesn't need braces at all.

if ($bar == true) $something = true;

I personally prefer to put it on the next line simply because it's a flow control structure and it makes the code look like it's literally flowing.

    Bonesnap;10996294 wrote:

    If a conditional (as well as loops) only has one statement it doesn't need braces at all.

    if ($bar == true) $something = true;

    I personally prefer to put it on the next line simply because it's a flow control structure and it makes the code look like it's literally flowing.

    I was gonna say the exact same thing. Of course I still generally use two lines, with indentation:

    if( $bar == TRUE ) 
       $something = TRUE;
    Elev8;10996275 wrote:

    I hate wasting lines of code with single brackets where they are not needed.
    It may seem like a small change but saves a lot of scrolling when you develop fully fledged systems.

    You know what saves even more scrolling? Use a good editor that supports Code Folding

      Derokorian;10996298 wrote:

      I was gonna say the exact same thing. Of course I still generally use two lines, with indentation:

      if( $bar == TRUE ) 
         $something = TRUE;

      You know what saves even more scrolling? Use a good editor that supports Code Folding

      Firstly, using braces allows for easy expansion at a later date without the need for trying to find find the end point.
      It does work well in template code, similar to WordPress' layout...

      <?php if($x=$y) : ?>
      Random HTML
      <?php else : ?>
      Other HTML
      <?php endif; ?>

      Apart from that case, I find the atypical aspect of using it arbitrarily can cause confusion.

      Secondly, code folding is just terrible IMO.
      Both Dreamweaver and Notepad++ use "folding", but it's just such a bad habit to get into.

      Maybe it's just a bad habit I have developed after being a notepad coder for 15 years.
      Either way, I never have, and never will, use code folding.

        I use code folding to collapse documentation out of the way, but I don't use it for pairs of braces. Instead I avoid letting the code between them get too long and hairy in the first place. If I find myself checking braces I take it as a sign that "here be long and hairy code" threatens and I work on sorting it out.

        I have braces on their own line so that it takes less conscious effort to pair them visually, raising the threshold where I start deliberately checking. Using a 7-pt font further reduces the amount of eyeball roving involved.

          How is code folding a bad habit? When looking for a certain function to update I don't even have to scroll, I just expand that function only...

          Also since I tend to comment the way PHPDocumentor says to (which is supported for parsing by my editor) its super nice to get rid of those 40 lines of comments and get to just the code.

            Elev8;10996351 wrote:

            Firstly, using braces allows for easy expansion at a later date without the need for trying to find find the end point.

            Why would you need to "try and find" the end point? The end point is the very next line...

            Elev8;10996351 wrote:

            It does work well in template code, similar to WordPress' layout...

            <?php if($x=$y) : ?>
            Random HTML
            <?php else : ?>
            Other HTML
            <?php endif; ?>

            I can't stand the way WordPress does this in their template code. I usually end up modifying it to use braces, or remove it entirely and do my own thing.

            Elev8;10996351 wrote:

            Secondly, code folding is just terrible IMO.
            Both Dreamweaver and Notepad++ use "folding", but it's just such a bad habit to get into.

            How is it terrible and how is it a bad habit? 😕

            Weedpacket;10996354 wrote:

            I use code folding to collapse documentation out of the way, but I don't use it for pairs of braces. Instead I avoid letting the code between them get too long and hairy in the first place. If I find myself checking braces I take it as a sign that "here be long and hairy code" threatens and I work on sorting it out.

            I'll use it for functions, especially if I have a PHP file that has a bunch of functions in it. Most editors allow you to hover over the collapsed section and a small "tooltip" of sorts shows you the code that's collapsed, so you can at a glance see the function's code (at least part of it, anyway). I also never find myself "checking" braces since my editor highlights matching braces.

            Weedpacket;10996354 wrote:

            I have braces on their own line so that it takes less conscious effort to pair them visually, raising the threshold where I start deliberately checking.

            I also put braces on their own line; I feel it makes it not only easier to read, but it also gives literal meaning to "code block".

            Weedpacket;10996354 wrote:

            Using a 7-pt font further reduces the amount of eyeball roving involved.

            My eyes are bad so I code in size 14pt font. :eek:

            Derokorian;10996381 wrote:

            Also since I tend to comment the way PHPDocumentor says to (which is supported for parsing by my editor) its super nice to get rid of those 40 lines of comments and get to just the code.

            +1 for phpDocumentor comments.

              Using a 7-pt font further reduces the amount of eyeball roving involved.

              My eyes are bad so I code in size 14pt font.

              I have no idea what size font I use, I tend to use the ctrl+mouse scroll a lot to make it bigger or smaller. I tend to make it small to find what I'm looking for then bigger to change it...

                Bonesnap;10996583 wrote:

                Why would you need to "try and find" the end point? The end point is the very next line...

                I have never, ever used:

                if ($bar == true) $something = true; 

                without it expanding to include other code at a later stage.

                I thought that's what the condensed conditional statement was for?

                $var = (1 == 2) ? "A" : "B";

                I didn't say it was right or wrong, just my personal preference after many years of coding in notepad.
                I have severe OCD so like my code to be as small and readable as possible.
                I am also an autodidact, so my definition of readable probably isn't the same as yours.

                  Elev8;10996923 wrote:

                  I have never, ever used:

                  if ($bar == true) $something = true; 

                  without it expanding to include other code at a later stage.

                  I tend to agree.

                  However, this is a non-issue for me, since here at work the practice of leaving off {}'s for one-liners like that is expressly prohibited in the coding standards (which is not an uncommon practice).

                  Can't same I blame the authors of the standard for doing so, either.

                    I use it a lot for concatenation, especially in my models. This way, I can build queries based on function arguments, so I end up with a few lines of:

                    if( !is_null($group) ) $sql .= ' AND group = :group';
                    // ... after preparing
                    if( !is_null($group) ) $stmt->bindParam(':group',   $group,   PDO::PARAM_INT);
                    

                    Of course I don't work for a development company (though I would like to) so this is my personal preference.

                      6 days later
                      Elev8;10996923 wrote:

                      I have never, ever used:

                      if ($bar == true) $something = true; 

                      without it expanding to include other code at a later stage.

                      I thought that's what the condensed conditional statement was for?

                      $var = (1 == 2) ? "A" : "B";

                      I didn't say it was right or wrong, just my personal preference after many years of coding in notepad.
                      I have severe OCD so like my code to be as small and readable as possible.
                      I am also an autodidact, so my definition of readable probably isn't the same as yours.

                      All I said was that you didn't have to "try and find the end point" since the "end point" would be the very next line. Perhaps I misinterpreted, but I got the feeling from reading what you wrote that it was somehow difficult to determine when the conditional ended.

                      Also, I usually write it as...

                      if ($bar == true)
                          $something = true;
                      

                      ...simply because it gives literal meaning to "program flow". Also, that way if I do have to add additional lines at a later point it requires minimal effort.

                      Lastly, the two coding examples you gave are completely different and the first one would never work as a ternary operator since there's no value being assigned if the condition is false. Sometimes all you want to do is set something to true, or false, or whatever; there's not always an opposite.

                        Bonesnap;10997368 wrote:

                        the first one would never work as a ternary operator since there's no value being assigned if the condition is false.

                        Oh really?

                        if ($bar == true) $something = true;
                        
                        // same as:
                        
                        $bar != true ?: $something = true;

                        (Regardless, I still consider either one to be more obfuscated than it is efficient, thus doing more harm than good.)

                          bradgrafelman;10997371 wrote:

                          Oh really?

                          if ($bar == true) $something = true;
                          
                          // same as:
                          
                          $bar != true ?: $something = true;

                          Definitely never seen a ternary operator written like that before. Interesting. :eek:

                          bradgrafelman;10997371 wrote:

                          (Regardless, I still consider either one to be more obfuscated than it is efficient, thus doing more harm than good.)

                          So you never use ternary operators?

                            Bonesnap;10997375 wrote:

                            So you never use ternary operators?

                            Depends on where I'm coding. Here at work (or any other place I've been that has official coding standards), the answer is: no. In the hundreds of thousands of lines of code (perhaps more), you will never find a) the ternary operator, or even b) an if() statement without {} braces.

                            If it's just my own personal code (which has been rather non-existent recently), the answer is: occasionally.

                              Bonesnap wrote:

                              Definitely never seen a ternary operator written like that before. Interesting.

                              I still maintain "ternary operator" is a silly name for it, and the name for it in C ("conditional operator") much better describes its function.

                              Especially since [font=monospace]$foo ?: $bar[/font] is a binary operation.

                              Which, not incidentally, is basically equivalent to the [font=monospace]||[/font] of JavaScript or the [font=monospace]or[/font] of Python. Unfortunately, PHP as yet has no equivalent of their [font=monospace]&&[/font] or [font=monospace]and[/font].

                                5 months later
                                Horizon88;10856253 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.

                                What's wrong with using an IDE that formats it for you and then paste it into the field. Instead of using Notepad why not use Netbeans?

                                  Bonesnap;10996583 wrote:

                                  My eyes are bad so I code in size 14pt font. :eek:

                                  I've been setting my terminals to that a lot more often of late ... sigh.

                                    6 days later
                                    dalecosp;11008841 wrote:

                                    I've been setting my terminals to that a lot more often of late ... sigh.

                                    I'm only 27... 🙁

                                    Though since switching to Komodo Edit I've actually gone down one size and now code at 13pt size, though there are times I'd like to increase it.

                                      2 months later

                                      Write down like below this is proper way to write code:: 🙂
                                      // Correctly spacing a class with tabs to make it readable.
                                      class foo{
                                      public $somthing;

                                      function __construct(){
                                          // Constructor
                                      }
                                      
                                      public function bar($bool){
                                          if ($bool == true)
                                          {
                                              $this->something = true;
                                          }
                                      
                                          return $this->something;
                                      }

                                      }