A lot of programmers are passionate about what they feel is the “right” way to do things. A common topic of discussion is code standards, especially questions such as “Do brackets go on a separate line, or at the end of a statement”. It’s understandable, because I see pros and cons in both. On other topics, I have a hard time understanding both sides – such as whether to use tabs or spaces for indentation (I only ever use tabs). What we can all agree on however is what I started with: programmers are passionate about “the right way” to do things.
“String literals’
Heh, that was clever. See how I used both ” and ‘ in the heading there?
PHP is a funny language in a lot of ways. One of the things it does, along with some other languages such as Javascript, is allow you to specify strings using both double and single quotation marks (is that the right name for them?). In PHP, I generally just use what is most convenient at the time. There are differences between them (in PHP), which I’m sure most people are aware of already (double allows you to use variables directly in the string, using single you have to hop out and concatenate the variable with the . character. Some programmers apparently see this as an enormous amount of effort, and just use double quotes instead. Lazyness.
I generally find it easier to read code where variables are concatenated. If you want to rewrite the code to use values from an array instead of different variables, you either end up with the even uglier {$aArray['key']} style, or you end up rewriting the code to use concatenation anyway. Why not just do it “the right way” the first time you write the code?
The horror!
Even more horrible is using the single quotation marks for (X)HTML attributes. Most of the time people get this one right, but once in a while I stumble across pages that use the single quotation mark for HTML attributes – horrible stuff, even if it is “legal”. I was looking through the HTML for my blog today and fell off my chair when I saw this:
<ul class='xoxo blogroll'>
After peeking through the WordPress code, I discovered that this was a someone obviously does not share my hatred for using single quotation marks in HTML Image may be NSFW.
Clik here to view. .
Really though:
$output .= "$title_before$title_li$title_after\n\t
- \n";
// Could easily be written:
$output .= "$title_before$title_li$title_after\n\t
- \n";
// Or...
$output .= $title_before . $title_li . $title_after . "\n\t" . '
- ' . "\n";
Personally, I prefer the last one, as I find it much easier to read. Plenty more grep’ing later, I think I’ve translated most of the occurrences (at least on the front page).
What are other peoples views on string literals? Am I just being a complete code nazi here?