BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage Articles Got NIM?

Got NIM?

Key Takeaways

 

  • NIM is an open source language that is on the verge of becoming a fully operable 1.0 language
  • NIM is unique in that it's output is source code written in other languages (primarily C and Javascript)
  • NIM can be of great value when integrated with legacy C and/or JavaScript code.
  • NIM produces high quality C code that provides optional safeguards against classic C coding errors such as memory leaks and array pointer mishaps.
  • Learning NIM is perhaps worthwhile solely on the basis of it being a a great tool for (re-)learning well-written C or JavaScript code creation, while revealing the interesting relationship between the lower level code and the higher order NIM language.

What if I told you there were a programming language that is more elegant than Python, is efficient like C, is easily compiled to JavaScript, and utilizes user-friendly Tracebacks?

Nim is one of the most underappreciated languages available, and it may be just right for you.

What is Nim?

Nim (originally Nimrod) is a statically typed, imperative programming language that compiles natively into C and JavaScript. It is free and open source with an actively maintained GitHub repository

Andreas Rumpf first designed and developed Nim in 2008. The other principal developer of the project, Dominik Picheta, wrote the language's package manager, Nimble, and authored the book Nim in Action as an introduction to the language.

Nim is designed to be "efficient, expressive, and elegant"  by compiling to C and taking inspiration for its syntactical design from languages like Python, Lisp, and Pascal.

Nim's core features are unique among scripting languages

Nim is not just beautiful; it is also dynamic, but not like other scripting languages. If you've ever been burned by binary dependencies, or weak typing just isn't for you, Nim offers a solution. 


A Nim code sample from the language's official website, licensed MIT

Nim’s familiar type system

Nim’s type system should be easy to comprehend for anyone with fundamental programming knowledge. Common types like String literals, numerical literals, identifiers, keywords, comments, operators, and other punctuation marks are written as in most other languages. Nim also allows users to create custom types for expanded customization.

Uniquely, Nim’s type system uses "references" to implement pointer types. Of these references, there are traced and untraced. Traced references point to objects in a heap that is garbage collected, similar to what a java developer would be familiar with. Untraced references, otherwise known as pointers, point to memory that has been manually allocated or to objects located elsewhere in memory. 

Nim is fast like C

Associating itself closely with C enables a few key features that may impress Python users, and the language's efficiency is no exception. Because Nim compiles directly to C, you can expect your scripts to far outpace Java equivalents, and you can easily implement them in legacy C code.

Nim offers this speed even while maintaining customizable garbage collection, which users can specify to maximize speed or determinism. The fastest option, deferred reference counting, affords memory-safe, pause-less code execution.

Memory-safe C porting

Another consequence of its close relationship with the language C, Nim offers foreign function interfacing that is compatible with C, C++, and Objective-C. This means code optimizations can help your scripts reach top-notch speeds offered by C.

Its foreign function interfacing is also simple and elegant because of the language's metaprogramming features. This allows even the pickiest of users to alias functions and custom keywords to allow their code to read exactly the way they want it.

Not only does its interfacing with C offer code optimization, but these optimizations are also memory-safe because Nim is a garbage-collected language. This means you get the memory safety you’d expect from a scripting language without sacrificing efficiency.

[Click on the image to enlarge it]

Garbage collection performance, measured against some comparable languages. Graphic from Nim’s official website, licensed CC BY 3.0

Metaprogramming with robust preprocessor macros

Nim offers pre-compiler code substitution via templates and their lower-level equivalent, macros. These allow users to effectively extend core Nim syntax (although syntax changes are not supported with these features). These features are also implemented in Nim’s standard library as well as various community-built packages, allowing users to program in various paradigms and bypass things like boilerplate.

Example of utilizing macros in Nim. Graphic obtained from Nim’s features page

First-class JavaScript support

Nim also offers an option for compiling into JavaScript, allowing developers to leverage Nim features in their JavaScript codebase. Included in Nim's standard library is a module named "dom", which powers Nim’s web scripting capabilities. This module also enables users to integrate Nim code, compiled into JavaScript, into legacy JS projects.

One notable project created with Nim’s JavaScript compilation feature is this rendition of snake. The source code can be found here.

Graphic from Nim’s features page

Humane tracebacks

If Python tracebacks have ever come in handy to save the day, you'll also love Nim's tracebacks, which are modeled directly from Python. Tracebacks include useful error messages at the end of the log, making them easy to navigate and minimizing the amount of thinking required during debugging.

Example of a traceback from Nim. Graphic obtained from Nim’s features page

Downsides of Nim

Picheta described Nim as "Python done right,"  and points to features like dependency-free binary products, strong typing, and support for generics—all features that are not native to Python—as differentiating factors of the language. Indeed, Nim looks and feels a lot like Python, down to the syntactical significance of indentation.

However, Nim is far from attaining the popularity or wide support of Python, and it is used in very few production environments.

Software quality consulting company TIOBE releases monthly rankings of top programming languages based on popularity and relevance. Based on their rankings, Python was the fourth most popular language in July 2018. Python received TIOBE’s 2010 and 2007 "Programming Language of the Year" awards for recording the highest rise in popularity in each of those years.

Nim has never appeared in the top 100 of TIOBE's rankings.

For all the syntax offers in terms of style and grace, Nim still lacks considerably in areas that are key to widespread adoption.

Nim seems to be stuck in beta

There is no official timeline for languages or software to "transcend" the beta stage of development into a release, and there is no evidence that development on Nim is slowing

