Coding conventions, best practices, standards — all or nothing.

A few years ago I’d joined PHP project and this project didn’t follow PSR-2 standard, especially in variables names.
For example, we had pictures in project and we provided this pictures to our Android & IOS clients through REST-API, our JSON looked like:
{“picture_id”:1,”file_name”:”main.png”,”picture_type”:2}In PHP we had something like:
return new Response(json_encode([
‘picture_id’ => $model->getPictureID(),
‘file_name’ => $model->getFileName(),
‘type_id’ => $model->getTypeID(),
]));After discussion with team we decided to start follow PSR-2 standard.
At that moment project was more than 1 year old and had pretty big code base — so we decided not touch old code,
not do monkey job and not refactor code just for rename variables.
That’s why we decided apply this standard only to new code.
Applying this standard was very easy and very pleasant, because we applied this standard to all levels (database, PHP, JSON response for clients).
For PHP developers life become easier because field in DB has name pictureId, property in model has name $pictureId, JSON response has key pictureId — perfectly, isn’t?
Moreover, PHP become look like:
return new Response(json_encode($model->toArray()));Awesome — less code, fewer bugs, faster development and maintenance of code!
We’d been lived in this happiness almost year and created huge code base which is followed to PSR-2 standard.
And after some time business team decided to implement video feature.
For PHP team it was super simple — just create in DB table with fields:
videoId, fileName and typeId and write `json_encode($model->toArray())` with purpose obtain next result:
{“videoId”:2,”fileName”:”top.mp4",”typeId”:3}But for Android & IOS clients it became nightmare
because REST-API lost predictability and consistency.
No one was able to answer the question “what key is correct type_id or typeId”.
No one understood why they differ, why they had different code style.
And this problem spawned lot of bugs, lot of discussions and lot of disappointments…
As result PHP team wasn’t happy to check code base every time when someone asked about a proper key for video.
As result, we end-up that our huge code base is inconsistent,
because some tables in DB have fields in camel case — some not,
some fields in models have properties with underscores — some not, etc…
After some time no one was happy with such awful implementation of PSR-2, even new PHP team members considered this as disadvantage…
PS:
* Don’t apply blindly standards and best practices.
* Apply standards and best practices properly
(move code base from one consistent state to another consistent state).
* Consider an option to follow style which is already applied on project
(even this style implicit or not standardized).
