It's time to ditch Medium for good! 🌈⚡️

Introducing Devblog by Hashnode. Blog on your domain for FREE. Highly customizable and optimized for developers.

Learn more

RE:

As always, depends. If it's a hot path, keeping the function or method together might yield performance benefits. Other than that, I'd split it into logical groups. That way, the code is even self-documenting (but you should add comments nevertheless!)

if the function is a method and you're splitting it, do you just share data through instance fields, or pass everything as arguments?

It's just one method. Keep your method's business out of the class/object. Especially when going parallel, having one field for all threads is a bottleneck. It's usually better to take a more functional approach (even in OOP) by using helper functions. Hell, I'd not even write the parts into methods, I'd write them into functions, which are encapsulated inside of the module (which I already do in my JS and Rust projects, btw).

import foo from 'Foo';

export default class Bar {
  doSth() {
    // very long method here
  }

  // ...
};

would become

import foo from 'Foo';

export default class Bar {
  doSth() {
    feedThePigeons(123, 'bread');
    spankTheMonkey(987);
    lickTheLizard(17);
  }

  // ...
};

const feedThePigeons = function(pigeonId, food) {
  // small function
};

const spankTheMonkey = function(monkeyId) {
  // small function
};

const lickTheLizard = function(lizardId) {
  // small function
};

Reply to this…

(4 answers) Take me to the question