However, bringing a language to release stage indicates a commitment to maintaining that language to meet market expectations on matters such as maintenance, documentation, and support.

First developed in 2008, Nim has gone 10 years without a 1.0 release. Fortunately, it seems that this perpetual beta cycle will soon come to an end. NIM version 0.20 was just released in June of 2019 and serves as the release candidate for version 1.0. Once version 1.0 is released, developers can count on a stability guarantee and this will make it easier for NIM to make its way into larger projects.

[Click on the image to enlarge it]


Minecraft is a famous example of software that remained in beta for an unusually long period. It reached an official release and was eventually acquired by Microsoft. Graphic from Stack Exchange, licensed CC BY-SA 3.0

Small standard library

The website on the Nim language boasts that Nim has a "Small core with a rich prospect for extensibility." What this means, in the words of Picheta, is that "the standard library does need some work." 

However, accessibility to making core Nim language contributions may be considered a key feature.

While major languages have stable code bases that receive changes only from very experienced language developers or official team members, Nim is notable in that it is hosted publicly and invites community contribution. Both issue tracking and code base pull requests can be made by even intermediate programmers with a desire to implement proposed features.

Few third-party libraries

Despite its open nature and robust, language-specific package manager (Nimble), Nim's third party library offerings are somewhat limited.

According to Picheta, third party Nim libraries are not as abundant as he would like

Again, though, this offers beginner and intermediate programmers an opportunity to write quality third party libraries without fear of redundancy. Robust communities around programming languages tend to offer very complete feature sets in the form of third party libraries. This is plus for developers who use the language, as it gives them ample resources for using the language.

However, this abundance of third party libraries can present missed opportunity for programmers seeking to make community contributions in the form of libraries. What may not make the cut of being a standard language feature can at least be submitted for community approval in the form of an external library.

With Nim, the lack of community libraries provides programmers an opportunity to make community contributions to a developing language.

[Click on the image to enlarge it]


The graphic for Nim’s package manager, Nimble. The crown has been used as Nim’s official graphic since at least 2014. Graphic from Nim’s official website, licensed CC BY 3.0

So, who's the target audience for Nim?

Despite the drawbacks of using Nim in production environments, Nim may still be appropriate for some.

Nim may, for example, be appropriate for programmers who are interested in learning about language development. Whether you want to understand how language paradigms impact the usability, readability, and applications of a language, or if you just want to have your name on a "contributors" list, Nim's small community of developers makes it very accessible for new contributors. Don't underestimate the power of the hands-on experience that comes with contributing to a project like Nim!

In addition, Nim could prove to be very useful to programmers who have working C++ or JavaScript codebases, but who want to take advantage of the third party libraries, packages, and Nim’s other language features, thanks to its compilation compatibility and overall impressive versatility. 

As for users who may benefit from programming in Nim, the audience is small. As previously mentioned, Nim is not ready for prime-time, so production implementation is likely not a good option for most users.

However, for anybody looking for a new scripting language because Python just doesn't quite cut it, Nim may be a useful tool for you. It affords sugary syntax for those who enjoy reading and writing concise scripts, and it is also among the fastest scripting languages because of its close relationship to C. 

Drivers of NIM’s Growth Moving Forward

With any programming language, there must be a growth driver for widespread adoption to occur. For Ruby, the Ruby on Rails web framework lead to a surge in popularity. For Python, a number of variables help drive growth, including ease of learning and the network effects of a large developer community. 

This begs the question: what are the potential growth catalysts for NIM? At a high-level, there are three key drivers that can enable NIM’s growth: the release of version 1.0, increased module availability, and cultivating a reputation as a solution to the "two language" problem. 

Getting out of beta is a must if NIM wishes to achieve widespread popularity. The recent release of version 0.20 is a great sign for the NIM community. The sooner NIM can provide developers with a stable 1.0 release, the sooner it can become a viable candidate for large projects that demand stability. 

We’ve already discussed the negative impact of limited module availability with NIM today, but, if growth continues, this narrative can change. Currently, NIM has just over 970 modules available and there has been an impressive growth rate over the last two years. As more modules are developed, NIM will become more extensible and practical for use in a wider variety of projects. This will, in turn, lead to more exposure and use by a wider group of developers, which will result in further development of modules for more integrations and applications. In short, module development will make NIM’s adoption more practical for a wider range of projects, and it will incentivize developers to use it. Finally, barring a single app that takes the world by storm (e.g. what Ruby on Rails did for Ruby), NIM needs to cultivate a reputation as an effective solution to a real-world problem. Fortunately, NIM has a niche it can exploit here. The "two language problem", or the need to use a faster static programming language (e.g. C++) to optimize code otherwise written in a high-level language (e.g. Python). As empirically demonstrated by Chris Smith’s submission to the Advent of Code programming challenge, NIM’s blend of simplicity and performance help make it a contender (along with languages like Julia) as a solution to this problem. 

Got Nim?

Regardless of whether you think Nim is the language for you right now, it is certainly worth attention. It is a fast, versatile language with significant community support. The language certainly has drawbacks, some more serious than others, but its potential is undeniable. Perhaps with more attention and support, Nim will supplant the very languages it seeks to improve upon.

About the Author

Carter Pape is a writer and programmer for DO Supply Inc. with six years of software development experience and nearly 10 years of coding experience. He is an expert web developer, having built two of his own websites and maintaining three others, including a project dedicated to open source software engineering research at North Carolina State University. Pape has extensive experience with scripting languages, including Python, Swift, and JavaScript. He has also worked on projects developing in C and C++. 

 

Rate this Article

Adoption
Style

BT