-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update code style to match what's actually used #9
Comments
should also summarize the API documentation styles, especially about the usage of markdown tags (this is still a work-in-progress). |
This might be the wrong place for this - but why not the PHP-Fig Recommendation Standards? |
@navarr, it's already used with some minor changes. Btw, PSR-×'s are not the ultimate truth—it's called Proposing a Standards Recommendation after all. |
PHP-Fig sure as hell is not a magic bullet on the topic of code style standards. Personally I think there's a lot of things in it that is utter nonsense. It doesn't matter how much they consider this a standard, there will always be at least two camps on every piece of style in it. Do what makes sense, not what some lousy standards attempt states :) |
I consider PSR-2 is one of the best code style proposals. There is only one thing, which I dislike at PSR-2: it recommends to place open brackets ("{") for classes and the functions at the next line after thier definition. At my opinion it simply wastes an extra row. |
Yii2 can have it's own coding standard ... https://github.com/squizlabs/PHP_CodeSniffer/tree/master/CodeSniffer/Standards |
It has. Yii2 code style does not completely match PSR-2, while it is based on it. |
Extended style description https://github.com/yiisoft/yii2/wiki/Core-framework-code-style |
Question 1: Why we are using tabs instead of spaces for indention? There are 2 common approaches for the code block indention: using tabs or using 4 spaces. I prefer 4 spaces, because spaces are always spaces no matter which program do you use to browse code, while tabs appearance may vary. |
Modern editors (even vi/vim, just do :set tabstop=4) should be able to configure how many spaces a tab represents. This gives you the freedom of choosing your favorite indention size. If you use spaces, you don't have this freedom. Moreover, when you want to move the cursor in a line, moving through tabs takes less typings than spaces. Let's not bring up the war between tabs and spaces again. The more important thing is we should be consistent. And this coding style only applies to the Yii core code. |
Question #2: Why place open bracket for classes and functions at the new line? While studying at the academy, I have learned several program languages. Pascal, C++, Java were among them. All these languages use special constructions for the code blocks: “begin/end” at Pascal, “{}” at C++ and Java.
When I read code samples on C++, the open brackets were placed in different positions:
Why C++ place open bracket at the same line as the control structure begins? – Because it can. In Pascal placing “begin” keyword at the same line with the control structure will make code hard to read, because Pascal is too verbose:
In opposite placing open brackets at the new line in C++ will make its code to be stretched without purpose:
The first time I ever heard about coding standards was when I was studying Java. Java code standard dictates the open brackets for the code block always should be at the same line as it parent control structure:
For the long time I considered Java code style as best for all “C”-like program languages. I was surprised when I come across PSR-2 standard for PHP, which tells open brackets should be placed at the same like for operators like “if” or “for”, but at the next line for class or function definition. I have digged around this a bit and found out, why such unlogical rule has appeared. The “C” programming language is very old and it was changed a lot during its lifetime. When the first “C” code style has been created it was dictating the open brackets for code blocks should be placed at the same line with the control structures, no matter what this structure is. However at that time the function definition was rather different from the one we all used to:
It was necessary to place each function argument at the new line, at there was no option to place the open bracket at the same line with the function definition. That is why the first “C” code style tells for functions open brackets should be placed at the new line.
Using new line for the function brackets is just a legacy of the old “C” syntax and has no practical usage, it just throws a line of code away.
and
My Academy Java instructor always told: only amateur, who can not forget Pascal, place the open brackets at new line. |
We're very literally arguing semantics here. My reason for bringing up PSR-2 was because it is (a) recognized by IDEs (except perhaps Eclipse, unless you know what you're doing) and (b) Is already programmed into many code quality testing tools, and democratically accepted by the larger PHP community as a standard for the most optimum amount of interoperability. The intent was not to start this very large list of things you're keen on saying, @klimov-paul. And while this is definitely not the right place for this, it is a very big shame that Yii does not have a representative in the PHP-FIG. |
@klimov-paul I had the similar doubt about the inconsistency about the bracket location in the first style which is adopted by most PHP projects. Over the time, I convinced myself to take it for this reason: a class or a function is a like an article or a chapter which needs a title on a single line by its own. While statements within a function are like sentences which if having brackets on separate lines would appear too empty. |
I do not attempting to start a war about code style. I just want it to be most appropriate. Code style is important - the readability of the code depends on it. However the main thing is maintain the chosen code style and do not forget about it. |
@klimov-paul for the reason you've mentioned we can create / adapt config for something like phpcs and use it when merging commits into the core. |
I am arguing about code style not because of myself. |
I like the PSR-2 and one of the reasons is, like @navarr said, because it is (a) recognized by IDEs, which makes it easy to follow, just press the key command and you are ready. |
+1 to PSR-2. I love to see code of other developers that looks like mine!!! |
I'm closing this as the current Yii 2 style is very close to PSR-2 except about the indenting which we will stick to using Tab. |
Please let me write the closed issue, sorry. One more thing about 4-spaces, I think, is... |
@tanakahisateru that's not a big issue. It can be tweaked easily for API docs. |
Why you cannot just follow PSR-2? imho "very close" isn't enough... |
@tarlepp We use similar to PSR-2 standarts with some extensions to it.
What was that? ;) PHP-FIG is not official group. "Enough" is not correct word here, real. I'm sure we use not PSR-2 because we think out standart is better. Thats all. |
Neither is really better at this point :\ But PHP-FIG is a fantastic group and a fantastic mindset and I honestly don't understand why Yii Framework hasn't gotten behind it. Especially with talk like "we should use the Zend_Mail instead of creating our own" and such. The Framework Interop Group sounds like something Yii should be embracing. |
@navarr What are the similarities of standarts and
? |
https://github.com/yiisoft/yii2/wiki/Core-framework-code-style
The text was updated successfully, but these errors were encountered: