Namespaces

This is a ClojureScript library providing a few essential macros for operating with native Javascript objects ("oops" stands for "Object OPerationS"). Cljs-oops provides optimizer-safe property and method accessors, compact but efficient nested property accessors, and development-build-only safety checks that catch many common errors.

For example, the ClojureScript form (.-nativeProp obj) will compile to obj.nativeProp in Javascript.

It works pretty well during development but there is a catch! When you naively write code like that, it might not survive advanced optimizations. Closure Compiler needs some information about which property names are safe to rename and which cannot be renamed because they might be referenced externally or dynamically via strings.

Someone at Google had a quick and bad idea. We could provide a separate file which would describe this information. Let's call it an "externs file"!

I'm pretty opinionated about using externs. I hate it with passion. Here is the list of my reasons:

Development behaviour is disconnected from production behaviour - discovering breakages only after switching to :advanced mode. I know, I should continuously run tests against :advanced mode. But :advanced builds are pretty slow and it is no fun to fish for "Cannot read property 'j349s' of null"-kind of errors in minified raw Javascript files which could balloon to multi-MB sizes. Have to wait for quantum computers to provide our IDEs with enough computational power to parse and syntax-highlight multi-megabyte one-line Javascript files ;)

Say, authors of a useful (native) library don't provide externs file (usually simply because they don't use Closure Compiler). So there must come someone else who is willing to maintain an externs file for their library by following changes in the library. You want to use the library so now you made yourself dependent on two sources of truth and they don't usually move in a lock-step. Also that someone will probably sooner or later lose interest in maintaining the externs file and you have no way of telling if it is outdated/incomplete without doing a full code-review. And the worst part is that "someone" is very often you.

Incomplete (or outdated) externs files provide no feedback. Except that you suddenly discover that a new build is broken again and you are back to "pseudo-names fishing".

Externs have to be configured. Paths must be specified. Externs are not co-located with the code they are describing. It is not always clear where individual externs are coming from. Some "default" externs for standard browser/DOM APIs are baked-in Closure Compiler by default which might give you false sense of security or confuse assumptions about how this whole thing works.

What if I told you to ditch your externs because there is a simpler way?

Simply use string names to access object properties in Javascript (in cases where you would rely on externs). Instead of (.-nativeProp obj) write (aget obj "nativeProp") which compiles to obj["nativeProp"]. String names are not subject of renaming in advanced mode. And practically the same code runs in development and advanced mode.

I hear you. This looks dirty. We are abusing aget which was explicitly documented to be for native array only. Alternatively we could use goog.object/get or the multi-arity goog.object/getValueByKeys which looks a bit better, but kinda verbose.

Instead of investing your energy into maintaining externs you could as well incrementally write a lightweight Clojure-style wrapper functions to access native APIs by string names directly. For example:

It is much more flexible than externs. You have full control and power of ClojureScript code here. And who knows, maybe later you will extract the code and publish it as a nice ClojureScript wrapper library.

Sounds good? With oops library the situation can be even better. What if we had something like aget but safer and more flexible? I'm pleased to introduce you to oget...

Please note the ".?" is a modifier for "soft" access (inspired by CoffeeScript's existential operator). We expect that the key 'k31' might not be present and want oget to stop and silently return nil in that case.

In case of oset! you can use so-called "punching" for creation of missing keys on path. For example:

(oset! (js-obj) "!k1.!k2.!k3" "val")

That will create k1 and k2 on the path to setting final k3 key to val. If you didn't specify the exclamation modifiers oset! would complain about missing keys. This makes sense because if you know the path exists for sure you don't want to use punching and that will ultimately lead to simpler code generated in :advanced mode (without any checks for missing keys).

Dynamic selector is a selector which is not fully known at compile-time. For example result of a function call is a dynamic selector:

(oget o (identity "key"))

At runtime the form result is the same but generated code is less effective. Dynamic selectors should be very rare. By default, oops assumes that you want to prefer static selectors and dynamic selectors are unintentional. Compiler will issue a compile-time warning about "Unexpected dynamic selector usage". To silence this warnings use "plus" version of oget like this:

(oget+ o (identity "key"))

This way you express explicit consent with dynamic selector code-path.

By default, oops generates diagnostics code and does pretty intensive safe-checking in non-advanced builds. As you can see on the screenshots above you might get compile-time or run-time warnings and errors when unexpected things happen, like accessing missing keys or traversing non-objects.

Well, only if the strings are computed dynamically at runtime. In case of string literals Javascript parser can see them and there should be no reason to treat them differently than dot properties. But you don't have to worry about this. Google Closure compiler rewrites string literals to dot property access whenever possible.

Should I use cljs-oops with Closure Library (e.g. goog.something namespace)?

No! Use oops only for interop with external code which is not part of your :advanced build. That means for all code where you would normally need to write externs.

Closure Library is compatible with advanced compilation and identifiers get properly minified during compilation. You don't have to write any externs for it, so you don't have to use oops with it.

Second area where you want to use string names is when you work with JSON data objects (e.g. data received over a network). String names explicitly prevent minification of key names which must stay intact.

Externs inference is very recent feature and looks promising. It was introduced after I put all the effort into developing this library so my opinion is biased :-). I personally still prefer investing time into building light-weight ClojureScript wrapper libraries using string-names than dealing with externs (even if they are auto-inferred).