Premature Optimization != !Deoptimization

Date: 2013-02-21 04:47 pm (UTC)
I agree that premature optimization is bad, but there is absolutely no reason for the creation of the hlf variable. hlf.x is just a reference to spt.cx. Why not just replace all instances of hlf.x with spt.cx and *not* create the additional variable?

Creating that additional variable can also introduce unneeded code later on that can be reduced if you see the obvious part, e.g. -
function incoming(a) {
  var b = {
    m: a.x,
    n: a.y
  };
  /*
    Lots of other code that doesn't operate on a or b
   */

  return b.m - a.x;
}


This is obviously a contrived example, but it illustrates my point. This function will always return 0, but if you don't see the declaration and return on the same screen, it's not immediately obvious.

I (and I believe Elf as well), have no problem with a declaration like so:
var b = {
  x: (a.x + a.y) / 2,
  y: (a.x - a.y) / 2
}

Because that actually has a transformation in it, but that is not the case in the example Elf posted.

-Michael
This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

Profile

elfs: (Default)
Elf Sternberg

May 2025

S M T W T F S
    123
45678910
111213141516 17
18192021222324
25262728293031

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 20th, 2025 06:15 am
Powered by Dreamwidth Studios