Pietrzak Roman
Kemu Studio - yosh.ke.mu
First version: 2016
Last changes: 2018-06-08
All rights reserved


ES6


Optimal JS, how V8 is built, V8 flags, memory optimization etc.


node.js good practices

Autorestarts

during development: nodemon
for production: forever

any form of live-reload instead of hitting F5

I mostly use the one which is a part of Kinession Framework
but there is plenty of choice of packages

Good OOP

OOP is king, KISS is another king.
Good code, proper modular architecture (instead of monolith).
Don't reinvent the wheel - there is plenty of code packages for most of stuff you want to do. But don't be affraid of writing your own code - when what's available is weak.
Forget false promises of "the only one framework" like Angular, React, Vue, etc.* - these will not solve a problem of NOT having architecture in your project.
[weak]* I'm not saying not to use them!
Callback hell ? Promises chasing other promises are "promises hell". Think about structure, design your code, log stuff, think again.

Good logs

Log a lot and often. Log EVERY error. Log every warning.
It's easier to get through good logs when something strange happened, than to investigate from "zero" point.
Don't safe too much of space - MBs and GBs of logs per day are managable.
I log witk K.Log as it logs with a good "context path" (machine->app->http_connection path), but there is plenty of libs.

Auto tests

Runner: Mocha.js
Web testing: Selenium. If you need more stuff: karma.
Do not ignore tests. Do not write too many tests.
Do not write unit tests for what is not an unit: think twice if it's better to test a thing at the module level, or API level or UI level. It's rare case that you need them all.
Too many tests on too many layers - slow development. Not enough tests or stupid tests - slow development.

Links


General Web


Some general node.js

npm install --global --production windows-build-tools

Electron

Electron-gitbook

Using forever to run/restart/stop node.js server

This line is the main hint:
sudo forever -l log_production.log -o log_production.out -e log_production.err start -c "nodejs --max_old_space_size=300" production/src/run_production.js
this will run the server with 300MB limit for "old-space heap", and log stuff to "log_production" series of files.
WARNING: Look for max_old_space_size instead of max-old-space-size - which is advised in many old forums/stackoverflows etc.

Restarting and listing:
sudo forever list
this will show the list of uid-s of running forever-spawned processes. Pick a process and:
sudo forever restart <uid>
or
sudo forever stop <uid>

HTML


Using Jake


Transpiling

How to serve ES6 to modern browsers, and ES5 to old ones.

HTTP2

HTTP/2

CoffeeScript is dead

We loved CoffeeScript at some point and 99% of node.js/browser code we did in CS.
However, now with ES6 in place, the CS is a bit of dying thing.
Some thoughts:
  • The tool which is good enough to convert CoffeeScript to Javascript: https://github.com/decaffeinate/decaffeinate
    • return: it produces a lot of return statements at the end of functions which don't make sense - it's because of "functional" nature of CS
    • Array.from: in many/all for-loops which iterate over arrays it does Array.from(), which in many cases is useless and makes the code harder to read/optimise. And this breakes IE11
    • get the good thinking about ; (semicolons). We decided to have a loose policy - we mostly leave them in the code.
    • formatting of linebreaks around {} and few other formattings
    • an ugly hack for class constructors, where this code:
      constructor: (@x, @y) ->
        super()
      
      produces a lot, lot of noise JS code
  • Reading/editing CS is a big pleasure for us, while JS looks a lot more clunky
  • ES6 classess work well, the one missing point is static class variables (or class consts !)
  • we use a lot of namespacing for class names (react does it too ?)
    class K.somethingA extends K.somethingB
      ...code of the class...
    
    which doesn't work with ES6 classes.
    The ES6 pattern is ugly:
    K.SimpleObjectReadRemoteModel = class K_SimpleObjectReadRemoteModel extends K.Model
    {
      ...code of the class...
    }
    
  • we really miss @. I mean this:
    this.fieldA = this.fieldB + this.fieldC;
    
    comparing to this:
    @fieldA = @fieldB + @fieldC;
    
    when code is more advanced makes it a lot harder to read now...
  • we miss good existence operator
    It means this:
    if x?
      @set(x)
    
    is now
    if (typeof x !== 'undefined')
    {
      this.set(x);
    }
    
    In our Kinession framework we use "operators" on K.Object:
    K.isExistent(x)
    K.isNotNull(x)
    K.isNotEmptyString(x)
    K.isUndefined(x)
    
    However, still, writing CS code of:
    main?.instance?.of?.some?.class?.setVariable(x)
    
    is now a huge pain.

Libs, Frameworks etc.


Other stuff

node-gyp and Python problems

If you haven't got python installed along with all the node-gyp dependencies, simply open Powershell or Git Bash with administrator privileges and execute:
npm install --global --production windows-build-tools

JavaScript failed !
So this is static version of this website.
This website works a lot better in JavaScript enabled browser.
Please enable JavaScript.