Saturday, May 14, 2016

Why Node?

Friday, May 13 2016

Why chose to develop in Node? What are the pros and the cons? What was it designed for?

Node was designed for web application development in mind, specifically aiming to process non-intensive CPU process asynchronously and fast. Knowing when to use Node and when not to use Node depends on context; to make this easier, below is a list of pros and cons to using Node.

Pro's:

  • One language: 
    • Node is written in Javascript which means that, assuming your already using javascript for your frontend, you would only ever have one language to work with. That makes it easier for developers to write more code and focus on logic rather than language syntax. This is especially true for companies that maybe using JS, PHP, C or a mix of multiple languages. 
  • Its fast.
    • V8: The V8 engine developed and maintained by Google compiles javascript, using C++, into machine code, which makes it incredibly fast. Also,
    • The Event Loop: As I've written in a previous post, the event loop is what provides javascript is awesome power in conjunction with the V8 engine. The event loop is a single thread that performs I/O operations asynchronously by handing async operations to the event loop with a callback so it can run the rest of the code. Once the async operation completes, the callback is called.
    • Network connections, filesystems, and database queries run very quickly in javascript due to the event loop and v8 compilation.
  • NPM
    • NPM is the largest package manager on the web and is entirely open-sourced. With a large, thriving community supporting it, the reliability of node's package manager is a sure thing for many years to come. 
    • Additionally, many of these modules are made in a plug-and-play sort of manner and are incredibly performant - one of the many benefits of having the support of a large online community for an open sourced platform. (EG: bluebird, socket.io, etc)
Con's:

  • Its based on JavaScript
    • This one isn't so much to do with Node as to what language its built for. Javascript itself is not as opinionated as other languages, which can be a good thing and can be a bad thing. If your looking to create a safety-critical system that would be disastrous if there were any bugs, then yeah, maybe look for a more opinionated language, like TypeScript, with more built-in testing interfaces. 
  • CPU heavy requests
    • Because Node is single-threaded, CPU intensive requests are not as ideal (unless a significant portion of the request can be run asynchronously, but this is not always the case). 
  • The number of modules
    • NPM is flooded with many different modules, which can be a good thing and, again, a bad thing. The bad thing about having many different modules is that you can have 2 separate apps that do the same exact thing but built very differently with different modules. This can make it difficult to find one answer to one problem with one type of module. However, this is minor since there are market-recommended modules that are more used than others, thereby consolidating a good portion of FAQ that are easy to lookup.


References:
http://blog.modulus.io/top-10-reasons-to-use-node
http://stackoverflow.com/questions/5062614/how-to-decide-when-to-use-node-js?rq=1

Tuesday, May 10, 2016

Generic Programming

Tuesday, May 10th 2016

What is Generic Programming? Generic Programming is a style of writing code whereby code functions are written input type agnostic. That is, regardless of the type of input (array, obj, etc), the function is able to process that information all the same and return a predictable output. A perfect example of a function that was written with generic programming principles is the forEach function. forEach detect what the type of the input was (array/object based) and then iterates through the list accordingly.


Generic programming helps us abstract towards the purpose of the function. Rather than having to create several different functions (all with the same purpose but fit for different input types), we can abide by DRY by simply using one function.

References
http://www.generic-programming.org/
http://www.boost.org/community/generic_programming.html
http://stackoverflow.com/questions/3862378/what-is-the-meaning-of-generic-programming-in-c

Monday, May 9, 2016

Functional Polymorphism within Javascript

Monday, May 9th 2016

What is functional polymorphism within Javascript? Well, in Object-oriented programming, polymorphism describes how a language is able to process inputs differently depending on their data type (str, arrays, etc). It also outlines the ability to redefine preexisting methods for derived classes. Consider the image below:



The draw() method is attached to the class 'shape'. Polymorphism allows us to derive the draw() method from the shape class and apply it to subclasses (or different data types), in this case: triangles, rectangles and circles because they are all shapes.

In JavaScript, we can use functional polymorphism to delegate functions onto data of different types. For example, arguments is not a real array and, therefore, doesn't have access to the .sort() method on the Array class. However, we can manipulate the code to get the function of sort() onto arguments, despite it not being an Array by doing something similar to the following:


Polymorphism is an important and cool tool in JS that allows us to stay DRY (method delegation) and KISS (borrowing predictable methods from other class types). 

References
Programming JavaScript Applications (O'Reilly)
http://raganwald.com/2014/04/10/mixins-forwarding-delegation.html