Uutiset:

Join the Facebook Fan Page.

Main Menu
Advertisement:

PHP coding guidelines

Aloittaja davidhs, lokakuu 05, 2013, 08:37:58 AP

« edellinen - seuraava »

davidhs

I read coding guidelines
http://wiki.simplemachines.org/smf/Coding_Guidelines#Whitespace_2
for apply to my MODs, but I found some not applied in SMF 2.0.5.

LainaaUse braces (curly brackets) for switch ... case and every other time there are multiple statements.
Accordingly should be written like this
switch ($foo)
{
case 'foo1':
{
statement11;
statement12;
statement13;
break;
}
case 'foo2':
{
statement21;
statement22;
statement23;
break;
}
}

but in SMF is written like this
switch ($foo)
{
case 'foo1':
statement11;
statement12;
statement13;
break;
case 'foo2':
statement21;
statement22;
statement23;
break;
}

I use this second way but if the correct is first way, I will apply.

LainaaUse one space after language constructs such as echo, list, require, etc.
Accordingly should be written like this
echo 'foo';
list ($foo1, $foo2, $foo3) = $foo_array;
require_once ('foo');

but in SMF is written like this
echo 'foo'; // Follows the rule
list($foo1, $foo2, $foo3) = $foo_array; // Not follow the rule
require_once('foo'); // Not follow the rule
require_once ('foo'); // Follows the rule


These guidelines are correct?

Illori

from what i understand those are guidelines that are for coding smf and not mods [although they may have changed over time] the guidelines for mods are http://wiki.simplemachines.org/smf/Customization_approval_guidelines

Arantor

Guidelines are that: *guide* lines. They are an ideal to strive towards, not a set of rules that must at all times be obeyed.

Following them is good but it really isn't a big deal if they aren't followed perfectly as long as the important parts are done.
Holder of controversial views, all of which my own.


Matthew K.

Furthermore, for functions (such as require/include) the general style is no space between the function name and the parenthesis. So...require_once($sourcedir . '/Subs-Editor.php'); for instance would be "correct", while require_once ($sourcedir . '/Subs-Editor.php'); would be "incorrect". Note for echo ''; which is a language construct and not directly a function (in the sense of you aren't using parenthesis, mainly) there is a space. That's just common practice within SMF. Same thing goes for variable concatenation, a lot of people use periods where commas are appropriate, personally, I like keeping all of those consistent and only using periods where commas can't go for obvious reasons, however, it's not required, which is also why I used quotation marks above for "correct" and "incorrect", because frankly either way will do the same thing, but there's something to be said about good clean, consistent coding.

onepiece

Just to note, require and include are language constructs too.

Arantor

Yup, you don't even need the parentheses for require/include/_once variants thereof.

I'm just not that big on the guidelines; guidelines are great but being hard-ass about them doesn't do anyone any favours.
Holder of controversial views, all of which my own.


Matthew K.

Lainaus käyttäjältä: onepiece - lokakuu 05, 2013, 05:09:19 IP
Just to note, require and include are language constructs too.
Yes. You're right. Thank you.

Advertisement: