Skip to content

Instantly share code, notes, and snippets.

@patrickallaert
Created June 30, 2017 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickallaert/0f0f32135d47ae6459d0bb60327e6d18 to your computer and use it in GitHub Desktop.
Save patrickallaert/0f0f32135d47ae6459d0bb60327e6d18 to your computer and use it in GitHub Desktop.
Readability poll
<?php
// Performance appart, what is the most *READABLE* code to generate a simple error string?
$errorMessage = "Rule execution failed: " . $e->getMessage(); // Choice 1
$errorMessage = "Rule execution failed: {$e->getMessage()}"; // Choice 2
$errorMessage = sprintf('Rule execution failed: %s', $e->getMessage()); // Choice 3
@hollodotme
Copy link

hollodotme commented Jun 30, 2017

I think the actual context matters. Where will this message be displayed. How many variables. Is formatting needed.

I personally use sprintf() without the variable reusing feature, because this harms readability, is confusing and easily leads to misplaced values when changing the text. So I repeat arguments, if needed.

BTW: The PhpStorm plugin PHP Inspections (EA Extended) by @kalessil warns, if there is an argument count mismatch when using printf, sprintf, fprintf, sscanf, fscanf. So no direct relation of % to argument, but at least a helpful warning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment