Tuesday, March 22, 2016

ACID

Tuesday, March 22nd 2016

ACID
Acid is an acronym that stands for a set of properties that guarantee that database transactions are processed reliably; to measure reliability, engineers look at atomicity, consistency, isolation and durability.


  • To illustrate these points, let's consider bank transfers.
  • Atomicity: 
    • An atomic system must guarantee that the entire transaction happens or the entire transaction doesn't happen. That is, if only a part of the transaction happens, it's in some cases worse than if the transaction didn't happen at all.  
      • EG: When you transfer money from your checking to your credit card, it would be a problem if your checking was deduced the total amount but your credit card was not debited the total amount. Then, it would look like you simply lost money. This is an atomicity error, as only part of the transaction occurred
  • Consistency:
    • While significant ambiguity exists about this principle, it generally means that the database store similar transactions in the same manner without issues. In addition, it also means that the constraints of the database itself cannot be violated once a transaction commits. 
      • EG: After making a successful transfer of money from your checking account to your credit card, you attempt another transfer of a similar nature and it fails. That, or the database incorrectly refers to the initial balance in your account before the previous transaction. This is a consistency error, as transactions of similar nature should have no issues.
  • Isolation:
    • Isolation refers to how and when a transaction becomes visible to other users and systems (transaction schedule). A lower isolation level increases the ability of many users to access data at the same time, but increases the number of concurrency effects users might encounter. Conversely, a higher isolation level reduces the types of concurrency effects that users may encounter but increases the chance that one transaction will block another.
      • EG: You decide to buy a car unbenounced to your spouse and, coincidentally, your spouse decides to do the same unbenounced to you. You're both using the same bank account. Let's say your spouse pays for the car a couple of minutes before you purchase yours. When you go to purchase yours, you get an error "not enough funds", so you go to your online bank account and see that all of the funds are indeed available. This is an error of isolation as your spouse's transaction is not yet viewable to you.
  • Durability:
    • Durability is one of the more straightforward properties, outlining the need for redundancy and durability of the system to retain information even in the event of a power outage. Storing data permanently helps prevent against data loss in the event of crashes, systematic errors etc. 
      • EG: The bank's database crashes and loses all records of your account funds, thereby leading you to lose however much money you had in their system with no record to refer to. 
References
http://stackoverflow.com/questions/3740280/acid-and-database-transactions
http://searchsqlserver.techtarget.com/definition/ACID
http://www.dbrnd.com/2015/05/acid-properties-in-database-system/

Strong/Static vs Weak/Dynamic Typing

Monday, March 21st 2016

Static/Strong vs Dynamic/Weak Typing

Static/strong and dynamic/weak are both gauges applied to how forceful a programming language is in enforcing a set of pre-specified of rules. Both strong and weak typed languages have their purpose. Software engineers utilize strongly-typed languages to avoid bugs and errors on run time, while also communicating purpose clearly (what type a value is expected to be). On the other hand, software engineers also utilize weakly-typed languages provides more flexibility to engineers to mess with variable type (guided by best practices), allowing them to be more creative in their code construction.



Specifically, static/dynamic typing is concerned about when type information is required (compile or runtime) whereas strong/weak typing is concerned about how strictly types are distinguished (though its definition is still the subject of scholarly debate). As such, try to avoid describing programming languages as either strong or weak and, instead, focus on describing them as static or dynamic. If a language pops up an error at runtime because you failed to specify the type of the variable, it's probably static and if it doesn't, it's probably dynamic.


References
http://blogs.agilefaqs.com/
https://en.wikipedia.org/wiki/Strong_and_weak_typing
http://stackoverflow.com/questions/2351190/static-dynamic-vs-strong-weak


Thursday, March 17, 2016

Promises (ES6)

Thursday, March 17th 2016

What is a promise? 
Simply put, a promise is used in lieu of a value to allow code to be run asynchronously. Promises are important because they address an important issue and without them, websites and apps would take noticeably longer to run. The issue is that every time you make a request to a server, you have to wait for that server to respond, which can sometimes be slow (~1-2s). Meanwhile, there is all this other code to run that's all just waiting for this server response. So, instead, we can create a promise that refers to the value that we receive from the server some point later in the code so all of the other code can run. As a result, promises allow us to run code asynchronously thereby making our websites and apps much faster. Below is a flow chart explaining the process that promises go through.



So we start out with creating a promise (there are multiple modules for promises in es5, but thankfully promises are native to es6). Once we have encapsulated what code we want to run within the promise, we can then run code asynchronously while waiting for that promise to be settled (either fulfilled (success) or rejected (error)). Once it's settled, the promise is returned and the code moves on. (An important note, returning the promise typically refers to the value returned, not repeating another Promise Constructor).

References:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
http://stackoverflow.com/questions/22539815/arent-promises-just-callbacks
http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call?lq=1

Monday, March 14, 2016

Binary Search Tree

