0

Given the task of printing to the terminal, which code block is "best practice"?

    string name = get_string("Whats your name? ");
    printf("Hello, %s\n", name);

or

    printf("Hello, %s\n", get_string("Whats your name? "));

This question has come up two times for me in interviews and seemed to be a sticking point of some kind.

Both times the ladder was held up as the "proper" way but no real explanation why besides taking up less space. Is it due to not taking up a namespace and/or passing functions being more efficient?

  • 3
    The second example isn't passing a function, it is just calling a function; the result of that call is passed to `printf`. – ad absurdum May 16 '23 at 18:41
  • @AdultStoreVHS It is just a silly question of unqualified programmers. At first you should write code that is more readable. – Vlad from Moscow May 16 '23 at 18:46
  • 1
    The problem isn't efficiency. It's that the caller in #2 can't `free` the memory allocated, because the pointer was not retained. However, if you are doing nothing else with `name` the compiler might optimise the first one to be identical code. – Weather Vane May 16 '23 at 18:51
  • You should tag as CS50 as both "string" and "get_string" here are CS50 things and not standard. – Avi Berger May 16 '23 at 19:05

1 Answers1

1

Stylistically the first is somewhat better readable, but the second evades a variable which might or might not be used later. Of course one needs to include string. Also note that the second version places the input prompt after the output, not the sequential order.

Generated code: a string object is held in the first version till the end of its scope. In the second a const char* is expected of the get_string result which might be optimized immediately without data flow analysis; inlining without string object.

Nevertheless my answer is:

  • this seems interactive code, so a possible optimisation is irrelant;
  • even a C reference I saw used the first style - for clarity evidently;
  • only when the code contains many variables or has really many lines, one should use the second style.
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • *only when the code contains many variables or has really many lines, one should use the second style.* I'd strongly disagree with that - long, complex code with many variables is not going to be made more readable by cramming the same functionality into fewer lines. If the construct is harder to read and understand when it's in the context of three lines of code, putting that same construct into the context of 300 or 3,000 lines of code just makes it worse. – Andrew Henle May 16 '23 at 21:07
  • @AndrewHenle as detection of variable usages also requires effort, I still find it one means to simplify (admittedly bad) code. At least I totally agree that variables are better. A variable also names some calculated value. However the OP wondered why twice the second solution was preferred. Thanks for supporting the variable version. – Joop Eggen May 16 '23 at 22:40