PBP: 040 Multiline Strings

The PBP suggests breaking your strings on any embedded newlines so they look in code something like they’ll look when output.

It’s not a bad idea, and I generally support it.  I don’t find it as useful as it could be, because things tend to be full of long variable names or object calls that make the string in the program wider than the string that will be displayed.  The revers is sometimes true, too; the variable $address might contain, “1600 Pennsylvania Avenue NW, Washington, DC 20500”.

Their trivial example is helped a lot:


$usage = "usage: $0 <file> [-full]\n"

. "(Use the -full option for full dump)\n"

;

If your code has more work to do, things are less clearly beneficial:


$greeting = "Hello "

. $user->full_name

. " and welcome to "

. $site->name

. "\n"

. "Today is $full_date, and the system has been up for $uptime, "

. "with $highest_caller callers having connected $total_connections times for $average_call_length each.\n"

;

The next practice helps with this some.  Templates and getting the verbiage out of your program help even more.

Leave a Reply