JavaScript, why are you like this?

JavaScript have some weird quirks. And one of them is how pointer “this” works.

look at the code below:

var c = {
name: "this is c object",
log : function(){
this.name = "updated c object";
var setName = function(newname){
this.name = newname;
}
setName("Updating again! The c object");
console.log(this);
}
}

Look at the line 6, you might think that name has been set to value “Updating again! The c object”, because this is pointing to c object. BUT, if you run the code, you will find out that actually line 6, is creating new Global variable name, and setting it’s value to “Updating again! The c object”. This happens because of how the execution context is created in JavaScript. In JS, the interpreter will hoist all the variables and methods first. Meaning before real execution line 6 is interpreted and variable is set to global variable(sort of being pushed all the way up the code).

The solution would be to store the reference to c object then pass it.

 var c = {
name: "this is c object",
log : function(){
var self = this;
// note some developers use "that" instead of self
self.name = "updated c object";
var setName = function(newname){
self.name = newname;
}
setName("Updating again! The c object");
console.log(self);
}
} 

This way, we will not set the name to be global variable and it will be set to c object. So c.name will return “Updating again! The c object”

Advertisements

String … and Generating Javadocs

In java we can pass some arguments in methods. Sometimes we want to pass the array of objects. So if you see writeText(String … text) is actually nothing but array of Strings. It’s same as writeText(String[] text).

To easily Generate javadocs in Intelij Idea you can use this shorcut.

type /** then press enter. This applies for the methods. Later you can add more details to that javadoc

ex.:

/**

public String addText(String[] text){

return ‘abc’;}

becomes

/**
 *
 * @param text
 * @return 
 */

public String addText(String[] text){

return ‘abc’;}

Java Streams

Since java 8, we have access to java streams api. It works similar to java collections(List, Set, Queue).

The Java 8 Streams can be seen as lazily constructed Collections, where the values are computed when user demands for it. Actual Collections behave absolutely opposite to it and they are set of eagerly computed values (no matter if the user demands for a particular value or not).

Just like functional programming languages, Streams support Aggregate Operations. The common aggregate operations are filter, map, reduce, find, match, sort. These operations can be executed in series or in parallel.

The Streams also support Pipelining and Internal Iterations. The Java 8 Streams are designed in such a way that most of its stream operations returns Streams only. This help us creating chain of various stream operations. This is called as pipelining. The pipelined operations looks similar to a sql query. ex. a.b().c()

Source: https://dzone.com/articles/understanding-java-8-streams-1

GraphQL

I’ve started learning GraphQL and decided to share some information that I find useful.

GraphQL is a query language to retrieve data from the server.

Unlike REST queries, GraphQL is “smart”. Main benefit is that, instead of many REST nodes, we only have one smart node, that can provide us with the exact information we need.

Basic GraphQL queries

People commonly call everything that hits your GraphQL API server a “query”. But there are a lot of things mixed in there. What do we call a unit of work we’re asking the server to do? It could be a query, a mutation, or a subscription. 

GraphQL Operation.JPG

Selection sets

GraphQL Selection Set.JPG

  • Selection set: A set of fields requested in an operation, or nested within another field. A GraphQL query must contain a selection set on any field that returns an object type, and selection sets are not allowed on fields that return scalar types, such as Int or String.

Source: https://dev-blog.apollodata.com/the-anatomy-of-a-graphql-query-6dffa9e9e747

SCHEMA

[String!] vs [String]!

myField: [String!]

This means that the list itself can be null, but it can’t have any null members. For example, in JSON:

myField: null // valid
myField: [] // valid
myField: ['a', 'b'] // valid
myField: ['a', null, 'b'] // error

Now, let’s say we defined a Non-Null List of Strings:

myField: [String]!

This means that the list itself cannot be null, but it can contain null values:

myField: null // error
myField: [] // valid
myField: ['a', 'b'] // valid
myField: ['a', null, 'b'] // valid

Source: http://graphql.org/learn/schema/