6

I am given a project where the previous developer used <? and <?= whereas I am using <?php and <?php echo.

My questions: - should I rewrite all files _ or just when i need to tweak the code - with new files/code should i use my pratice or copy the existing pratice

I am hoping this is the right subdivision of stackexchange to ask this general question.

closed as primarily opinion-based by gnat, Jules, Pieter B, Robert Harvey, 8bittree Oct 23 '17 at 18:08

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 15
    If you have an existing project, what is your rationale for introducing a new coding style? It seems like there are already many files that adhere to an existing standard. Even if you don't personally like it, why break consistency or go through the hassle of updating many files for a purely stylistic change? – Thomas Owens Oct 18 '17 at 19:53
  • Thanks guys (Thomas and Ryan) as a loner I just needed to pick your brain and you have valid points, consistency is king... – alex Oct 18 '17 at 20:04
  • Could you please edit the question to specify if the tags are being used in definition files or templates – James Fenwick Oct 18 '17 at 20:23
12

<?= is a shorthand for <?php echo $a; ?>. As far as the short syntax is supported by all the servers where the application is deployed, there are no technical reasons not to use it.

So it comes to personal preferences. Previous developer seemed to prefer shorter syntax. There is nothing wrong in that. You seem to prefer the explicit one; this has nothing wrong either.

What is wrong, however, is the inconsistency you're introducing in the code base. As with any style choice, it's much more important to be perfectly consistent than to chose the absolute right one. Therefore, don't mix both.

If you decide to change the style everywhere, well, you can. Unless you work for a company which pays you to deliver value; spending your time changing the style doesn't deliver value, and could potentially introduce bugs.

So what should you do? Either keep your code consistent with the existent code base, or discuss the change with the stakeholders, so they could decide if the benefit of the verbose syntax outweighs the cost of the change (and the cost of solving bugs possibly introduced by the change).

  • Torn here. It’s the absolutism in the answer. How do coding styles change or evolve if inconsistency is always negative? In this circumstance, if Alex has the authority and desire to change the rules, then tweaking as new files are touched is a good way to go. Make a style check part of check-ins. Value-delivered, code gets updated. ::shrug:: – Josh Bruce Oct 25 '17 at 20:09
4

Depends.

If you're the only developer working on the project, you can pick whatever style you'd like. Pick one, stick to it consistently and you're good. You could probably use grep (or something similar) along with a regular expression to automatically change all of occurances you don't want with the ones you do.

On the other hand, if other developers are working on the project or if they have in the past and wil do so in the future (which seems likely), it would be best just to keep the coding style in place that the project already has. You conform to the project style.

Whatever style is chosen isn't terribly important, what is important is that the style is consistent across the entire project.

Lastly, even if you CAN change the style, maybe it would be best not to. It could be good to get used to the idea that the projects you work on will not always conform to the style you prefer (they will rarely, if ever, conform exactly to your preferences). Think of it as a way to grow as a developer (albeit a small one).

4

If you can update the code, then update it! The reason for that is keeping the code up to standard. To quote PSR-1,

  • Files MUST use only <?php and <?= tags.

Fortunately, there is tools to do this, e.g. php-cs-fixer, all you need to do is run it at can apply all the accepted coding style standards, not just the one you mentioned.

Better yet, make it part of your development workflow because no one read guidelines, add commit hook to run the code style fixer, you will weeks of time spent on debating (if working with teams) about how the coding style should be.

1

Yes, clean them up to be consistent.

The PHP CodeSniffer will help you enforce and verify coding standards for your project: https://github.com/squizlabs/PHP_CodeSniffer

You can use it in conjunction with build tools like grunt or gulp, or as part of a git pre-commit hook to ensure new code follows this standard.

Not the answer you're looking for? Browse other questions tagged or ask your own question.