Category Archives: PSR-2

Yii 2 PSR Coding Standards

I’d like to take a moment and welcome everyone who is finding their way to this blog. We have readers from all over the world. A lot of people have voted in the polls and the most active one is on my Yii 2 vs. Laravel post. Over 700 people have voted in that poll, so thank you to everyone who has participated.

Recently I read the book Clean Code by Robert Martin and it got me thinking about coding style and standards. I saw on the news release for Yii 2 that it follows the PSR standards, however I found some instances in using Yii 2, where it’s not following the standard.

For example, the naming of a private variable in the LoginForm model:

class LoginForm extends Model

public $username;
public $password;
public $rememberMe = true;

private $_user = false;

Accodring to the PSR-2 standard:

Property names SHOULD NOT be prefixed with a single underscore to indicate protected or private visibility.

Personally, I like the way Yii 2 is doing it there, it seems very intuitive to me and I couldn’t understand why the PSR standard would be against it. So I posted a question about in the Yii 2 forum and got an excellent response from forum member DocSolver. This is what he said:

“An argument in favour of the PSR-2 standard: if you change the status of a property from protected to public, you have to refactor all references to it.

The underscore in the name reminds me of to the old programmer’s habit of prefixing a variable’s type in its name (e.g. “floatAmount” and “strCustomerName”), which was popular in older languages when IDE’s were nonexistent. But now these habits are considered a bad practice by authorities such as Robert C Martin (see book “Clean Code”).

Because of this, I would say that your IDE should notify you about a variable being protected, instead of a naming convention that will probably be violated by many people. So I would vote in favour of conforming the Yii2 codebase to the PSR-2 way.”

DocSolver made some excellent points. But one thing to keep in mind is that the Clean Code book covers Java, which is a strongly typed language anyway, meaning that the variable type is explicitly stated with descriptors like string and int. So java gives you more hints going in.

Personally, I like the underscore because it pops out at you like a speed-bump. For example, in the code snippet above, it helps me understand that $_user is not from the form input, it really jumps out at you as different.

Anyway, this might be microscopic overkill on style, but I do feel standards are important and should be respected. One of the main points in the Martin book is that programmers spend much more time reading code than they actually spend coding. It’s so true. And that means things like this become important because it’s likely someone will have to read your code and the easier it is for them to understand it, the better.

I wrestled with this issue a bit because I would be much more comfortable using the underscore for private variables, just seems to have stuck in my head as a good way to go, but I’m going to sacrifice that peace of mind I get doing it that way to stick to the standard. I think following the standard is actually more important, at least in this case. Plus DocSolver made a pretty convincing case of why following the standard is actually better.

Internally, we can resist change, but that is just a form of inertia. I remember friends being resistant to the new array notation because they were so used to the old style, which to me looks like dinosaur code. I don’t want to be in the dinosaur camp.

I’m not a fan of mindlessly following what we think others expect from us. I think the controversy on TDD is an example of that. On the other hand, when programmers come together to set a standard, it’s worth taking it seriously.

In terms of Yii 2 projects, I will not be refactoring the framework code just for the sake of eliminating an underscore, that would silly. Plus, I would probably just mess it up anyway. I’ll just be mindful for my future code, when creating a private property, to keep it clean of the identifying mark. Let us know what you think. Take the poll and let us know if you follow the PSR-2 Standard.