1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

XenForo PSR-2

Discussion in 'General XenForo Discussion and Feedback' started by Jake B., Oct 22, 2015.

  1. Jake B.

    Jake B. Well-Known Member

    I've always wondered, Why doesn't XenForo follow the PSR-2 coding style? I've also noticed that different parts of code are styled differently. For example, I've seen places that align all of the fat arrows in arrays, and other places that don't, I believe I've even seen some places that use spaces and others that use tabs, and some places have the beginning curly brace on the same line as the if statement, and places that don't do this. I'll perhaps try to find some specific examples of all of these, but was just a bit curious about this :p
     
  2. Liam W

    Liam W Well-Known Member

    Don't all the dev's use different IDE's? They've probably never synchronised their code styles fully... :)

    Liam
     
  3. Jake B.

    Jake B. Well-Known Member

    As far as I know Mike uses phpStorm, and I'm fairly certain Chris does as well as he's the one that suggested it to me. For PSR-2 it's just a matter of selecting an option in a dropdown box and it takes care of everything. The only change I make to it is to use tabs instead of spaces, and that's only because spaces are aggravating :p
     
  4. Mike

    Mike XenForo Developer Staff Member

    The code predates PSR-2 anyway, though I don't really like some of the decisions/mandates in it either.
     
  5. Dakota Storm

    Dakota Storm Well-Known Member

    I always preferred PhpDesigner. It's not free, but it's worth it.
     
  6. Jake B.

    Jake B. Well-Known Member

    Does it? Fair enough I suppose :p
     
  7. Chris D

    Chris D XenForo Developer Staff Member

    I'm not totally sure when PSR-2 was accepted, but bearing in mind the first beta releases of XF were 5 years ago, it must surely pre-date that.

    That said, I hate PSR-2.
    PHP:
    <?php
    if ($expr1) {
        
    // if body
    } elseif ($expr2) {
        
    // elseif body
    } else {
        
    // else body;
    }
    Noooooooo! Seems really petty, but this is the worst for me.

    I think Mike perhaps agrees with me on that, too, and likely some of the others. Seems like a bad decision to me.

    We do actually all use PhpStorm so we could probably standardise a few things quite easily (for XF 2) but hopefully not PSR-2 ;)
     
  8. Jake B.

    Jake B. Well-Known Member

    I'm somewhat curious of why this is, even before I found out about PSR-2 I did this. Even tried "conforming" to doing
    PHP:
    if (true)
    {
        
    // Do this
    }
    when I started working on XenForo Add-ons, but it drove me mad so I switched back after a few months :p
     
  9. Daniel Hood

    Daniel Hood Well-Known Member

    I do it the same way as Chris and I can't stand the bracket being on the same line as the if condition. At this point, it's just because I'm used to it. I also think it's beneficial when skimming code because I can look at the farthest left character and tell where things start easily. When the { is on the first line, it seems as if the } is closing something that hasn't been started from a quick glance.
     
  10. Brogan

    Brogan XenForo Moderator Staff Member

    ^ That.
     
    Daniel Hood likes this.
  11. Jake B.

    Jake B. Well-Known Member

    @Daniel Hood I think both you and @Chris D would hate Ruby. No curly braces at all.

    PHP:
    /*
      * This is a conditional
      * and it does something
      */
    if ($thisVar === null) {
        
    $thatVar 'this';
    }
    turns into...

    Code:
    =begin
    This is a conditional
    and it does something
    =end
    if this_var.nil?
        that_var = 'this'
    end
    
    No semicolons either, and you generally don't use camel case for variable names either :p
     
  12. Chris D

    Chris D XenForo Developer Staff Member

    Never used Ruby.

    iOS Swift has sort of gone in that direction, though - generally you don't need braces, but you can still use them if you want.
     
  13. Daniel Hood

    Daniel Hood Well-Known Member

    I can actually deal with
    =being
    and
    =end since the first character symbolizes the start and finish in both lines ;)

    I can also work without the brackets
    PHP:
    if (true)
     do();
    else
     
    dont();
    the thing is, if the closing symbol exists, I want the beginning symbol to exist in the same spot.
     
  14. Jake B.

    Jake B. Well-Known Member

    This is the one that really drives me crazy. that is the only way to do a block comment, AND the =begin and =end have to be on the far left, no whitespace before them (including tabs).

    You definitely get used to it. I can't stand not using curly braces in PHP though. That's how you end up with things like this
     
  15. Daniel Hood

    Daniel Hood Well-Known Member

    I always use curly braces, I'm just saying that I can read
    PHP:
    if (true)
       
    yes();
    else
       
    no();
    easier than I can read
    PHP:
    if (true) {
      
    yes();
    } else {
      
    no();
    }
    but I still really want
    PHP:
    if (true)
    {
       
    yes();
    }
    else
    {
       
    no();
    }
    the whitespace part is a deal breaker.
     
  16. Jake B.

    Jake B. Well-Known Member

    I like it, even with all of its quirks. Rails is pretty awesome, and makes it really easy to do just about anything.

    Haven't had a chance to play with Swift, definitely looks good from what I've seen though.

    Yeah, this one has really been driving me mad. Also the way you define private methods in Ruby is ridiculous.

    Example:

    Code:
    class User < ActiveRecord::Base
    =begin
      This method sets the last active date to now.
    =end
      def set_last_active
        self.last_active = Time.now
        self.save!
      end
    
    =begin
      This method checks if the user is currently online
    =end
      def is_online?
        if self.last_active > 15.minutes.ago
          return true
        end
        return false
      end
    
      private
    
      def some_method
        # This method does something
      end
    
      def another_method
        # This method does something else.
      end
    end
    
    everything after 'private' is a private method, so some_method and another_method are both private.

    If you're creating a method that is a setter that you pass paramters to (not like the set_last_active method above) you have to suffix it with an equal sign, if it's a getter that will return a boolean it has to be suffixed with a question mark, and any other getter doesn't get a suffix

    Also with Ruby you're supposed to indent using two spaces ;)

    So many rules, and so many things that will break if you don't follow them exactly. :|
     
  17. whynot

    whynot Well-Known Member

    Clear.
    Good choice.

    I use it this way in C++
     

Share This Page