-3

A common pattern I've been using for JS/NodeJS applications is the following:

import { utility1, utility2 }

function exampleScript() {
utility1()
utility2()
// Does something
}

Basically my main application function would rely on smaller helper functions that I could separate and modularize. But I'm not sure what style you would consider this as (e.g. procedural)? I've been told that I should follow Object Orientated design patterns. Is this considered bad practice?

  • 5
    "Bad practices" are those which produce an undesirable result. Does this practice produce an undesirable result? – Robert Harvey Sep 20 '18 at 16:08
2

Not necessarily. Decomposing program functionality into smaller, reusable parts with limited responsibilities is good. My concern here would be whether utility1 and utility2 are clear on what they are doing (if it's too hard to name them, then they probably aren't). As a concrete example, think of setUpPlayfield and setUpScoringSystem: what there two do seems clear enough.

I would personally prefer that they explicitly returned something and/or got some explicit input. As they are here, you are relying on whatever their side effects are and, if it's not clear (or it changes at a later point) you will want to reconsider your choices.

1

In JavaScript terms, this like the following C# code:

using Utility1;
using Utility2;

public class ExampleClass
{
    public void Foo()
    {
        // Use a class in Utility1
        // Use a class in Utility2
    }
}

Yes, your code is in JavaScript, and this is in C#, but the principal is the same. Import code from another library and use it.

This is pretty standard across languages and tech stacks.

protected by gnat Sep 23 '18 at 11:02

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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