How to write Node modules with Rust
TLDR: there’s a sample repo here if you’re lazy to read this post. The sample repo include GitHub Actions sample for CI as well.
The Rust bit # It’s very simple. You write your function in Rust
#[js_function(3)] fn say_hello(ctx: CallContext) -> Result<JsString> { let name = ctx.get::<JsString>(0)?.into_utf8()?; let name = name.as_str()?; let s = ctx.env.create_string_from_std(format!("Hello, {}!", name))?; Ok(s) } And then you expose it to Node.js runtime with
Sharding and IDs
So I was going through this post from Instagram Engineering blog while researching for some sharding solutions.
The solution is quite elegant and I decided to port this to MySQL.
Turns out, it’s harder than I thought since we’re using a pretty dated MySQL version at work. There is no sequence, just AUTO_INCREMENTAL. In order to use the code snippet for PL/PGSQL, I would have to find a way to mimic nextval function.
Explicit over clever
I always prefer explicit over clever, hacky hack. Explicit make the code looks clearer, more maintainable and leaning toward a more predictable behavior (aka junior developers will be less likely to mess it up).
Take an example of this code where I have a folder called providers. This here below is the content of the index.js file which basically read all the files in that folder (except index.js), require them and then module.
Some of the most useful tips I learn when working with NodeJS
modules management with npm # Nodejs comes with an amazing package manager called npm. Start a project with npm init which will then create a configuration file named package.json, keeping track of all the modules your project is using. You don’t have to manually manage this file yourself. If you want to add a package to package.json you can add --save parameter when installing it.
npm install koa-static --save Also, you should ignore npm_modules folder when git push because whoever clone the repo can do npm install by themselves.
Things software developers wish they had known in their 20s
The era in which a common career trajectory is to become a lifer at some software company and work one’s way up to higher and higher internal positions has been over for a long time (This era had already been over in the 1990s when I was a young programmer).
You don’t owe the company you work for anything beyond what you put your name on when you signed the employee agreement.
REST APIs made easy with StrongLoop
StrongLoop allows you to quickly create REST APIs using their graphic interface and CLI. SLC also supports debugging, profiling, tracing, deploying as well as monitoring features.
Creating REST APIs with slc is as easy as creating datasource and model. StrongLoop will do the rest for you.
I was very tempted to use StrongLoop for a recent project but I had to deal with a legacy database that use a very old odbc driver which force me to use node.
Fuck callbacks! Let’s use generators
Let’s write a simple function hello that return a string when called.
var hello = function () { return 'Hello ' + name; } Now convert it into a generator. Call hello() this time will return you an Object instead: the generator.
var hello = function *() { return 'Hello ' + name; } Let’s consider the following snippet.
var hello = function *() { yield 'Stopped here!'; return 'Hello ' + name; } var generator = hello('Clark Kent'); console.
bluebird - a promise library with unmatched performance
The documentation of bluebird is so much better than Q - the library which I’m currently using. bluebird with nodejs is even better: it can convert an existing promise-unaware API to promise-returning API which is so awesome. Thid feature works with most popular libraries (which use error as first arg (as they all should)).
I’m sold!!
Usage # function getConnection(urlString) { return new Promise(function(resolve) { //Without new Promise, this throwing will throw an actual exception var params = parse(urlString); resolve(getAdapater(params).
The next bullet on my resume: AngularJS
I started a HTML5 hybrid mobile app project recently. Before I start coding the frontend, I had to decide which client side JavaScript framework I will be using. The choice narrows down between AngularJS (Ionic framework) and Ember.
Of those two, AngularJS is obviously the more popular choice. There’re many frameworks built on AngularJS; thousands of questions on StackOverflow; lots of open-source projects base on AngularJS on GitHub. There are many great frameworks out there but few has gained so much developer mindshare like AngularJS.
Shorter code is better?
Which one do you prefer: shorter code or more legible code? As for me, legibility of the code always come first. Working in a team will make one realize the importance of code legibility sooner or later. Your code is not for you alone to read. It should be friendly and easy for whoever has to maintain your code as well. It’s not easy to write elegant code but it’s much easier to write legible code.
Some useful use cases for Java Reflection
Some examples of useful use cases for Java Reflection
Clean code/dirty code
I like to think of myself as a perfectionist. I like going back and forth re- evaluate all the possible options and come up with the cleanest, best, perfect alternative (I can think of) out there. If you are like me and you got to maintain someone else’s bad code, this is gonna be a real nightmare. You just want to scratch everything and start over again. Unfortunately, management people doesn’t think so and you don’t get the luxury of times you need.
How to code like a dick in JS
The goal of this post is to make simple things look complicated.
1. Invert logic # Let’s start with something easy.
Instead of
if (x && y) { ... } // easy Write this instead
if (!(!x || !y)) { ... } // good enough 2. Extended unicode chars for variable names # Use of extended unicode chars, especially those that are not visible in console like \r, \t, etc…