← Back to context

Comment by crooked-v

8 years ago

Good work.

For a next step, I would suggest giving a try at breaking it up into multiple files using import/export statements (it doesn't need it but it's good practice), and using a bundler like Parcel to build the finished file.

    npm i --save-dev parcel-bundler

...then move your source JS files to a folder named src, and add to package.json...

    "scripts": {
      "prepare": "parcel --target node src/index.js"
      "start": " node dist/index.js"
    }

In your individual files, you do like...

    // file-1.js

    import somethingName, { whateverName } from './file-2'

    // file-2.js

    export function whateverName { ... }

    const somethingName = '...';
    export default somethingName;

...and then run...

    npm run prepare
    npm run start

(The prepare script is also a "magic word" that will auto-run anytime someone does 'npm install', so usually an end user won't have to manually run it.)

A bundler isn't strictly necessary if you plan to only run something in Node, but it lets you make a package that's usable in both Node and web projects, and it makes it simple to use Babel, which lets you Javascript features from the future that aren't actually in Node proper yet.

How about we let the kid learn how to program (which will outlive your favorite bundler and file hierarchy anyway) first? It's a 100 line script, one of the mistakes I made learning how to program was trying to be like the adults and focusing on minutiae like code organization rather than good code.

EDIT: story time. I once tried to make a 2D game engine in my late teens, but I wanted to be like the adults and have a proper class hierarchy. Suffice to say, I never finished it. Come to find a few years later, now class oriented OOP is passé and I was wrong to waste that time on it.

Learning to code and get things working is more cool (and in the end, more profitable) than making your code fit the norms of the day, which won't last anyway.

  • My intent is less "code structure", and more "most of the pieces you need to make a Node project also work in the browser". Imports/exports and Parcel would get liamilan most of the way towards being able to do...

       if (window) {
         document.write(...)
       } else {
         console.log(..)
       }
    

    ...and have it just work, without having to write everything twice.

That last thing a 12 year old needs is a complicated build toolchain.

  • Everybody needs a build toolchain eventually, and learning it early saves the eventual "this webpage has become a 5000-line JS file and it's incredibly unwieldy to maintain" hassle with a more complicated project.

It's a program that fits in one screen and has 3 functions. No need for these trendy overcomplications.

Of the things to learn when one is starting out, I would not put code modularisation and toolchains at the top of my list (especially for something so small).

Additionally, this runs in Node, so you don't need to compile the source files with `parcel`.