in reply to Re^2: Refactoring technique?
in thread Refactoring technique?

Now I got you. For my education (I have never used js), how do you parameterize x, y and z anyway. You have a lot of something.x and somethingelse.y. If you would want to write doStuff( u, v) how would you use u and v in the code?

Replies are listed 'Best First'.
Re^4: Refactoring technique?
by BrowserUk (Patriarch) on Apr 24, 2015 at 16:42 UTC
    how do you parameterize x, y and z anyway. You have a lot of something.x and somethingelse.y.

    I'm still getting to that, but it seems that in JS, any place you can write o.x you can also write o['x'], so, I'm hoping that by extension, instead of:

    function doStuff( x, y, z ) { ... oo.x.thing = p.y.max * qq.z.min; ... }

    I can also write:

    function doStuff( a, b, c ) { ... oo[a].thing = pp[b].max * qq[c].min; ... }

    I'm still cleaning up and refactoring other parts, so I haven't tried that yet; but from what I read, and a couple of small tests, I think it will work.

    If any JS guys are following along and can verify that (or not), I'd be grateful.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

      Lucky for you that want the oo[a] notation instead of oo.x notation. I am mostly positive that Javascript cannot interpolate a method call at runtime. Consider the following:

      #!/usr/bin/node var key = 'id'; var obj = { id: 123, key: "wrong key!" }; function do_meth( obj, key ) { return obj.key; } function do_windex( obj, key ) { return obj[key]; } console.log( do_meth( obj, key ) ); console.log( do_windex( obj, key ) );
      When run, do_meth returns "wrong key!" instead of 123. You have to hard code return obj.id ... or use bracket notation such as you desire.

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      

        That's good news, and thanks for the test script. This reflects the type of substitutions I need:

        #!/usr/bin/node var vec = { x: 1, y: 2, z:3 }; var obj = { id: vec, key: 'Wrong key!' }; function do_meth( obj ) { return obj.id.y; } function do_windex( obj, key ) { return obj[key].y; } var key = 'id'; console.log( do_meth( obj ) ); console.log( do_windex( obj, key ) );

        Where do_meth() is how it is, and do_windex() is how I need it to be. And, it works for my big example.

        At least mostly. There is still something that isn't quite right; some small difference I haven't tied down and parameterised properly.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
        In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

      I have been meditating over your original code. I cannot say what it does but the generalstructure looks like this: there are three sections, one for each plane. I would call that parameter $plane. In each of the three sections, there are three subsections: some prep stuff involving the two other directions in the same way and then two sections that you seem to want to refactor into doStuff. Each section needs both other directions but starts dealing with one of them first. For example, in tge z plane section it first deals with the x axis, then with the y axis. I would therefore call one direction $primary and the other $secondary. So in summary it would be doStuff( $plane, $primary, $secondary ).

        As you'll see in the third code block, I went with your suggestions, at least until something better falls out of my brain :)


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
        In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked