r/javascript • • Sep 15 '15

I'm a c++ programmer. Please sell me on javascript.

Not a troll post. Please read this as an earnest letter from a wizened programmer new to the whole JS thing.

Some background. I'm a polyglot programmer with professional experience in C, C++11/14, Haskell, Erlang, Elixir, Lua, Ruby, Python, PHP, and even some Javascript and other scripting languages too (throw in CUDA, HLSL, GSL, PSSL, etc). I've written my own compilers and am generally very comfortable thinking about type theory and language semantics. I have a heavy bias towards managing my own memory because I like predictable runtime behavior and work in memory constrained devices or consoles.

My issue is with Javascript. The whole world is slowly converging on this language that more or less materialized out of thin air with the advent of the browser. I want to be open minded and like it, but I can't get over its, shall I say, "deficiencies." By deficiencies, I mean a lack of all the standard built-in abstractions that make an enterprise language an enterprise language.

That said, I worry that my perception is potentially unfounded and I am suffering from biases that are clouding my judgment. So now I'm turning to you /r/javascript! If you came from a world similar to mine, and have come to terms with investing in javascript, I'd love to hear your opinions. Alternatively, maybe I'll learn that JS is just going to stay whether I like it or not so I should just suck it up and deal with it.

edit: I appreciate the time people took to write responses! It will take me a while to read it all.

1 Upvotes

77 comments sorted by

12

u/Rhomboid Sep 15 '15 edited Sep 15 '15

Here's the sales pitch for JavaScript: it's the only language that runs natively in all browsers. *Mic drop*.

If you don't care about browsers, then you probably don't need to care about JavaScript. But a whole lot of people do, because it's the easiest way to get your application in front of a huge number of people with very little friction. And once you're caring about browsers, and writing client side code in JS, then it becomes nice to be able to unify your whole toolchain by using JS to write your build tools, your testing tools, your package manager, etc. And from there the next logical step is to want to write your back-end in JS, which in some cases allows you to share code between the back-end and the front-end. For example, if you're writing a SPA but you want the initial page load to be prerendered server-side, you don't want to have to have to implement your view logic twice and maintain two copies of it in different languages/frameworks.

Edit: and even if you don't care about browsers, you can still draft off of all the effort of people that do. V8 for instance (and other engines too, but notably V8) has had a ton of effort thrown at it to make executing JavaScript as efficient as possible, so if you're considering embedding a dynamic scripting language in some larger app and performance really matters, then V8 might be worth a look. (LuaJIT might be worth a look too.) You get the side benefit of the millions of web people who are already familiar with JS being able to use your embedded scripting language.

2

u/[deleted] Sep 15 '15

So the primary things going for it in your mind are (growing) popularity and ubiquity (coupled by a subsequent growth in tooling and frameworks and such)?

4

u/sime Sep 15 '15

I would put it slightly differently: It's the Platform (Stupid!)

;-) It is not just a language that runs everywhere, the web platform is a rich platform that runs everywhere.

For example, if I'm building a website or webapp I can go shopping for a markdown renderer and be confident that it will work with what I have already, because everything is built on the same display engine (HTML+CSS). When it comes to GUI and graphical work in C++, it too often fragmented across toolkit and operating system lines. A nice GTK widget isn't going to get along with Qt or the Windows API without great difficulty. In the web world this is common. In fact most web pages pull in and combine stuff from multiple different sources (think embedded twitter stream here, a Google map there etc) and renders it all together. I can't think of anything comparable in the C++ world. C++ apps are closed by nature.

There are other political advantages to the web platform. No one entity controls it, standards bodies, multiple major implementations, no fees to use it, decentralised by nature, etc etc.

3

u/[deleted] Sep 15 '15

You make a good point. The standardized display engine is a powerful concept. Part of me wishes that interfacing with it would be language agnostic (some ABI or something) but that may be too large of a technical challenge.

2

u/sime Sep 15 '15

Compiling to JS and/or web assembly is your best hope for language choice.

3

u/Meefims Sep 15 '15

Having come from a C++ background myself, I love JavaScript. Memory management is highly overrated (this is true in C++ as well - the language and its library have taken strides to eliminate direct memory management). JavaScript has a sufficient number of abstractions to make building software comparable to other languages. It also has npm, a great package manager that can help you find even niche abstractions.

1

u/[deleted] Sep 15 '15

I love RAII semantics though. C++ is getting things like std::shared_ptr but personally, even being able to distinguish a stack allocation from a heap allocation and leverage lexical destruction is a godsend.

I will admit that the infrastructure/tooling built around the JS ecosystem seems better put together. The C++ guys haven't agreed on how to do that stuff yet.

4

u/sime Sep 15 '15

Coming from C++ you need to break the C++ obsession with needing to know and worry about every tiny little detail like stack vs heap allocation for example.

1

u/[deleted] Sep 15 '15

Calling it an obsession might be a little heavy-handed. I like to know how things work. If something breaks, or if something gets hacked, or I lose frames or something, I like being able to look at the disassembly and see what's going on. I want to conceptually know if I'm liking to be doing an L1/L2 fetch, or a call to main memory. This might matter if I'm doing something millions of times or even thousands of times depending on what it is.

This doesn't mean I'm always thinking about it. Ideally, I only think about it when it matters, but when I need to think about it, I have a way to reason about things. (incidentally, this is one of the reasons I eventually abandoned Haskell, as much as I love the type system)

1

u/Meefims Sep 15 '15

For a long time I was confused why there were so many languages without destructors since well defined object lifetimes seemed so useful. Having used such languages I realized that it hasn't actually mattered. Objects just go away at some point when I'm done with them.

The same type of thing has happened to a number of other things that C++ allows you to control that other languages don't. I have found that I typically just don't care.

This interacts interestingly with a lack if destructors (or even finalize). RAII appears impossible but I again don't miss it in JavaScript primarily because I don't have too many resources that need to be cleaned up. I'm not working with pointers or handles or COM interfaces. Instead I'm typically working with objects that are themselves garbage collected.

In the cases where I do care, it's just something to work around similar to how C++ programs have to work around a lack of duck typing.

1

u/[deleted] Sep 15 '15

I'm not working with pointers or handles or COM interfaces. Instead I'm typically working with objects that are themselves garbage collected.

This is probably the crux of the argument. Part of the benefit of controlled lifetimes is that I won't greedily hold onto resources longer than I should. This extends beyond memory to mutexes, file descriptors, ports, etc. But if you aren't doing any of the above, the problem is a bit less critical.

1

u/Meefims Sep 16 '15

It's not that I don't end up using these things in JavaScript, it's more that they are encapsulated within objects that are part of or built for the language.

1

u/[deleted] Sep 16 '15

Sorry, yes, I should say you are using them indirectly. A program that doesn't use file descriptors, ports, etc, is unlikely to be useful :)

1

u/[deleted] Sep 15 '15

I agree with all of this except using javascript for the the backend. There are simply cleaner and better solutions to use on the backend, I mean the OP is using Haskell/Elixir etc, so this is not someone who should forgo those and use js on the backend IMO.

4

u/gnarly Sep 15 '15

A major selling point of JS is (along with HTML and CSS) it's made the old dream of "write once, run anywhere" come true (mostly). There isn't a major platform out there without a decent browser and/or JS engine. I once heard the trifecta of HTML, CSS and JS described as "the universal language of everything".

ES6 (the latest version of JS) adds a lot of stuff that turns it into a "proper" programming language, and ES7 appears to be continuing in the same vein. Tools like Babel enable it all to work in current (ES5) JS environments. Development tools are coming along nicely - albeit still treading some of the ground already covered by older, more mature languages.

If you're a C++ developer, and that's where you're most comfortable, perhaps you want to have a look at Emscripten? It's an LLVM-to-Javascript compiler, which outputs a highly optimisable subset of JS, called asm.js. They claim to get relatively close to native speeds on the latest JS engines.

If that doesn't really do it for you, perhaps Microsoft's Typescript is closer to what you're looking for? Again, it compiles to Javascript.

2

u/[deleted] Sep 15 '15

I've been reading about ES6 and ES7 and find some of the proposed changes delightful (others of them will help certain classes of programmers shoot themselves in the foot, hehe).

Generally speaking, when a new version comes out, what is adoption like? I imagine you can't just immediately use it unless users update their browsers. Or does everyone just use Babel and convert to the lowest common denominator?

3

u/Rhomboid Sep 15 '15

There are compatibility tables. Using Babel or Traceur is probably the most common strategy, but it has its issues. For example, for various reasons some features can't be efficiently implemented via transpiling, such as the temporal dead zone. And the overall debugging experience might be impacted, even with source maps.

Browsers do take their time implementing native features and users aren't always using evergeen browsers, so it takes some time for features to trickle down.

1

u/gnarly Sep 17 '15

Bang on. The other thing to consider is not all JS environments are browsers. Server environments like node.js might update on a different schedule (although in that particular case they're using Chrome's V8 engine). Same would be true of OS-level JS engines, such as JSC in iOS.

2

u/[deleted] Sep 15 '15 edited Sep 15 '15

Benefits of C++ over JS:

  • Faster

  • Control over memory allocation

Benefits of JS

  • It can run absolutely anywhere, while you cannot always install or run a C++ application everywhere.

  • JS code is much faster to write

  • You don't need classes in JS. They are there now if you really really want to punish yourself.

  • Closure/lexical scope/lambdas are native concepts in JavaScript from inception.

  • JS is powerful enough now that you can accomplish most application tasks directly in this language without noticeable delay.

  • No static compile time. The code is compiled on the fly at execution time, so just write your code an run it anywhere.

  • Because of the previous point you can edit and test this code substantially faster than a C++ app.

Things non-JS developers frequently whine about:

  • Sloppiness - There is a great deal of sloppiness allowed in this language. Just code defensively and build strong coding practices so that this is never a problem.

  • Doesn't look like Java/C++ class system - This is actually a fantastic quality, but you have to learn to remove yourself from this madness. Read about functional programming. If you attempt to write C++/Java in this language you honestly should fail.

  • Poor understanding of abstraction - The benefit of abstraction is to allow developers to focus directly on the problem at hand without concentration on low level concerns. I have found many developers tend to either want to abstract how the language actually works thereby allowing them to work on a completely unrelated problem and/or falsely identify the parts of development they don't want to do as low level unrelated concerns.

  • JavaScript's relationship with the browser - JavaScript runs almost anywhere. Soon Cisco switches and routers will be running JavaScript natively. JavaScript runs on the Windows command line, and anywhere Node.js is installed. JavaScript has nothing to do with the browser directly. A list of external APIs and features allow JS to talk to features of the browser including: DOM, setTimeout, setInterval, File API, navigator object, window object, and others.

1

u/[deleted] Sep 15 '15

I'm OK with things not resembling the C++ class system (I haven't done enough Java to know what that is like). I recognize Javascript as a prototypal language much like Lua is.

How do you manage things with Javascript like it's global namespace, lack of modules, and performance? Having everything be an object with variable length keys strikes me crippling, but this might just be acceptable for all the javascript programs people are writing.

1

u/[deleted] Sep 15 '15

So.... architecture

I try to limit my global footprint as much as possible. The global space should be limited to an exposed API(s). For example:

var myAPI = {};
(function init() {}());

I would build all my API keys inside that function. All other variables and other qualities would also go inside that function. The only reference in the global space is myAPI, so there is less opportunity for collision, but there is no limit upon what you can do and build with your application code.

Most JavaScript applications make heavy use of modules now. There is not a native module system (yet). A native module system is proposed and standardized, but nobody is supporting it yet. This means people are heavily leaning upon build tools to fill that gap. Such tools are: gulp, grunt, browserify, broccoli, and a couple of others.

In JavaScript object literals are the slowest and most painful data stores. I only use objects to create methods when I create an API or when I need to store data where the keys are commonly known. If I need to store data and the keys are not commonly known I would just use an array. You can do substantially more, from a data perspective and algorithms, using arrays and they are much faster to access.

I tend to avoid inheritance and poly-instantiation in this language. The use cases for inheritance are when you need access to a persistent store in memory that is without constantly being garbage collection since frequent garbage collection will impact performance of a persistently execution application. Examples that would benefit from inheritance are games and applications that perform recursion in extremely long intervals faster than GC can be performed.

Aside from those rare (extremely rare) cases where you need consistent memory performance the typical goal of performance in JS is to execute as absolutely fast as possible everything else be damned. Inheritance won't give you this. An understanding of the scope model will certainly help.

Another thing that helps with performance is to limit access to external APIs (from within an application) to as few as possible. There is a dramatic performance hit every time you have to leave the language for something. Also, be aware that a poor choice of abstraction may make reading and editing code easier, but at a dramatic performance penalty.

As far as code organization I tend to organize all my code as a hierarchy of functions, perhaps how people might use a class hierarchy in C++. This means I am nesting functions, and sometimes deeply so. JavaScript did not get block scope until very recently and it is only supported on very recent software (within the last 1.5 years).

1

u/[deleted] Sep 15 '15

Thanks for your comprehensive response! I'm starting to form a clearer picture of how JS is coded in the wild.

How do arrays work in javascript when objects have variable size? Are they internally pointers to heap memory or are the objects self describing?

I had a little bit of trouble understanding the connection you made between inheritance and garbage collection. Could you elaborate a little?

When you say external APIs, do you mean things like system/OS calls? Or do you mean accessing the foreign function interface? How does JS interoperate with other languages? Is it a typeless stack like Lua?

3

u/[deleted] Sep 15 '15

Different interpreters have different unique optimizations that they use to make arrays even faster. For example v8 has something called hidden classes to optimize arrays (objects too) by data type.

Here is a crash course on arrays.

In algorithms arrays are most commonly populated and depopulated with the push and pop methods, because this is absolutely the fastest way to store and destroy data in JS. Making changes to the middle of an array is much slower, because it causes a change of indices for all following data points in the array. The most powerful array method is splice, because you can add/delete/change indexes anywhere in an array. It comes at a performance cost though.

Avoid spare arrays, which are arrays with empty indexes. This destroys a lot of performance potential. Always ensure every index in an array is populated with data, preferably of the same type.

I had a little bit of trouble understanding the connection you made between inheritance and garbage collection.

Garbage collection occurs at regular intervals and you have absolutely no control over when, how, or what. Normally this is a convenience, because it completely frees the developer from all memory management concerns. Removing the developer from all memory concerns impacts performance in some rare cases though.

Let me back up... The whole point of inheritance/polyinstantiation is memory conservation. The idea is to create an object that serves as a data model in memory and resides in a single known location. Other objects inherit from this stored thing (classes and prototypes), which allows them to clone the object under the hood but draw a reference to something already in memory (cached) instead of writing to a new memory space. This eliminates excess memory consumption (so long as the memory is later freed) and also eliminates excess memory writes to various locations in memory (performance penalty).

But remember... you have absolutely no control over memory allocation in JavaScript, so therefore inheritance becomes almost entirely a stylistic quality (vanity) opposed to a performance tool. One reason why this is true is due to the scope model. Prototypes (and thus classes) occupy a secondary scope chain after the primary lexical scope chain, which means it takes longer to resolve a prototype reference compared to a local reference at execution time. This used to be easy to profile before the JIT, but now it is harder because scope chain resolution is largely cached under the hood.

There are some exceptions where inheritance is beneficial to performance in JavaScript:

  • there is a performance hit to garbage collection, which is noticeable in some long constantly running applications, like games

  • when memory consumption is constantly occurs and occurs faster than it is reclaimed by garbage collection

The performance problem in the first point is most often observed as a saw tooth graph when an application is profiled. What happens is that garbage collection is running at regular intervals in the background drawing away CPU cycles from the primary execution threads of the application. When GC completes the application returns to its prior faster execution until GC executes again. GC can be halted on certain resources by ensuring access to these resources constantly remains alive until the application terminates. This can be accomplished through inheritance.

The second point typically has to do with recursive function calls which can fill memory in mere milliseconds if not properly controlled. This failure can be reduced (somewhat) by storing all data and references (I do mean everything) in prototypes. This usually a lost cause though, because many engines attempt to predict for this failure before it occurs and will throw an error based upon prediction. The preferred solution is reorganize the execution flow of the application to rely upon callbacks and make everything internally event based.

