Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Aiheen aloitti: davidhs - lokakuu 05, 2013, 08:37:58 AP

Otsikko: PHP coding guidelines
Kirjoitti: davidhs - lokakuu 05, 2013, 08:37:58 AP
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?
Otsikko: Re: PHP coding guidelines
Kirjoitti: Illori - lokakuu 05, 2013, 09:06:39 AP
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
Otsikko: Re: PHP coding guidelines
Kirjoitti: Arantor - lokakuu 05, 2013, 01:33:55 IP
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.
Otsikko: Re: PHP coding guidelines
Kirjoitti: Matthew K. - lokakuu 05, 2013, 04:17:10 IP
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.
Otsikko: Re: PHP coding guidelines
Kirjoitti: onepiece - lokakuu 05, 2013, 05:09:19 IP
Just to note, require and include are language constructs too.
Otsikko: Re: PHP coding guidelines
Kirjoitti: Arantor - lokakuu 05, 2013, 05:13:22 IP
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.
Otsikko: Re: PHP coding guidelines
Kirjoitti: Matthew K. - lokakuu 05, 2013, 05:38:02 IP
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.