1

I'm a young developer and I'm trying to get some best practice down.

I hope my question isn't subject to too much opinion and I can get a standard answer here.

Basically I have a big ol' JS file I wrote, couple thousand lines long. There are three main types of things? (objects?).

  1. exported functions I use on my node.js server to manipulate data submitted
  2. variables used by the exported functions and nowhere else
  3. functions that are called by the exported functions in this file and nowhere else.

I'd like to split them up so that I don't have to scroll so much when making edits. In my head it would make sense to have the three files, with the "exported functions" file calling the variables and functions it needs on top.

Is this common practice? Is there a best way to call variables and functions from another file? Any advice here would be greatly appreciated.

David Makogon
  • 69,407
  • 21
  • 141
  • 189
Magic Man
  • 15
  • 7
  • sounds like 1+2 should be imported into 3 – dandavis Oct 25 '14 at 22:15
  • Separate *form* from *function* – Weather Vane Oct 25 '14 at 22:44
  • It's best to keep files as short as possible - if I were you, I'd rebase that file into much more than just three files (~600 lines each), I'd do maybe 10 or so. Probably just opinion though, node has 2k+ line files. – Brendan Oct 25 '14 at 23:31
  • Not sure why the edit was approved; rolled back. It added no value, and left an unneeded intro and outro. Please think twice before approving such edits. Or correct the edit accordingly. – David Makogon Oct 26 '14 at 00:11

1 Answers1

1

As long as you're asking for preference that's what you gonna get. But today I want to share part of my personal experience as a programmer that's why I'm writing an answer rather than just commenting.


Modular Programming is good

As long as a software can be managed and modified with ease keep it as it is. If it starts to become cumbersome then consider OOP.


Structured Flexibility

The real power of OOP lies in its flexibility to make any program easy to implement because the planning is usually done beforehand. With the help of UML diagrams you can decide how to best represent an application.


OOP is about acronyms

The most scaring part of OOP is its acronyms, which will take sometime to be digested and sometimes may even sound scary. But those acronyms will help you think of software from a better perspective in the long run.


Consider Design Patterns

Since javascript is not a real OOP language I used to think that design patterns were not applicable to it. Indeed, patterns is more about solving problems than teaching a best practice. Consider structural patterns for inspiration.


Programming is a journey

Because you're young I believe you have plenty of time for making mistakes and learning from them. Make a decision. Try something. Your learning curve will boost in the right direction.


Finally, stay close to StackOverflow. There's much to learn around here.

carlodurso
  • 2,886
  • 4
  • 24
  • 37