When you say external APIs, do you mean things like system/OS calls?

I mean absolutely anything external to the language itself. For instance JavaScript connects to HTML though an API called the DOM. There is a performance penalty for accessing the DOM merely because you are stepping outside the language. Anything to do with clocks is external to the language, such as setTimeout and setInterval. In Node.js you can create child processes, make system calls, walk the file system, and do all kinds of other external stuff. These things are treated as events and they do execute slower than remaining directly within the language.

How does JS interoperate with other languages?

I do this by opening a child process in Node. In that child process I can issue a command just the same as typing into the console.

1

u/[deleted] Sep 15 '15

I see. Based on your response, I imagine that JS implementations implement arrays similarly to Lua arrays (which are either sparse or dense, and may convert from one to another).

There are strange rules in Lua that may prevent an array from ever being stored contiguously. Reading your post, it seems such concepts exist in JS-land too, so I may have some more reading to do. Thanks again for your reply!

1

u/sime Sep 15 '15

How do arrays work in javascript when objects have variable size? Are they internally pointers to heap memory or are the objects self describing?

Arrays conceptually contain values and/or references to objects (you can think heap here).

How they work internally is magic fairy dust as far as I need to be concerned. The JS engine can employ a number of different strategies and/or optimisations. This is one of the benefits of having a language having a programming model which is abstracted somewhat from the implementation. I programmer just use the clean programming model, and the JS engine gets the space to run my code how it sees fit.

2

u/just-boris Sep 15 '15 edited Sep 15 '15
  1. Global namespace is only browser thing. Node.js has own module system named CommonJS, and each files there has own scope. For browsers you can also use converters from CommonJS code, webpack, for example.

  2. There are several package repositories with JavaScript components. NPM is the main of them, also there is a Bower - repository focused on browser libraries and few others. You can find almost everything that you want or publish your own, if don't.

  3. Performance is not a problem unless you are using hacks. While you are repeatedly creating similar objects with the same fields, everything is going right and JavaScript engine can optimize your code and make its speed closer to native code. It is a nice thing, that you get fast code in general, but can do hacks like object patching in rare cases

1

u/mendokusai_yo Sep 15 '15

You don't have to compile, and your games, I mean programs, are playable by anyone, not just pc users.

2

u/[deleted] Sep 15 '15

See, personally, I would argue that compilation actually catches a lot of my errors and forces me to structure my code correctly.

In addition, compilation allows me (via C or C++) to target ios, android, ps4, xbox one, pc, and more. The C runtime and C++ runtime is more ubiquitous than the Javascript runtime still (and much more performant)

1

u/ehamberg Sep 15 '15

I would argue that compilation actually catches a lot of my errors and forces me to structure my code correctly.

Isn't that the type checker, though? In the world of C-family languages the type checker is always part of the compiler, so you think of them as being inseperable¹, but you can also type-check non-compiled languages.

Facebook has written a type checker for JavaScript called Flow and there are also type checkers for other languages where the compiler doesn't include a type checker, or there is not compiler at all.

[1] They actually are since you need to know the type of values to know how to represent them natively.

1

u/[deleted] Sep 15 '15

Type checking is indeed part of it, but it's more than just the type (int, float, string, etc).

Most of the benefit is derived from taking advantage of type classes (like is this a pointer, or value, is this const or not const). By annotating my variables and arguments, I can signal usage intention to other programmers or my future self and have confidence that the compiler will yell at me if I try to break the contract. Also, it makes it very clear when data copying occurs.

1

u/nodealyo Sep 18 '15

I'd recommend looking into Typescript. It has many of the features found in a common compiler. http://www.typescriptlang.org/

1

u/meoowy Sep 15 '15

There is typescript(microsoft), dart (google) and flow(facebook) for that.

I feel like typescript with code studio is the way to go if you are working on a big project.

But you should learn javascript first.

1

u/spacejack2114 Sep 15 '15

Well JS obviously isn't suitable for PS4 or XBox, nor will it compete with cutting-edge, native game engines. As an indie developer though, I don't think I'd have the resources for a game of that scope, so writing a game for the browser platform works fine for me.