Thursday, January 14th 2016
  • Binary Search Tree
    • Storing numerical values (important! numerical values can be compared, which is the crux of the mechanic of this data structure!) in relation to one another in a, hopefully, balanced tree. While the time complexity for this isn't constant, it's not linear either. It's logarithmic. An increase in the number of inputs only slightly increases the time for processing. We shift through values fairly quickly, simply by sorting and nesting them within a series of true/false values. Particularly, is this value larger or small than the value your looking for? 
      • [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
      • v: 4 |||||||| 
        • is v = 11? false. v < 11? true--> 
          • value=5? false. v<5? true ->
            • v=3? false. v<3? false ->
              • v=4? true!
      • took 4 tries. If we double'd this array, we may add 1 or 2 steps, despite the 2 * n increase in n. While n increases, our time complexity only increases at a marginal rate. 4 - > 5 or 4 -> 6.


References:
 
http://bigocheatsheet.com/
http://algs4.cs.princeton.edu/32bst/

REST

Monday, March 14th 2016

What are REST, RESTful programming, and a REST API? Well, they all refer, mostly, to the same thing. First and foremost, REST (Representational State Transfer) is a software design for most web services (definitely the most popular one) and helps simplify machine to machine communication over a network. But what kind of design is it?


In the diagram, we have many different devices (with different browsers and OS's) all making requests and receiving responses from some database, using REST. The problem that REST solves is complexity so that instead of having all of these different protocols on many different devices, all you have to do is use HTTP protocol to perform all CRUD (Create, Read, Update, Delete) operations. In this way, web pages and applications are navigated to and through using HTTP protocol to communicate between devices. (In the case of the diagram above, the android phone, iphone and mac all use HTTP to access information in the database).

TL;DR: Rest allows machines and humans to interact with websites much easier because it standardizes the action of doing things to data. A noun, similarly, is an object we talk about and we can GET it and we can PUT it somewhere. Same thing with a car. Same thing with a chair. The power of REST is that we don't need a different set of operations that are noun-dependent. Its a universally recognized way of performing actions on objects.

References:
http://stackoverflow.com/questions/671118/what-exactly-is-restful-programming
http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm
http://web.archive.org/web/20130116005443/http://tomayko.com/writings/rest-to-my-wife
https://app.box.com/s/l8fn4eyt9payhe4a64bt3drmwdvz7usw

Friday, March 11, 2016

Deep vs Shallow Copy

Friday, March 11th 2016

In programming, there are generally two ways to copy things. You can create a shallow copy of data  and you can create a deep copy of data. Typically, a shallow copy of data makes a reference to where the original data is in memory, whereas a deep copy copies the original data in full and creates memory allocation for that copy, specifically.




Actual examples of each in JS:
//shallow copy                                                          //deep copy
var arr = [1,2,3];                                                       var arr = [1,2,3];
var arr2 = arr;                                                            var arr2 = [];
console.log(arr2); //[1,2,3]                                        for(var i = 0; i < arr.length; i++) {
arr[0]++;                                                                          arr2[i] = arr[i];
console.log(arr2); //[1,2,3]                                        }
                                                                                  console.log(arr2); //[1,2,3];
                                                                                  arr[0]++;
                                                                                  console.log(arr2); //[1,2,3];

In summary, a shallow copy is a reference to the memory of the original whereas a deep creates new memory for a copy of the original. Most related bugs aren't actually a result of shallow/deep copies, but rather confusion regarding reference by type vs reference by value.

References
http://stackoverflow.com/questions/3947227/deep-copy-of-an-object-array
http://geniuscarrier.com/copy-object-in-javascript/
http://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy

Transpilers

Friday, March 11th 2016

Transpilers, transcompilers or source-to-source compilers are types of compilers that translate code from one programming language into equivalent code in another programming language. You know how traditional compilers will compile code from higher level programming languages and compile them down to lower level languages for the CPU to process? Well, similar process here, except instead of going from higher to lower, transpilers typically stay on the same level of abstraction. Some examples of transpilers include Babel, CoffeeScript and TypeScript.

So what are transpilers used for? Many things. Transpilers, such as TypeScript, allow developers to have more choices on how to write their code. For example:

function greeter(person: string) {        evaluates to JS as:      function greeter(person) {
    return "Hello, " + person;                                                       return "Hello, " + person;
}                                                                                               }

The expression inside of the argument for that function evaluates, in typescript, to making sure that the argument passed in will be a string and, if not, throws an error.

In summary, transpilers allow developers to use more tools to help write their code using other languages which are on the same level of abstraction (hence 'trans').

So, what is Homebrew?

Monday, March 11th 2016

Homebrew is a package management software that allows Apple OS X users to install software on their operating system. Homebrew is a free, open source library of essential tools that any developer can use to download important software packages, such as NPM or Node.js. Regardless, most OS X developers use Homebrew to locally install vital software packages. In short, think of brew as the 'npm' or 'bower' of your mac.

References
http://brew.sh/
https://web.archive.org/web/20150708101237/https://blog.engineyard.com/2010/homebrew-os-xs-missing-package-manager/

== vs. ===

Friday, March 11th 2016

What's the main difference between == and ===? The == is a special boolean operator that tells the compiler to do some type coercion under the hood and then, after the type coercion, check to see if its still the same value. But often, and by often I mean almost always, this behavior is not predictable and gives mixed results. Examples:

'' == '0'           // false
0 == ''             // true
0 == '0'            // true

false == 'false'    // false
false == '0'        // true

false == undefined  // false
false == null       // false
null == undefined   // true

' \t\r\n ' == 0     // true
Make sense? The answer is no, not really. This makes little sense unless you've spent the time to study case-by-case how the compiler will type coerce and evaluate these values. I highly recommend not wasting your time and, instead, simply using the ===.

The === does no type coercion. It simply evaluates the types of the values and then the values themselves. It will always tell you whether any two values are exactly the same.

Regardless, always use === or your going to have a bad time.

Is this concept misconstrued? Often!
== is the same as === when the values you're evaluating are of the same type, but are NOT the same when they are of different types. (They are also the same for reference checks, but this is not always the case). Also, the performance of  === is faster than == because no type coercion is done if the two values are not of the same type.

References
http://www.c-point.com/javascript_tutorial/jsgrpComparison.htm
http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3

Monday, March 7, 2016

Event Delegation, Propogation, Capturing and Bubbling

Monday, March 7th, 2016

Event delegation is the HTML mechanism that allows developers to setup event handlers for nodes, which are fired whenever a child of that node, through bubbling, triggers that event handler. In other words, children inherit the event handlers of their parents and, when the child is selected, fire each one of it's parents event handlers.


Example:
<ul onclick = 'alert('yo!')'>
      <li> One </li>
      <li> Two </li>
      <li> Three </li>
</ul>
- When any of children of the <ul> element are clicked on, the event handler 'onclick' fires, despite 'onclick' being assigned to the <ul> element and not any of the <li> elements.

So why use bubbling? Bubbling is useful to make code more readable and easier to manipulate. Instead of having to assign each one the <li> elements to the 'onclick' event handler, we instead can simply assign the parent element the event handler which will then fire when any of the children are selected.

Misconstrued: Sometimes
Often, event bubbling is synonymous with event propagation but they are different. Event propagation encompasses capturing and bubbling, though due to the deprecation of event capturing, event bubbling and propagation have inadvertently become synonymous.

Sources:
http://www.javascripter.net/faq/eventbubbling.htm
https://learn.jquery.com/events/event-delegation/
http://stackoverflow.com/questions/4616694/what-is-event-bubbling-and-capturing
http://stackoverflow.com/questions/1687296/what-is-dom-event-delegation

Tuesday, March 1, 2016

Web Sockets

Tuesday, March 1st 2016

WebSockets

So what are web sockets? WebSockets are a faster way to communicate smaller bits of information between the client and server. Generally, this results in faster performance for applications or browsers that require faster communication with smaller bits of information. What are WebSockets specifically? A WebSocket is an HTTP extension that when enabled will only require the initial 'handshake' between the server and the client to happen once, and from that point on the exchange of information, typically for the server, can happen continuously.

So where does the performance boost come from? The performance boost, despite some common misconceptions, does not come from the size of the information being sent, but rather the parsing of the logic contained with the message. This latency interferes with usability and performance. Websockets, however, have very low overhead (a header, mask and data) and are able to be parsed very quickly.

The takeaway is that one is not necessarily better than the other because they were both designed to serve different purposes. On the one hand, WebSockets were designed for bi-directional communication between a client and server whereas HTTP was designed for a wide range of different uses, requiring a more structured and compartmentalized req/res system.

References:
http://stackoverflow.com/questions/14703627/websockets-protocol-vs-http
http://blog.arungupta.me/rest-vs-websocket-comparison-benchmarks/
http://stackoverflow.com/questions/27376505/why-do-we-still-use-http-instead-of-websockets-for-building-web-applications?lq=1

NPM vs. Bower

Monday, February 29th 2016

NPM vs. Bower

Let's start with the basics. What are NPM and Bower? They are both package managers for Javascript. NPM is for Node.js and Bower is everything else, specifically the front end (so javascript only).

The main difference between Bower and NPM is their dependency structure; NPM uses a nested dependency tree whereas Bower uses a flat tree. The distinction here is defining: because bower uses a flat tree that is easier to traverse through, retrieval of dependencies is faster, which is why it is often used on the front end, which is influenced by the performance of fast retrieval more so than the backend. However, the downside to this is that if a dependency relies on a specific version of another dependency, it may not work or break. It's for this reason that most people do not use many dependencies through bower and only stick to dependencies that are known to work together. For example, angular and angular-ui. The key point here is that bower does not like to download redundant dependencies, even if they are different versions, to boost the performance of the page load and reduce latency.

On the other hand, NPM resolves this problem by downloading any versions of dependencies that other dependencies rely on. (NPM solves the issue of dependency hell). This is why it is a nested tree instead of a flat tree: NPM will download the same dependencies more than once to ensure that all dependencies are compatible and functional. However, the downside to this is a hit to the performance, but in regards to the user experience, it is not noticeable because it is in the backend.

References:
http://stackoverflow.com/questions/18641899/what-is-the-difference-between-bower-and-npm
http://maxogden.com/nested-dependencies.html