Coding standards??? #60
codemasher
announced in
README
Replies: 1 comment 2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
What coding standards do you follow? Why don't you adhere to PSR-1 or PSR-12?
First off: i don't enforce any coding standard. If you'd like to contribute code to this project (or any other projects of mine), you are welcome to do so - no matter what code style your contribution is written in. I can always run it through a formatter so that it aligns with the project's style.
That said, this doesn't mean that there aren't any rules at all - most importantly your code should be self-describing and/or be commented so that a reader understands what's happening. I'll lay out the general rules that i follow later.
Why don't you adhere to PSR-1 or PSR-12?
I do actually follow a lot of the proposals in these PSRs, however, i see several things in both of these as messy and inconsistent and opinionated (as all coding standards), making code harder to read and edit. I mean, just look at it. The working group had a chance to fix things and make it future proof but somehow blew it - not to mention PHP8 (attributes, constructor property promotion etc.) isn't even considered in the recent PSR-12.
One of the biggest gripes is for sure the choice of space indentation over tabs, which is not only silly now in 2021 when everyone and their grandma is using a smart IDE that allows to adjust tab sizes, but it also adds overhead and makes the code a LOT harder to edit (try copy pasting spaced vs tabbed code for example).
The next thing is the inconsistent bracing that makes code harder to read and adds a lot of unnecessary vertical space (in case of classes and methods), then again closing and opening braces on the same line in control structures (probably to make up for the wasted space elsewhere? We'll never know). I know, i know the latter might come in handy in case you want to outcomment something for whatever obscure reason, but i've never seen that case out in the wild, nor did i ever consider to use it on my own.
A thing that's completely missed (yet somehow expected) is properly naming things so it seems completely fine to name your class that does a very specific thing
MyCoolClass
that has a methoddoStuff()
as long as it follows the recommended naming scheme...So what are your rules then?
What follows are the general rules i (try to) adhere to and that are set in my global PhpStorm settings (which i may add to the repo at some point).
.
), object access (->
), unary (++
,--
,!
, etc.) and ternary in case the "if" is empty (?:)[]
)'
) for pure strings, double quotes ("
) in case code needs to run through the parser (e.g. using special characters"\r\n"
). Variable interpolation in double quotes is discouraged, use concatenation orsprintf()
instead.// comment
), PHPDoc blocks with a slash and double asterisk (/** doc block */
), single lines of code are commented with a caret (# foo();
), multiple lines of code are commented with a slash and single asterisk (/* code block */
).array
), a doc comment with a proper type should be added if possible (/** @var int[] */
).As you can see, a lot of it aligns with the PSRs, but i find it more intuitive that way.
Beta Was this translation helpful? Give feedback.
All reactions