Plus you get to do some good old fashioned optimization and amaze people that your app isn't native. Also consider that:

<canvas id="gamecanvas"></canvas>
<script>var gl = document.getElementById('gamecanvas').getContext('webgl');</script>

is all you need to start writing an OpenGL app.

2

u/jordaanm Sep 16 '15

I would say there is little inherent to the language itself that is amazing. To be brutally honest, for every nice feature, there seems to be multiple ugly ones.

But JS becomes worth it for the opportunities it provides. Runs on the front end, and the back end, and the tooling. Can be run on every modern mobile/desktop platform.

If you look at something like React Native or Microsoft's JS initiatives, they come extremely close to the write once, deploy everywhere ideal that so many strive for.

Beyond that, with stuff like V8 and asm.js and all the other optimisations going on over the last decade or so, its starting to become very fast. Currently lacking a good solution for SIMD, but that's in the pipeline too.

And finally, it's fun. Once you get past the ugly features, it becomes genuinely fun language to throw together solutions with, and you get amazing control over the final look/interface to your code. That was one of my favourite C++ features, that with Macros and operator overrides and other neat feature, you could design a class/library around what you want your code to look like, rather than just what it had to do (see Epic's source code for the Unreal Engine for a pretty extreme example). JS allows for a lot of the same, where you can control the shape of your code.

1

u/rauschma Sep 15 '15

It’s OK as a language (getting better with the latest version, ECMAScript 6), while being very practically useful:

  • The only native language for web browsers.

  • Build apps for iOS and Android via React Native.

  • Build cross-platform desktop apps via Electron.

  • Runs on the server, via Node.js.

  • Can be used for cross-platform shell scripting, via Node.js. Many build tools are written in JavaScript, these days.

  • Has numerous libraries, which are easy to install via the npm package manager.

  • Well supported by many NoSQL databases.

This is a longer JavaScript sales pitch that I have written: “Why JavaScript?”

1

u/callumacrae Sep 15 '15

Many build tools are written in JavaScript, these days.

Relevant!

1

u/[deleted] Sep 16 '15

Can you give me a brief summary of why there are so many? I was trying to look at a bunch of them but I haven't had my boots on the ground yet to understand the feature differentiation.

1

u/callumacrae Sep 16 '15

Eh, same with everything. People don't like existing solutions, so they write their own. It happens in every language, but it seems to happen more in front-end development—for example, there a tonne of MVC frameworks available, too.

I'd recommend Grunt or Gulp. I prefer Gulp and I think it's more widely used than Grunt now, but people still use Grunt too.

2

u/sime Sep 15 '15

All those build tools and I'm still looking for one which isn't garbage and doesn't depend on plugins .

2

u/callumacrae Sep 15 '15

So you're looking specifically for a build tool which has been written by something with exactly the same build as you? Or you're looking for a monstrosity that includes every tool known to man?

What's wrong with plugins?

1

u/sime Sep 15 '15

No, I just want a build tool which can invoke the CLI interface for compilers, preprocessors etc without requiring a layer of additional complexity and bugs in between.

1

u/callumacrae Sep 15 '15

But there is no standard interface for them, so either the tool would need to be aware of every single one of them and would need a major version update every time one of them got a major version update, or it would need plugins, which leaves us basically where we are now: gulp plugins, for example, just call other libraries.

Either way, you've got dependencies.

1

u/sime Sep 15 '15

The build tool doesn't need to aware of anything. It just needs to let me build up a command line for the tool I want, and then execute it. Executing a tool via its command line interface is the standard interface.

It is a strange feature of the JS community that build tools have gone down the plugin route.

1

u/spacejack2114 Sep 15 '15

Didn't you just describe npm?

1

u/sime Sep 15 '15

to a certain extent, yes.

1

u/ImAPyromaniac Sep 15 '15

Why not just use npm scripts then? Or child_process.execSync (or whatever)?

→ More replies (0)

1

u/NeuroXc Sep 15 '15

I wasn't aware that Gulp is garbage. Guess I have to go back and write my own build tool from scratch in C. /s

0

u/aaaqqq Sep 15 '15

Ignore ES5 for now. Take a look at ES6 or alternatively, Typescript. This alone should address some of the concerns you have.

