Coding Standards

In my opinion the space after "if" is much better.

It could be my math background, but I never use a space after a function call: f(x) not f (x).

But “if” isn’t a function, it’s a condition… so space.

If is control structure, condition inside it. In php a lot of control structures (foreach, for, while, …). Spaces are needed after all? It is much easier to not put a space after them. Why complicate style? It’s pointless and there is no advantage. Especially considering that from Yii 1.0.0 there is no spaces after control structures. And this is great, it is practical, for which many thanks Qiang. I love Yii style as it is, i learned this style, i write all my extensions using this style, all applications. And I do not think that I alone :) Moreover, I am sure that most of us. So why should we change it?

Some of the changes in style are justified (for code folding, for debugging, for readability), but spaces after control structures never. There is no rational arguments why they should be.

P.S. Style should be such that it was possible to justify every detail and aspect. Because it leads to discipline. Discipline leads to order. Order leads to perfective code. Perfect code leads to immortality. This is of course a joke, but you get the idea.

Readthatunspacedcodeyourselfplease!

Please refrain yourself from comments like this…

The example you gave is not the same as if to use one space after if or not…

But that’s exactly how code looks when a it’s done. Yes, maybe simple code like get data from db and pass it to the views will look fine without spaces, but framework code isn’t so simple. And I tend to do projects where simple code is not in abundance and most business logic is not trivial leading to tons of if’s, foreach’es, function/method call and so on. And believe me, when you open that code even after a few weeks - you want it be perfectly easy readable.

Reading something like


if($some_var>10||$something=='somethingelse'){

is a major headache even reading your own code after a while. Reading someone’s code in that style is just ridiculous - I have to reformat it all over to make sense of it.

Code readability is built on a ton of tiny things that become what we call readability. And we, humans, are all built the same way and we read the same way. You can train and make yourself to read anything - some can read hexadecimal dumps as it was plain text, but that doesn’t mean that it’s a readable form, don’t you agree?

Separating items in code is one of those things that make reading code easier. You don’t have books witch do not have spaces between some words of phrases. Here is the same thing - it is easier to process separated by space elements that a string with no spaces and trying to identify where there is what.

And for example about the braces.

I use them like this:




class MyClass

{

    public function test()

    {

        if ($expression) {

            // code

        }

        

        foreach ($item as $key => $value) {

            // code

        }

    }

}


function ()

{

    // code

}



Why? Because when I skim the code, I can identify what the block of code is by the the layout of the braces. If braces are on their own lines - that’s method/function/class, in other case is a conditional block or a loop. That really helps a lot.

Read again the above posts… it’s not about spaces inside the condition… it’s just about the space after the if…


if($some_var > 10 || $something == 'somethingelse')

or


if ($some_var > 10 || $something == 'somethingelse')

You could write everything without spaces (and newlines too… just write everything inline, semicolon separated), the argument is readability. Like I’ve already said, I just avoid spaces for function calls because in math I never used them… f(x) and not f (x)… but “if” is not a function.

If you want to avoid useless things, maybe you can avoid to use useless braces for single line statements just to workaround a debugger issue.

No spaces at all or spaces everywhere are extreme cases. We’ll try to avoid these.

do you plan to use static code analysis tools to enforce your coding standarts? maybe it worth to develop (or customize some existing) rules for e.g. PHP_CodeSniffer, use it for core development and share it to the community

Did you consider following PSR-2 coding style?

Yes, we’ve considered it and decided to go with our own. It’s not that different from PSR-2. Tabs instead of spaces mainly.

Use curly brackets even if there is one code line inside brackets




if ($a == $<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='B)' />

{

	$var=1; //only one line

}

else

{

	$var=2;

	//many lines of code here

}



This way decreases number of errors.

I use it continually and it is good and convinient practice.

According to me,when we put tabs,and than ,start code after tabs,than code is not readable.

If out line is big,for example findByAttributes() qurey,after 2 tabs,you have to scroll left-right to see the whole code,so I am not preferring tabs.But let yii core team decide,what to follow.Please make a good document for it,so anybody can easily follow the standards,& code easily.

You can configure your IDE for any width of the tab. At least in NetBeans, PhpStorm you can do so. Document is already there as well as codesniffer style:

https://github.com/yiisoft/yii2/wiki/Core-framework-code-style

Thanx samdark.