Closed szepeviktor closed 1 year ago
I know it's a bad preference of mine to do string concatenation like this. But actually for me it still feels better than sprintf
, because when reading, the inserted variables are right in the place where they are inserted and I don't have to count and see which argument will be inserted for the third %s.
$number = 123;
$string = 'asdf';
$message = 'Foo ' . $number . ' bar ' . $string;
$message = sprintf('Foo %s bar %s', $number, $string);
$message = "Foo {$number} bar {$string}";
I'll accept it if you go for the third option.
still feels better
When I meet feelings 🐰 I bravely run away
@szepeviktor OK, I used the word "feels", but actually I also argued why I prefer concatenation over sprintf
. It's not just a preference without any reason and if it was, still no reason to be rude.
@otsch I am sorry. When I meet personal things, preferences I really run away. Viktor is 100% data-driven even in things not to my liking.
printf
was invented for this purpose. Everyone has feelings and preferences.
I put them away when it comes to software making.
🤖 Robot Viktor!!!
ps. In the past decade, I have learned to like measurement results. Some of them are still generating bad feelings!
still no reason to be rude.
I am very sorry that you felt rudeness. I just wanted to let you know that I do not talk about personal things. I only mention facts, measurement results.
OK, so what could you measure to decide which option is better here? Isn't it mainly about readability?
What makes sprintf
the superior choice for you over the mentioned third option?
$number = 123; $string = 'asdf'; $message = 'Foo ' . $number . ' bar ' . $string; $message = sprintf('Foo %s bar %s', $number, $string); $message = "Foo {$number} bar {$string}";
Very good question. Thank you!
Every language has bad parts. For example when some parts are targeting developer experience, not the robustness of the code.
The concatenation operator is a bad choice. It mixes literal strings and values of variables. It is the typical "too much liberty".
The complex curly syntax (as PHP docs call it) is the worst. It mixes string literals and variable values deeper. There is barely any separation between them. And curly braces are highly error prone: just a single space and it stops working, no QA tool will notice that unless you unit-test all parts of it.
Then it comes to good old printf
.
Side effects
ps. PHP was born as a template engine, and since 7.0 it wants to be a Java-like programming language.
Finally something personal for you. Secret! viktor stuff.
$message =
'Foo '
. $number
. ' bar '
. $string;
This is to my liking ❤️ But you won't see me doing it.
Sorry, but I think there's a lot of preference and believes in your argumentation:
abc
, then sprintf('Foo %d', $var)
will result in Foo 0
. I think handling types is better done with type hints and return types.no QA tool will notice that unless you unit-test all parts of it.
=> in your comment you linked to a custom PHP CS fixer rule that you've built 😅 so, if you know that you sometimes make mistakes using the curly braces syntax, why not just make a CS fixer rule for that?And further you proofed yourself that you can put everything on a separate line also with string concatenation...which I think is just a matter of taste not an argument for printf superiority. And also you mentioned yourself that it takes a year or two, to read printf syntax like plaintext. I think I just did this with string concatenation, which basically is a pain for almost everyone, but I'm just used to it. But I admit, that the curly braces syntax definitely looks better.
Use sprintf instead of complex concatenations.