1

u/[deleted] Sep 15 '15

Thanks I'll take a look.

-2

u/dhdfdh Sep 15 '15

Do NOT look at Typescript, except with a jaundiced eye. Everyone remember JScript?

1

u/sime Sep 15 '15

Yes I do, and what's your point? JScript was just microsoft's implementation of JavaScript. It was/is very compatible, bugs and all.

-4

u/dhdfdh Sep 15 '15

hahaha! My laugh of the day. Obviously you're a Microsoftie. No one in their right mind would say what you just did. (And your user name may have given you away, Sime)

2

u/sime Sep 15 '15

What planet are you on?

-4

u/dhdfdh Sep 15 '15

Ahahaha. Not only are you a 'softie but you're a redditor! hahaha

1

u/jesstelford Sep 15 '15 edited Sep 15 '15

professional experience in ... Lua

Then you know JS. Lua is based on EDIT: similar to ECMAScript, just like JS.

As an ex C/C++ dev myself, two other things spring to mind:

  1. You never have to worry about threads / shared memory again.

  2. Prototypal inheritance blows Class-ical inheritance out of the water. Favor 'object composition' over 'class inheritance'. is right there at the start of GoF's book ;)

1

u/uberpwnzorz Sep 15 '15

The real reason are:

  • Higher Productivity

  • Scales well

  • You can reach more people with it faster

  • Community, and developer resources

You can get more done in less time and with less code. It's amazing for small businesses and businesses that are growing quickly.

NPM and Bower are great community resources, and allow for granular version control for all your dependencies. If you're using one library and one of your dependencies is using the same library, you don't have to worry about conflicting versions (at least on the Node.js side). Also, if you like C and Python you can still use these skills to add low level functionality to Node.js modules. NPM will distribute these modules as code and they will be compiled on each machine, so the same modules can be used on different OS and system architectures without having you pre-compile for each one.

Here's a good article from StrongLoop about why many companies are choosing Node.js

I have some other articles that review the productivity benefits, but I need to leave for work.

1

u/quad50 Sep 15 '15

can you run c++ in a browser?

1

u/dhdfdh Sep 15 '15

As a matter of fact, you can

1

u/tonyalicea Sep 15 '15

npm is the largest open-source package ecosystem ever.

first-class functions exist in a popular, mainstream language: Javascript.

You can look at and deeply understand the code of major Javascript engines. they are open-source and mostly written in C++. No secrets.

I find lack of strong typing is not as horrific as it first seems, and can be gotten around with tooling (like TypeScript) if truly deemed necessary.

I find Javascript to be surprisingly expressive. Look into the source code of popular Javascript libraries to find good patterns.

Prototypal inheritance combined with mixins is simpler, yet just as powerful as classical inheritance. I find prototypal inheritance discourages the unnecessarily complex networks of objects classical inheritance pushes us towards. 'objects inheriting from objects' is, in my opinion, wonderful to work with if you don't try to impose classical inheritance patterns on it.

You can get what I call 'an open source education'. Because so much Javascript code is open source, you have a preexisting wealth of good code (the popular libraries and frameworks) to learn from. Embracing the Javascript community is embracing a massive open source community, and that's good for everyone.

I'm a dev coming originally from C++, Java, C#. I've also done some compiler work. At this point, for all the reasons above, at this point in my career I would rather code in Javascript than any other language.

1

u/[deleted] Sep 16 '15

how about just try it?

1

u/[deleted] Sep 19 '15

If you don't like JavaScript and you don't have a need to write JavaScript, maybe life is too short to be writing JavaScript. Write something you want to write in a language you enjoy writing.

1

u/[deleted] Sep 19 '15

I say it this way not to be snarky but to point out that all of the very thoughtful points made on this thread won't in-and-of themselves give you a taste for the language. Some people like Mozart. Some people don't like Mozart. Life goes on.

0

u/shappir Sep 15 '15

Since you're already a polyglot programmer you should know that:

  • Different PLs have different sweat-spots, and which PL you choose very much depends on what you're setting out to accomplish. For example, C/C++ is more appropriate for embedded or real-time programming than JavaScript. OTOH JavaScript is more appropriate for constructing a web-services, for example.

  • Learning additional PLs is always beneficial. Learn JavaScript, play with it, and then make your own informed decision.

