Skip to main content

The answer depends, for me, on many things. Some are:

  • is it a prototype or not? Looks like it is
  • are you in a rush? If so, do whatever works for you IF it's a prototype.
  • how big will the whole project be?

Here's a list of things that bother me:

  1. Function name: "from"from? Might make sense to you but it's not a very good name. It's too...generic. I'd take swissArmyKnifeFromElementswissArmyKnifeFromElement over fromfrom any day - easy to remember, hard to confuse. If this method is somehow part of an object (like your TreeTree), themthen it might be OK for a private function.

  2. The arguments might mean something else too - startstart and endend suggest a range. It's potentially misleading. endend can be many things (two, so far): a rule, like 'node''node' or 'child_list''child_list' or a class name. Again, misleading - it will only bring pain.

    function findTreeElements(root, rule, params) { //implementation }

    //you could call it like this findTreeElements(a,'class',{'name':'someClassName'}); findTreeElements(a,'headline');

     function findTreeElements(root, rule, params)
     {
       //implementation
     }
    
     //you could call it like this
     findTreeElements(a,'class',{'name':'someClassName'});
     findTreeElements(a,'headline');
    
  3. The more your project grows, the more complex this function will become - in ways that you cannot foresee :) Maybe that's why your friend was talking about objects and multiple functions. jQuery uses objects everywhere and I think that you should do that too:

  • define a tree/list object
  • define a task/node/whatever object
  • have the tree keep a list of tasks/nodes/etc
  • define the methods for manipulating the tree/node/whatever inside the associated objects

something like (totally random, possibly related, example):

    var list = tree.findNode(someNodeNameOrIndex).tasks();

It's up to TreeTree object to manage it'sits list of nodes and implement the findNodefindNode method (that parses the DOM whatever way you like).

It's up to the NodeNode object (returned by Tree's findNode) to manage the list of tasks inside that node (or something similar) and implement the tasks function which would work in a similar way to your current find(elem, 'child_list')find(elem, 'child_list').

Look at the code written by 'better' javascript developers and learn from them. There are many good projects out there that can teach you a lot. Look for decent sized projects, not 'plugins' or basic snippets; browser extensions are also good candidates.

The answer depends, for me, on many things. Some are:

  • is it a prototype or not? Looks like it is
  • are you in a rush? If so, do whatever works for you IF it's a prototype.
  • how big will the whole project be?

Here's a list of things that bother me:

  1. Function name: "from"? Might make sense to you but it's not a very good name. It's too...generic. I'd take swissArmyKnifeFromElement over from any day - easy to remember, hard to confuse. If this method is somehow part of an object (like your Tree), them it might be OK for a private function.

  2. The arguments might mean something else too - start and end suggest a range. It's potentially misleading. end can be many things (two, so far): a rule, like 'node' or 'child_list' or a class name. Again, misleading - it will only bring pain.

    function findTreeElements(root, rule, params) { //implementation }

    //you could call it like this findTreeElements(a,'class',{'name':'someClassName'}); findTreeElements(a,'headline');

  3. The more your project grows, the more complex this function will become - in ways that you cannot foresee :) Maybe that's why your friend was talking about objects and multiple functions. jQuery uses objects everywhere and I think that you should do that too:

  • define a tree/list object
  • define a task/node/whatever object
  • have the tree keep a list of tasks/nodes/etc
  • define the methods for manipulating the tree/node/whatever inside the associated objects

something like (totally random, possibly related, example):

var list = tree.findNode(someNodeNameOrIndex).tasks();

It's up to Tree object to manage it's list of nodes and implement the findNode method (that parses the DOM whatever way you like).

It's up to the Node object (returned by Tree's findNode) to manage the list of tasks inside that node (or something similar) and implement the tasks function which would work in a similar way to your current find(elem, 'child_list').

Look at the code written by 'better' javascript developers and learn from them. There are many good projects out there that can teach you a lot. Look for decent sized projects, not 'plugins' or basic snippets; browser extensions are also good candidates.

The answer depends, for me, on many things. Some are:

  • is it a prototype or not? Looks like it is
  • are you in a rush? If so, do whatever works for you IF it's a prototype.
  • how big will the whole project be?

Here's a list of things that bother me:

  1. Function name: from? Might make sense to you but it's not a very good name. It's too...generic. I'd take swissArmyKnifeFromElement over from any day - easy to remember, hard to confuse. If this method is somehow part of an object (like your Tree), then it might be OK for a private function.

  2. The arguments might mean something else too - start and end suggest a range. It's potentially misleading. end can be many things (two, so far): a rule, like 'node' or 'child_list' or a class name. Again, misleading - it will only bring pain.

     function findTreeElements(root, rule, params)
     {
       //implementation
     }
    
     //you could call it like this
     findTreeElements(a,'class',{'name':'someClassName'});
     findTreeElements(a,'headline');
    
  3. The more your project grows, the more complex this function will become - in ways that you cannot foresee :) Maybe that's why your friend was talking about objects and multiple functions. jQuery uses objects everywhere and I think that you should do that too:

  • define a tree/list object
  • define a task/node/whatever object
  • have the tree keep a list of tasks/nodes/etc
  • define the methods for manipulating the tree/node/whatever inside the associated objects

something like (totally random, possibly related, example):

    var list = tree.findNode(someNodeNameOrIndex).tasks();

It's up to Tree object to manage its list of nodes and implement the findNode method (that parses the DOM whatever way you like).

It's up to the Node object (returned by Tree's findNode) to manage the list of tasks inside that node (or something similar) and implement the tasks function which would work in a similar way to your current find(elem, 'child_list').

Look at the code written by 'better' javascript developers and learn from them. There are many good projects out there that can teach you a lot. Look for decent sized projects, not 'plugins' or basic snippets; browser extensions are also good candidates.

Source Link
bkdc
  • 226
  • 1
  • 4

The answer depends, for me, on many things. Some are:

  • is it a prototype or not? Looks like it is
  • are you in a rush? If so, do whatever works for you IF it's a prototype.
  • how big will the whole project be?

Here's a list of things that bother me:

  1. Function name: "from"? Might make sense to you but it's not a very good name. It's too...generic. I'd take swissArmyKnifeFromElement over from any day - easy to remember, hard to confuse. If this method is somehow part of an object (like your Tree), them it might be OK for a private function.

  2. The arguments might mean something else too - start and end suggest a range. It's potentially misleading. end can be many things (two, so far): a rule, like 'node' or 'child_list' or a class name. Again, misleading - it will only bring pain.

    function findTreeElements(root, rule, params) { //implementation }

    //you could call it like this findTreeElements(a,'class',{'name':'someClassName'}); findTreeElements(a,'headline');

  3. The more your project grows, the more complex this function will become - in ways that you cannot foresee :) Maybe that's why your friend was talking about objects and multiple functions. jQuery uses objects everywhere and I think that you should do that too:

  • define a tree/list object
  • define a task/node/whatever object
  • have the tree keep a list of tasks/nodes/etc
  • define the methods for manipulating the tree/node/whatever inside the associated objects

something like (totally random, possibly related, example):

var list = tree.findNode(someNodeNameOrIndex).tasks();

It's up to Tree object to manage it's list of nodes and implement the findNode method (that parses the DOM whatever way you like).

It's up to the Node object (returned by Tree's findNode) to manage the list of tasks inside that node (or something similar) and implement the tasks function which would work in a similar way to your current find(elem, 'child_list').

Look at the code written by 'better' javascript developers and learn from them. There are many good projects out there that can teach you a lot. Look for decent sized projects, not 'plugins' or basic snippets; browser extensions are also good candidates.