7

Which is the better practice and why?

Declare a variable:

exampleFunction(requestData: Object) {
  const username = requestData.username;

  doSomething(username);
}

Or access the object property directly?

exampleFunction(requestData: Object) {
  doSomething(requestData.username);
}

What if the code I use is over 50 lines long and the variable is used multiple times. Should I use the variable 'username' multiple times or use 'requestData.username' multiple times?

Jim L
  • 351
  • 2
  • 13
  • If you are asking for an opinion: It depends, and it is a preference. – Maihan Nijat Jan 08 '19 at 13:17
  • I'm wondering which one of these options is the better practice. – Jim L Jan 08 '19 at 13:18
  • 2
    In above context, access object property is better, I think. There is no need to declare a variable to hold it. And if you are using a better IDE like, it will suggest to use second, in the above context. – Maihan Nijat Jan 08 '19 at 13:20
  • I'm not sure if there is a best practice, but for sure `requestData` is too broad. What does requestData contains ? – Florian Jan 08 '19 at 13:21
  • I would go for the second option. Its much cleaner this way. – Kevin van Schaijk Jan 08 '19 at 13:22
  • As per my point of view, in the first case, you need an extra variable which is not actually required as you don't process it further in the same block. So, I recommend the second one in your scenario. – Tushar Walzade Jan 08 '19 at 13:28
  • What if the actual code is over 60 lines long and the variable is used multiple times in different places? – Jim L Jan 08 '19 at 13:30
  • then the first case is recommended so that you'll find it easy to process it with less/safe typing – Tushar Walzade Jan 08 '19 at 13:31
  • Depends on how costly it is to access the property (which is usually more costly than a variable access indeed, but with the optimisations in modern compilers, not by much). – Bergi Jan 08 '19 at 14:33
  • I see my question has been put on hold for being primarily opinion-based. If this is the case I guess I can conclude there is no better practice between the two. – Jim L Jan 08 '19 at 14:36

5 Answers5

5

V8 Javascript Engine performs some optimization pre-processing the code and checking if some variable can be deleted or if there's some regular pattern in the code, in order to reduce the execution payload.

For this reason, there are no memory issues in using a form or another, at least using V8 engine (for example, Chrome or NodeJS).

Even if this optimization isn't performed, it's just a pointer or a primitive variable, so it won't cost more than few bytes.

It's different if we talk about the cost of transmitting code. We know that JavaScript isn't a compiled language and it needs to reach the client's computer to be executed, so every space, indentation, semicolon, will cost in order of time used to transfer.

To reduce this cost, you can uglify and minify your code, but usually this type of optimization isn't performed.

Finally, it's impossible to think a scenario where it can make a difference, so I advise you to use the form that you think it's more readable and no matter of other parameters.

5

By refactoring of Martin Fowler is a technique (Replace Temp with Query).

after that you will go to second variant

cloneFullProject(requestData: Object) {
  doSomething(requestData.username);
}

But it is smell too (Middle Man).

As a conclusion in this example, it is better to remove this method altogether :)

However, there are cases when is preferable first example. For instance: you have several methods that take requestData.username parameters.

exampleFunction(requestData: Object) {
  const username = requestData.username;

  doSomething1(username);
  doSomething2(username);
  ...
}

And in this case temporary variable is justified

3

First option pros:

  1. Readable code
  2. You can debug and validate value before passing to another method

First option cons:

  1. It will create unnecessary variables and consume more memory
Ans Bilal
  • 987
  • 1
  • 10
  • 28
2

First of all you are referencing an object, not cloning it. The right way of cloning is throug Object.assign() or lodash (or similar) _.cloneDeep(), or spread operator or whatever.

Please refer to typescript - cloning object for more info about cloning.

Coming to the question, when you have a single variable to work both has the same readability in a such short method.

Just because the method is so short I would prefer the latter one, as reading a single line of code explain what property of the object I'm passing to my method instead of reverse-reading the code.

For my own taste and general opinion destructuring is the cleanest way another programmer would expect from your code:

const { username, var1, var2 }  = requestData;
Dion Williams
  • 165
  • 1
  • 7
1

Primarily, it is an opinion based question. So, the answer given below is my point of view.

  • In the first case, you need an extra variable which is not actually required as you don't process it further in the same block. So, I recommend the second one with direct property access in your scenario & is more readable too.
  • If you have a scenario where your actual code is over 60 lines long (or so) and the variable is used multiple times in different places, you can use the first case with an extra variable, so that you'll find it easy to process it with less/ safe typing.
Tushar Walzade
  • 3,737
  • 4
  • 33
  • 56