-1

TL;DR: what do I call a section of code that is used to assemble a Graph object?

I have a fairly large section of code that is duplicated in a few areas. I wish to extract it into its own class. What do I call this class is my question.

The section contains code like this:

    /*
     * First Graph
     */
    $flow_curve = new CurveEntity();
    $curves = array();
    $curves[] = $flow_curve;
    $curves[] = new CurveEntity();
    $curves[] = new CurveEntity();

    $graphs = array();
    $graphs[] = new GraphEntity('graph_1');

    /*
     * Second Graph
     */
    $flow_curve = new CurveEntity($x);
    $curves = array();
    $curves[] = $flow_curve;
    $curves[] = new CurveEntity($y);
    $curves[] = new CurveEntity($z);

    $graphs = array();
    $graphs[] = new GraphEntity('graph_2');

This is not a controller and not view. Is it part of the model? Is it a service? Is it a repository? Nothing seems to fit.

Thinking about what this code represents, it looks like assembly of the $graph object, where the object is later used in the view for graph plotting.

  • 6
    The code you show has the pattern called "creating and filling arrays". It's not doing anything else. – Mat Oct 30 '18 at 20:14
  • alright. I will create a class CreateGraph and refactor it later if need be. I am thinking it is a glorified value object of sorts – Dennis Oct 30 '18 at 20:33
  • 6
    a new pattern name is born... – Ewan Oct 30 '18 at 20:53
  • 2
    Is $curves used in $graphs at some point? Right now it's not obvious how they are related, if at all. – l0b0 Oct 30 '18 at 21:21
  • 6
    I think this pattern is called "a snippet of mundane code". Seriously, though... Usually a "pattern" is defined as something like "a general, reusable solution to a commonly occurring problem within a given context". What problem is your code trying to solve? In what context? What is your general solution? I don't see anything particularly distinctive in your code that would lend itself to being called a "pattern". – Eric King Oct 30 '18 at 22:56
4

If the variables $flow_curve, $curves, and $graphs belong to a bigger "something", give that "something" a name and create a class from it (maybe it is the Graph class you mentioned? Or maybe some GraphCollection?). Find an abstraction which suits your needs best. Then the code could be naturally placed in the constructor of that class - and that is the name of the "pattern" you are looking for, if you insist to call it a pattern (personally, I would not).

If, however, the assembling procedure is quite complex, and those variables are just temporary helpers, taking the code out of the constructor into a separate class is the classic Builder design pattern, and the class where the code "lives" may be called GraphBuilder or GraphCollectionBuilder, or similar.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.