- Was it the project manager, or maybe that senior tech lead guy with the massive beard that made you write the docs?What I want to describe in this post isn't complex Word documents explaining some software. It isn't a guide on how to write something clever about a public FirstName property of a Person class.
It's about JavaScript and data types. Keeping it simple, and fun.
JavaScript won't stop you from passing in whatever you want to a function, even when the code is expecting data of a specific type. It will crash during runtime instead, yes. When parsing or iterating something that cannot be parsed or iterated.
- No type checks for you!This is also what makes the language very flexible. Simplistic syntax, functions as first class citizens and with a little less noise. There is no need for explicit data type declarations, generics or interfaces, making it easy to write code in less than 100 characters width. In fact, you will find yourself writing code in less than 80 chars quite often. In addition, having only one thread to worry about is a good thing (for most of us). And I believe the V8 engine is fast enough too.
- It would be nice if there was something that could let us have a little bit of both.
Did you say TypeScript?
TypeScript is cool. But I'm not sure it is the way to go for back-end code written for Node.js. Node.js itself is probably a bad choice if you are in great need of strongly typed language features on the server. Why not go for C# instead?
If your code lives in the browser, well, that is something different. You are basically stuck with JavaScript for web development - whether you like it or not. TypeScript, or WASM, can probably help you - after some initial setup, headache & build scripts.
Have you tried JSDoc?
During my work on the Open Source project node-zookeeper, I have learned how JSDoc can help us make the JavaScript Developer Experience a lot better - without the headache. Your editor will give you hints and warnings about parameters and data types.
Here's an example:
/** @returns {number} */
function getRandom () {
...
}
With parameters:
/**
* @param {number} minValue
* @param {number} maxValue
* @returns {number}
*/
function getRandom (minValue, maxValue) {
...
}
In the example, documentation is used only for describing the data types of input parameters and the return type. Nothing else. I would recommend you to try hard and refactor the actual code, making additional comments not needed. Is the function name getRandom good enough?
- Maybe I should add some comments to the code here?
(don't do it)
Your editor is smart
Your IDE or editor will figure out the JSDoc syntax, even for custom type definitions. Custom type definitions are for objects - custom data types - that cannot be described as built in strings, booleans, numbers, objects or arrays.
Another example, this one is from the typedefs.js of the node-zookeeper project, describing a ZooKeeper node changed callback:
/**
* Watch callback
* @typedef {callback} watchCb
* @param {number} type
* @param {number} state
* @param {string} path
*/
The type definition for the watcher can be referenced in the code like this:
/** @param {watchCb} callback */
The node-zookeeper project has a typedefs.js file in the root folder. A smart IDE, like WebStorm, will parse it and use the type definitions to give you and your coder friends a nice developer experience.
When dynamic isn't good enough, use JSDoc. But be careful, use it only when needed. Or else, you will be stuck in the Documentation Swamp.