Destructuring in ECMA Script 6

In application programming its very common to come across cases where we get return values as objects or arrays from an API call and then for logic sake we need to store them into variables.so basically deconstruction steps require:

  • Declare appropriate variables
  • Access data from array or objects and assign to respective variables

Automatic Deconstruction feature in ES-6 helps in overcoming lot of this redundant coding stuff. As the name suggest we need to deconstruct a complex object say array or object into variables. Let’s compare the old way of deconstruction versus the new way

Old Style Deconstruction

<script type="text/javascript">

            function returnComplexObject(){
                return {id : 1 , name:"test"};
            }

            function returnArray(){
                return [1,2,3];
            }

            //Fetch Data
            var data = returnComplexObject();
            var id = data.id;
            var name = data.name;

            console.log(id);
            console.log(name);

            var arrayData = returnArray();
            var value1 = arrayData[0];
            var value2 = arrayData[1];
            var value3 = arrayData[2];
            console.log(value1);
            console.log(value2);
            console.log(value3);

        </script>


style="display:block; text-align:center;"
data-ad-format="fluid"
data-ad-layout="in-article"
data-ad-client="ca-pub-5021110436373536"
data-ad-slot="9215486331">

Continue reading

Default and Rest Parameters in ECMA Script 6

Another handy feature that has been existing in most advanced programming languages but added into JavaScript by ES-6 enhancements is the concept of default arguments.
We already discussed a lot about functions and arguments in previous article of this series here and we have already seen that you can pass any number of parameters to a function which might be accepting one , two or n number or parameters.
We already know that a function can have either named arguments which can hold the values passed to a function or arguments object containing all the arguments passed to a function.
Let’s try to handle default values for parameters in they are not passed explicitly by developer but required by program to run.

Continue reading

Constant Variables in ECMA Script 6

ES-6 introduced the support for immutable variables or constant variables. Constant variables are not a new concept in programming and have been in existence in most of the programming languages of today.
If while programming you come across a scenario where you don’t want somebody to change value of a variable then it makes sense to declare the variable as constant. You must understand that you cannot reassign a constant variable to a new value but in case of objects you can still be able to modify the property of objects.
Some rules accompanying a constant variable:
a.) Must be declared using const keyword
b.) Must be initialized at time of declaration
c.) Cannot change value of const once assigned
Let’s try to understand with examples

Continue reading

Arrow functions

Arrow functions provide lot of new enhancement apart from providing syntactic sugar to already existing JavaScript functions.
As you might already be aware, JavaScript is prototypical in nature and there are many ways you can add a function to a class. A JavaScript function itself can act like a class or a function. Here is how we can define a function

A normal function in JavaScript

Continue reading

Scoping in JavaScript – Var vs Let keyword

As a seasoned JavaScript developer, you might already be aware of nuances of var keyword. JavaScript has been a loose language unless used in strict mode.

  • You don’t need to declare a variable before using it, unlike other programming languages that provide better compile time support and syntactic sugar. Variables that are not declared are hoisted to global scope.
  • JavaScript Hoists the locally declared variable using var keyword inside a method i.e. no matter where you declare a variable inside your method its always hoisted to top of method and available to all code branches within the method.
  • You can declare global functions as well using var keyword by defining them outside a method.
  • Since var creates a function scoped variable it can cause unexpected results with loops.

Continue reading

Introduction to ECMA Script

Introduction to ECMA Script

As a web or mobile application developer you might already be aware of JavaScript. JavaScript has been a programmer’s favorite and lifesaver when it comes to changing Html on the fly since it runs directly on the browser. Now days, you are hearing a lot on JavaScript and corresponds frameworks that have popped up and wondering where to start with. JavaScript is evolving and is no longer object based language and you can see in new versions you can create classes, interfaces just like Java.

All the core features of JavaScript are defined in a standard widely known as ECMA-262.The ECMA-262 standards are defined in a language called ECMAScript. You should keep track of this language since most of the current programming languages are inheriting lot of features from ECMAScript. JavaScript and Node.js uses all the core features of ECMAScript and adds lot of other features on top of those feature to be as powerful a language.
The current version of ECMAScript at the time of writing the article is known as ES-2015 or ES-6. JavaScript as a language has seen lot of improvement over a course of ECMAScript upgrades. The biggest upgrade came in ES4.0 which apart from other smaller features includes the major ones like a new syntax, modularity, classes support, inheritance support, private access modifier etc.

style="display:block; text-align:center;"
data-ad-format="fluid"
data-ad-layout="in-article"
data-ad-client="ca-pub-5021110436373536"
data-ad-slot="9215486331">


There has been lot of improvements since then and if you want to learn any of the modern-day frameworks like Angular.Js, React.Js or Vue.js it’s a must to get started with learning ECMAScript.
In this article series, I will try to explain new features of ECMAScript and also will try to refer which features are adopted in which programming language.

Some of the features of ES-6 that we are going to discuss are:
• Scoped Variables (let vs var keyword)
• Arrow functions
• Constant Variables
• Default parameters
• Spread operators
• Rest Parameters
• Template Literals
• Destructuring
• Modules
• Classes
• Symbols
• Iterators
• Generators

Visual Studio Code – Getting Started with Python

Introduction

Python is one of the powerful languages of today and has gained wide acceptance as a tool to create machine learning applications due to its simple yet powerful syntax compared to other popular languages like C++, C#, Java etc.

Python was created in late 1980’s by Guido van Rossum and is developed under OSI-approved open source license by The Python Software Foundation.

Installation

Python can be downloaded for your choice of operating system from here

Continue reading

Visual Studio Code – Setup Python Environment variable

If python command is not working in visual studio code’s terminal window. You can follow below steps to setup PATH variable

 

Step 1:

