crwlrsoft / crawler

Library for Rapid (Web) Crawler and Scraper Development
https://www.crwlr.software/packages/crawler
MIT License
325 stars 11 forks source link

Improve message composition #114

Closed szepeviktor closed 1 year ago

szepeviktor commented 1 year ago

Use sprintf instead of complex concatenations.

otsch commented 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.

szepeviktor commented 1 year ago

still feels better

When I meet feelings 🐰 I bravely run away

🏃🏻‍♂️

otsch commented 1 year ago

@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.

szepeviktor commented 1 year ago

@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!!!

szepeviktor commented 1 year ago

ps. In the past decade, I have learned to like measurement results. Some of them are still generating bad feelings!

szepeviktor commented 1 year ago

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.

otsch commented 1 year ago

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}";
szepeviktor commented 1 year ago

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.

  1. It 100% separates string literals and variable values.
  2. It can handle many types not just strings
  3. QA tools can check number of placeholders, type of placeholders vs. type of variables
  4. You can put every variable on a separate line: 1 line 1 variable

Side effects

ps. PHP was born as a template engine, and since 7.0 it wants to be a Java-like programming language.

szepeviktor commented 1 year ago

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.

otsch commented 1 year ago

Sorry, but I think there's a lot of preference and believes in your argumentation:

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.