Using one function to go from one element to another After I made a change to the html structure for a project, I found myself having to change many jquery selectors in many places.  To avoid this problem in the future I decided to make a simple function where I pass an element indicating where I am and a string indicating where I want to go.  This function should apply whatever logic is necessary to access the given endpoint.  I showed this code to someone and he didn't like it, he said I should use multiple functions or an object.  I thought that would make the code too much longer for little to no benefit, but I'm not really sure.  The code below is for this tool, which is a tree-based time-tracking app, so for example, "node" refers to a node in the tree.  The logic is not exhaustive as I only add code to the function on an as-needed basis.

// Access one particular element from another.
function from(start, end) {
  start = $(start)
  if(start.parent().hasClass('headline')) {
    var node = start.parent().parent()
    if(end == 'node')  return node
    else if(end == 'model')  return get_model(node.attr('model_id'))
  }
  if(end == 'headline')  return start.children('.headline')
  if(end == 'child_list')  return start.children('ol')
  if(end == 'each_child')  return start.find('&gt; ol &gt; .model')
  if(end == 'parent')  return start.parents('.model')
  if(end.indexOf('.') != 0)  end = '.' + end
  if(start.hasClass('headline'))  return $(start.children(end))
  return start.find('&gt; .headline &gt; ' + end)
}
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:


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.
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');

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.