I would suggest it depends on the use case. Personally, in javascript, I have created a few classes that are a few hundred lines long, in some cases or two methods that are a couple hundred lines long and my reasoning for doing this is to improve performance when the code is running. Obviously, because of this I have commented well and used useful naming conventions.

If you're thinking of creating a class/set of methods that aren't going to be reused somewhere else then I wouldn't worry about splitting it. Splitting something for the sake of it can be counter intuitive as it can introduce unnecessary obfuscation, which slows down the code and also can make it harder to follow when debugging.

As for variables, I would suggest putting as much as possible in the constructor, some might disagree with this (obviously this has limits and should be taken on a variable by variable basis). My reasoning is that, in my opinion, it's cleaner. Especially if the variables are used in multiple methods. Furthermore I find that doing this allows easier tracking of the variables, meaning an improved consistency when in use as well as checking for debugging.

So that's what I would probably do given the limited information provided; create a class where you can setup the variables in a constructor then put the function in a single method and determine if it should be split based on complexity, future use cases and who's going to also be looking at this code.

Reply to this…

(4 answers) Take me to the question