An “Accident” I Constantly See in PHP Scripts
It's quite normal to have multiple engineers working on an individual file or files with conflicting styles. Preferences come into play that do not affect the code at all. For example, I don't like the one-true-brace style:
My braces stay on the same line as the conditional. But I understand that it's just preference, and that's fine; the application behaves the same either way. If a file already uses this pattern for the most part, I will leave it alone and sometimes even code in that style to keep things cohesive. But there is a bad PHP habit that I've recently come to believe is a mistake, and will change when I see it:
To me, this is accidental. These lines are parsed for variables, and when that feature isn't being used, there's an unnecessary performance hit happening. Granted, it's a very nominal performance hit, but they can add up, and why suffer at all when there is no reason?
Don't get me wrong: double quotes have their place, and I use them (properly) very frequently, such as:
Style is one thing, and intention is another. Some people prefer double quotes over single quotes, sure, but I think most engineers don't intend to parse their strings twice every time, and so I often see using double quotes as a mistake.
Personally i tend to go for the one-true-brace style, as it makes it easier for me to determine where all the “scopes” are when there is a lot of nesting going on.
Still, i also make sure indent my code so one could say that point is doesn’t make much sense.
Thankfully though, i’ve actually been doing a lot of programming in Ruby recently. No brackets there to worry about. There’s still the single vs double quotes issue though. I always go for single quotes unless i need extra parsing.
For other projects, i try to stick as close as possible to the original formatting as i can.
It seems logical to me that the double quote should be the bigger encloser and should always be used.
First, several languages don’t like ‘multiple characters’ and requires “multiple characters”
So by being using double quotes in languages that are flexible, you can remain being consistent.
Second, the probability of having a single quote in a string is much more likely then having a double quote.
Thus this is easier and cleaner “book’s” than doing ‘book\’s’
Third, the double quote just seems like it should be bigger and thus enclose single quotes rather than the other way around.
As far as PHP’s parser needing more time to parse double quoted strings over single quoted ones? Well, that’s PHP’s fault and not something that any SANE engineer should ever concern themselves with :)
I agree on using double quotes when apostrophes are present, but things being a language’s “fault” are exactly the sort of situations that engineers need to be prepared for. Take JavaScript for example: it’s one of the messiest languages around, but is quite elegant when used properly. PHP has similar traits.