0

if there is a way to split and make the code of every method in a separate file?

so I can have clean code and be simple and easier to maintain.

for now, I have a structure like this (minimal reproducible example):

// there is also a parentClass
// and I want to make sure that the method be inside parent
// so I can modify all the childs
// with the same method

class Parent {
  // myMethod.js
  myMethod() {
    // my long code
  }

  //secondMethod.js
  secondMethod() {
    // my long code
  }

  // thirdMethod.js
  thirdMethod() {
    // my long code
  }
}
class Child extends Parent {
  constructor() {
    this.someData = "someData";
  }
}

// this need to work after putting the file externally for example
new Child.myMethod();

but I want that the methods be in separate files

something like this: enter image description here

the problem is how can a method know that is been part of a specific Class and not another Class for example.


for example If we want to separate the Child from parentClass, this is easy:

  • thanks to import, export, extends

import ParentClass from './ParentClass.js';

// here since we have "extends" the child class know that is been part of Parent.
export default class Child extends ParentClass {
  // my child things and it will work
}

// but methods don't have extends or similar thing? 
// and if yes how we can export them? 
// export as a function? 
// (but they aren't function, they are methods?)

so what do I want?

  1. method -> file
  2. file -> method can be exported
  3. method -> parentClass can import it
  4. import -> method be part of parentClass
  5. method ParentClass -> get extended to all childs
  6. method extended -> read (and change) correctly the values of this. (like it was before inside with code splitting)
stackdeveloper
  • 352
  • 2
  • 21
  • you can add the methods using `ParentClass.prototype.myMethod = function() {.....}` – Jaromanda X Aug 13 '22 at 10:55
  • 1
    @JaromandaX is there a way with classes without Prototype? thanks – stackdeveloper Aug 13 '22 at 10:56
  • 1
    _"...to not repeat the same code."_ - Why? How? If you have a function that should work for multiple classes then make it a function that accepts those classes as input... – Andreas Aug 13 '22 at 10:58
  • @Andreas so a method() is actually a function? so if I do export function it will work fine? can I have a minimal example please (I will upvote and accept the answer promise) and sorry I am a begginer – stackdeveloper Aug 13 '22 at 11:00
  • 1
    @stackdeveloper - no, there is no way to split a single classes code into multiple files – Jaromanda X Aug 13 '22 at 11:03
  • 4
    _"simple and easier to maintain"_: imo what you're trying to do is the opposite of this. – Andy Aug 13 '22 at 11:07
  • @Andy but if I have a lot of methods with a lot of code inside them, I want that every functionality that is work get modified only if needed. so for example I need `methodOne` I just call it, and the code is imported from another file. so I can have more functionalities without having 100+ lines in one single file. (In a concept like "react", every component does one thing, and then we can reuse it everywhere by only importing it, and we can have the main app that gets 3 or 4 methods that every one has his own lines). but anyways sorry for the question, maybe this idea isn't good as I think – stackdeveloper Aug 13 '22 at 11:11
  • @JaromandaX yes, you can't https://stackoverflow.com/a/13258710/19485937 I see it. I tried to do the function export but can't access `this` (and it breaks the program) I think is not easier and don't make the program easier to maintain as I thinked – stackdeveloper Aug 13 '22 at 11:35
  • 1
    @stackdeveloper "yes you can't"? I know, I said you can't ... not sure why PHP is relevant though - do you understand JS is not PHP? – Jaromanda X Aug 13 '22 at 11:39
  • @JaromandaX I know that, but there isn't any question about this thing on StackOverflow in JS. I want only to get the idea, the perspective of how it works OOP (then I can make my own solution using import or something like that)... technically once you get the pseudocode example from another language, you can try to replicate it with the javascript syntax and making that work. yes, bro, I know the difference :) thanks however! and sorry for my stupid question (maybe I will add a bounty in the future, so maybe someone with experience can answer [who knows, maybe someone know how :) ] ) – stackdeveloper Aug 13 '22 at 13:59
  • @JaromandaX sorry for the typo, was "yes I can't" or "yes we can't do that". sorry for my stupid question and maybe impossible thing in javascript (oop) – stackdeveloper Aug 13 '22 at 14:07
  • A `class` could still `import` a function from another file: `import { myfunction } from "./function.js"`. Although you can't add it directly as a new method to your class, you can call it from any class that imports that function. But please be aware of reinventing the wheel. Trying to create a whole new coding style where you invent your own inheritance and composition just doesn't make very readable code. – Kokodoko Aug 13 '22 at 18:11
  • You can have a single class' methods spread over multiple files, because js classes are syntactic sugar over the the prototype model. Though I do not recommend using it. See here https://stackoverflow.com/questions/1635116/javascript-class-method-vs-class-prototype-method – wasserholz Sep 04 '22 at 19:10

1 Answers1

2

Some languages support partial classes, which is similar to what you are requesting. They don't get used a great deal as they make it hard to understand a class when it's internals are scattered around your project.

A more OOP way to solve your problem is to look at the parts you label // my long code.

You can usually break up that code and place it in other classes that you delegate the request to. For example, if your long code does many things:

myMethod(input) {
    // Map the input into a different shape
    // Part of the long code
    // Perform some form of calculation on some values
    // Another part of the long code
    // Create a response object and return it
    // This part probably isn't too long
    return response;
}

You might be able to split these up by creating a class that does mapping and a class that does the calculation. This means the mapping and calculation are easily used from other places and reduces the length of code in your method:

myMethod(input) {
    const mapped = this.specificMapper.map(input);
    const response = this.specificCalculator.calculate(mapped);
    return response;
}
Fenton
  • 241,084
  • 71
  • 387
  • 401