All that being said, here are a couple of reasons to buy into JavaScript. Off the top of my head:

  • Native browser support - JavaScript (and derivatives such as TypeScript) is definitely the go-to PL for browser-side programming. That being said, many developers now transpile JavaScript (ES6 -> ES5), and from there it's a smaller step to compile some other PL into JavaScript. wasm may also change the browser-side PL landscape, but it's still a ways away.

  • Node - a great environment from server-side development which also uses JavaScript natively.

  • npm, and a ton of Open Source libraries - whatever you intend to develop, there's a good chance someone created a JavaScript library that will help you do it. npm makes it very easy to manage the 3rd-party code you use.

  • React and React Native - examples of excellent libraries for building front-end applications

Specifically regarding C++: I was a hardcore C++ developer for 15 years, and enjoyed using it. Some of the best code I've written was done in C++. Today though I find it too low-level for my tastes. Yes, modern C++ has lambdas and closures, but the syntax ... I think that C++'s glory-days are behind it and it's time to move on.

1

u/[deleted] Sep 16 '15

Transpile was a new word for me, thanks. Also, I plan on experimenting with React Native soon.

Incidentally, I'm bullish on both C++ and JS actually (albeit for different reasons). If you haven't seen C++14/17, you should give it another look.

-3

u/dhdfdh Sep 15 '15

this language that more or less materialized out of thin air

That line, by itself, refutes your claimed background. That you now come to reddit to get an answer, and don't already know the answer, is more troubling.

0

u/[deleted] Sep 15 '15

Just FYI, I'm referring to this. https://www.w3.org/community/webed/wiki/A_Short_History_of_JavaScript

Not sure how that line would refute my claimed background.

1

u/dhdfdh Sep 15 '15

Something that has been ubiquitous for nearly 20 years did not appear "out of thin air". To know all those languages and need to come here to ask about Javscript is strange at the very least.

2

u/[deleted] Sep 15 '15

It's because I know all those languages that I am honestly requesting alternative opinions. Everything I know is telling me that it's a poorly designed language, and it seems (looking at ES6/7, typescript etc), that practitioners are aware of this too.

And I would classify a language invented by one guy in ten days as coming out of thin air. Unless you know many languages that were invented in a shorter time. Compare to languages like Haskell which took a long time to construct in an academic environment by committee.

2

u/jordaanm Sep 16 '15

Javascript v1 was designed and implemented by one dude in under a month. A lot of the quirks in the language today are carry-overs from that initial version. To say that the language "appeared out of thin air" isn't that much of a stretch

1

u/dhdfdh Sep 16 '15

Being a redditor, I'm sure this is difficult for you, but it's now 20 years later and 20 year old languages are not just now appearing out of thin air.

1

u/jordaanm Sep 16 '15

Neither I nor the OP suggested that JS is a young language. You seem to be interpreting "appeared out of thin air" as implying that JS is a recent phenomenon; my use of the term, which I believe is the same as the OP's, is mean as it went from nothing to something very suddenly. Which it did, even if that event happened many moons ago.

JS is quite obviously not the exact same language it was at inception, but any informed developer would be hard pressed to deny there are counter-intuitive quirks legacy to the language that are almost certainly due to its rushed initial implementation.

1

u/dhdfdh Sep 16 '15

any informed developer would be hard pressed to deny there are counter-intuitive quirks legacy to the language that are almost certainly due to its rushed initial implementation.

Which has nothing to do with my point but I'll let you drivel on.

0

u/jordaanm Sep 17 '15

Your point was that the language is 2 decades old, and didn't meet your criteria for "appearing out of thin air", my counter point is that we were interpreting that phrase differently, and the quoted point I made about legacy quirks is reasoning that justifies the alternative interpretation of "appear out of thin air" as being a legitimate complaint.

Separately, you are the only party in this conversation choosing to be rude/dismissive of others, so how about giving that a rest.

1

u/dhdfdh Sep 17 '15

I treat stupid comments as they deserve to be treated so take that as it is.