0

I found many answers about using a unique return for each function, but my question is quite tricky for me.

I really need to know if there is some reason to return a variable with the expected result or if i can just return the value, as shown below.

function bar(foo: string): string {
  return foo.toUpperCase().trim()
}

function bar(foo: string): string {
  let result = foo.toUpperCase().trim()

  return result
}

In this specific case, which one works better? If there is no difference in performance, should i use the first or the second approach for readability and a "cleaner" code

Someone has downvoted it because its opinion based, i don't want opinions, i want to know if there is a concrete reason like performance, readability(convetions), or anything else that justify choosing between each other

Mateus Martins
  • 442
  • 3
  • 16
  • 2
    Completely ignore the performance - it's irrelevant here. Pick whichever feels better. I personally prefer the second option, since it allows me to very easily set a breakpoint on the `return` and examine the value that is going to be returned. However, if you have many similar functions, it's fine to have them so they take up less space. – VLAZ Jul 16 '20 at 14:19
  • 1
    I am unfamiliar with typescript, its ideology and its "behind the hoods", but generally speaking, for such minor optimizations, compilers usually do a better job than you'd expect, and let them keep at it - focus on what's more readable and easier to maintain. – amit Jul 16 '20 at 14:19
  • @amit i know it looks a little over engineering, totally agree with u – Mateus Martins Jul 16 '20 at 14:45

0 Answers0