Set up Path variable only for this visual studio code session. Run below command in visual studio code terminal where “C:\Program Files\Python36” is python install directory. You can choose a different one based on python version installed .

 

$env:path=“$env:Path; C:\Program Files\Python36″

  Continue reading

New Feature in C# 7.1 – Inferred tuple element names

From past few years I have been keeping a close watch on ECMA scripts and seems C# is running neck to neck in importing features of ES-6

I have explained about Tuples in older Blog article here

C# 7.1 introduces a small feature enhancement to Tuple de-structuring.

To run below sample you need to install System.ValueTuple Nuget package


style="display:block; text-align:center;"
data-ad-format="fluid"
data-ad-layout="in-article"
data-ad-client="ca-pub-5021110436373536"
data-ad-slot="9215486331">


Continue reading

New Feature in C# 7.1 – default Literal expressions

default value expression provides a default value for a type and are very handy in generator functions and generic classes/methods.

This is how we can use default keyword till C# 7.0 i.e. as default(T) where T can be value type or reference type

private void btnDefaultExpressions_Click(object sender, EventArgs e)

{

string str = default(string); //Old way of defining default strings

&nbsp;

var intValue = default(int);

&nbsp;

string stringValue = default(string);

&nbsp;

var nullableIntValue = default(int?);

}

Continue reading

New Feature in C# 7.1 – Async Main

“Main” is a special method while defining assemblies. It’s an entry point of an executable application. Main runs in static context by is called by operating system to load the program into memory. Main does so by defining a startup object or logic.

static class Program

{

        /// <summary>

        /// The main entry point for the application.

        /// </summary>

        static void Main(string[] args)

{

}

}

Continue reading

C# 7.1 New Features – Set up Visual Studio

In my past blog series, I have tried to explain all new features that shipped with C# 7.0. You can read about that here.

With Visual Studio Update 3 and version 15.3, C# 7.1 has been made available as well. This feature is not available by default and there are many ways you can turn this feature on. I will be writing a blog series on new features of C# 7.1 but before that lets understand how to set up support for 7.1 in visual studio.

There are 3 ways this feature can be enabled:

Way 1 – Changing Project Setting to target latest C# language

Continue reading

Asp.Net Core 2 – Runtime Store

Welcome to the series of asp.net core tutorials, in this part we will discuss another new feature that has shipped with asp.net core 2.0

Let’s first discuss 2 problems faced by monolithic applications:

  • All packages need to be deployed for application to run correctly and for different target environments there were challenges in picking the right version of package.
  • Since packages are not precompiled the application load time becomes slow

To overcome these issues Asp.net core 2.0 has introduced the concept of runtime store using which you can package and deploy applications against a known set of packages that already exist in target environments.

Continue reading

C# 7.0 New Features – Throw expressions

C# developers have always used throw statement to throw unhandled exceptions to caller. A reactive programming approach involved validating the variable values and if it doesn’t fall within a valid range or is null throw exception as demonstrated in below example

//Method that fetches data from server 
object Getdata()
        {
            //Fetch data from server or database and if data is null return null
            return null;//
        }

        private void btnThrowExpressionAlternateC6_Click(object sender, EventArgs e)
        {
            var data = Getdata();
            if (data == null)
                throw new Exception("No Data found");

        }

Continue reading

C# 7.0 New Features – Pattern Matching

Pattern matching is a feature that allows you to implement method dispatch on properties other than the type of an object. Pattern matching is very useful when we want to create branching logic based on arbitrary types and values of the members of those types.

Object dispatch is a very common feature of object oriented programming wherein we identify objects based on their type and call appropriate method. Based on resolved type the method gets dispatched on appropriate type i.e. child or base. This feature is automatically implemented if we use concept of object oriented programming like Hiding and Overriding.

Problem occurs when there are multiple classes that our code uses and the classes are not related to each other. In that case the branching logic requires below steps to correctly dispatch method call to object:

  • Identify type of object using various branching logic
  • Cast to appropriate type if needed
  • Call method on object

Example in C# 6.0:

Continue reading

C# 7.0 New Features – Tuples Enhancements

Many a times we would like to return more than one values from a method call. C# provided lot of features to support this functionality. Some of them are:

  • Out Parameter
  • Ref Parameter
  • Anonymous types can be returned via dynamic keywords
  • Tuples

C# 4.0 introduced a type Tuple<…> using which you can return more than one value from a function without creating a new type. Tuple behaves like a dictionary with each returned item accessible as Item1, Item2 and so on. This is very verbose and require a Tuple object to be created.

Example using C# 4.0

  Tuple<int, int> ReturnRuntimeTimeEncapsulatingTwoValues()
  {
      return new Tuple<int, int>(1, 2);
  }

  private void btnTuple6_Click(object sender, EventArgs e)
  {
     Tuple<int, int> tuple = ReturnRuntimeTimeEncapsulatingTwoValues();
     //Usage in c# 6
     int value1 = tuple.Item1; //No control over member name and its too unintuitive
     int value2 = tuple.Item2;
  }

Continue reading

C# 7.0 New Features – Local functions

Local functions are another very handy feature to be introduced into language if you are into code refactoring. Many a times developers create a very large method and while code review we see lot of places same logic has been repeated and thus think about refactoring it. Problem is that if the logic is required only at multiple places within same method than it’s not a good idea to make it into a separate named method.

Let’s take an example in C# 6 and then we will try to refactor that using various ways and finally with local functions feature in C# 7.0

Here is a simple code that works on array of numbers and calculates sum and subtraction of each number in array

style="display:block; text-align:center;"
data-ad-format="fluid"
data-ad-layout="in-article"
data-ad-client="ca-pub-5021110436373536"
data-ad-slot="9215486331">



Continue reading