phpBB

Development Discussion Board

phpBB's testing ground of bleeding edge code
Advanced search

[RFC & Patch][Implemented] Coding Guidelines

Publish your own request for comments or patches for phpBB4. Discuss the contributions and proposals of others.
Forum rules
Information on how to create an RFC and a list of current RFCs can be found at http://wiki.phpbb.com/PhpBB4/RFC

Re: [RFC & Patch] Coding Guidelines

Postby bantu » Fri Jun 18, 2010 12:17 pm

bantu wrote:I'd like to suggest to add a rule that the last line of a text file should end with "\n".

Example

Changing
Code: Select all
..."\n"
$someVar = false; 
to
Code: Select all
..."\n"
$someVar = false;"\n"
$someOtherVar = false; 
will list the $someVar-Line as modified in the repository, which should not be.

It should be
Code: Select all
..."\n"
$someVar = false;"\n" 
from the beginning, so you can easily add
Code: Select all
$someOtherVar = false;"\n" 


For the same reason
Code: Select all
array(
    'key1'    => 'value1',
    'key2'    => 'value2'
) 
should always be written as
Code: Select all
array(
    'key1'    => 'value1',
    'key2'    => 'value2',
)
 

Note the comma on the last line in the array.

___

On a side note
Code: Select all
array('key1' => 'value1', 'key2' => 'value2',); 
should probably always be written as
Code: Select all
array('key1' => 'value1', 'key2' => 'value2'); 
as a convention (without a real reason, maybe for readability).
User avatar
bantu
3.0 Release Manager
3.0 Release Manager
 
Posts: 437
Joined: Thu Sep 07, 2006 11:22 am
Location: Karlsruhe, Germany

Re: [RFC & Patch] Coding Guidelines

Postby Erik Frèrejean » Fri Jun 18, 2010 4:20 pm

bantu wrote:For the same reason
Code: Select all
array(
    'key1'    => 'value1',
    'key2'    => 'value2'
) 
should always be written as
Code: Select all
array(
    'key1'    => 'value1',
    'key2'    => 'value2',
)
 

Note the comma on the last line in the array.

I'm really in favor of this, I've found it always annoying++ that not all arrays in phpBB have a comma at the last entry. Makes it annoying to add stuff to the array.
Available on .com
Support Toolkit developer
User avatar
Erik Frèrejean
Registered User
 
Posts: 207
Joined: Thu Oct 25, 2007 2:25 pm
Location: surfnet

Re: [RFC & Patch][Implemented] Coding Guidelines

Postby Michaelo » Mon Jul 12, 2010 1:43 pm

One other advantage... simplifies search updates...
Always better to use Search > Add, after as opposed to search, in-line find, in-line add after etc... and all for a comma!
Michaelo
Registered User
 
Posts: 103
Joined: Thu Apr 01, 2004 7:56 am
Location: Dublin

Re: [RFC & Patch][Implemented] Coding Guidelines

Postby bantu » Mon Jul 12, 2010 10:08 pm

Michaelo wrote:One other advantage... simplifies search updates...
Always better to use Search > Add, after as opposed to search, in-line find, in-line add after etc... and all for a comma!

That's not a valid point as we want to get rid of this whole core-edit thing.
User avatar
bantu
3.0 Release Manager
3.0 Release Manager
 
Posts: 437
Joined: Thu Sep 07, 2006 11:22 am
Location: Karlsruhe, Germany

Re: [RFC & Patch][Implemented] Coding Guidelines

Postby Michaelo » Wed Jul 14, 2010 11:54 pm

True in relation to manual editing... but assuming the core may require additional elements to be added during normal updating, the comma will still make it easier...
Michaelo
Registered User
 
Posts: 103
Joined: Thu Apr 01, 2004 7:56 am
Location: Dublin

Re: [RFC & Patch][Implemented] Coding Guidelines

Postby callumacrae » Wed Jan 04, 2012 6:10 pm

This one is a bit of a holy war, but we're going to use a style that can be summed up in one sentence: Braces always go on their own line.


Needs to be changed to:

This one is a bit of a holy war, but we're going to use a style that can be summed up in one [s]sentence[/s]paragraph:

Braces always go on their own line, with several major exceptions when writing JavaScript. When returning an object, you should obviously have braces on the same line, as due to JavaScript's implied semicolon insertion mechanism, the following code is invalid:

Code: Select all
function func() {
    return
    {
        foo: "bar"
    };
}


In that example, the function would return "undefined". For this reason, you should have the braces on the same line when returning objects. You should also have braces on the same line when defining variables as objects, as while it is still valid, it is difficult to read.

When defining anonymous functions, you should have braces on the same line, or the code is difficult to read. However, you should still have the braces on their own line when defining functions traditionally (function funcName()).



Seriously though - while I can understand that where to have braces in PHP is purely a matter of personal preference - depending on what you're used to, either look good - in a language such as JavaScript, it simply does not work to have braces on their own line. It can result in code like the following:

Code: Select all
/**
 * Returns a random function.
 *
 * @constructor
 */
function MyFunc(a)
{
   var i = 0;
   if (a)
   {
      return function() {
         return {
            i: i += 1
         }
      }
   }
   else
   {
      return function() {
         return {
            i: i += 2
         }
      }
   }
}


The code doesn't do much, but you can that it looks fairly inconsistent and stupid compared to how I would write it:

Code: Select all
/**
 * Returns a random function.
 *
 * @constructor
 */
function MyFunc(a) {
   var i = 0;
   if (a) {
      return function() {
         return {
            i: i += 1
         }
      }
   } else {
      return function() {
         return {
            i: i += 2
         }
      }
   }
}



So I think that all JavaScript contained in the phpBB package should have the braces on the same line. However, as having JavaScript and PHP using different coding standards would be confusing and where the braces are in PHP are purely down to personal preference, I think that the entire phpBB project should switch to having braces on the same line.

In summary:

Advantages to having braces on same line:
- Doesn't look stupid, and is more consistent.
- It's usually more widely used, so developers will be more likely to be used to it.

Advantages to having braces on own line:
- No change, so nothing would need to be changed. However, as phpBB would be having a full rewrite, this doesn't seem like a major problem.
- Symfony has the braces on own line.



I would also suggest an amendment to the Method & Function Names section; functions, when used a constructor in a Javascript, should start with a capital letter.
"In JavaScript, there is a beautiful, elegant, highly expressive language that is buried under a steaming pile of good intentions and blunders"
—Douglas Crockford

View my MOD, phpBB Mobile
User avatar
callumacrae
Website Team
Website Team
 
Posts: 882
Joined: Tue Apr 27, 2010 9:37 am
Location: England

Previous

Return to [4.x] RFCs

Who is online

Users browsing this forum: No registered users and 3 guests