Tuesday, November 20, 2018

Concurrency VS Parallelism.

Actual Parallelism VS feel of Parallelism

Why a feel of parallelism and why not actual?

Concurrency is about design, parallelism is about hardware’s


Further readings

Actual Parallelism VS feel of Parallelism

Technical vocabulary in IT industry are sometimes very confusing and “Concurrency” and “Parallelism” is one of them. Many developers think “Concurrency and parallelism means executing at the same time” which is right 50% but with one big difference:-

  • Concurrency gives you a feel of parallelism and parallelism as the name implies is actual parallelism.

Feel of parallelism means you execute multiple tasks on the same core and the core switches context between tasks and serves them. You can also term this has time slicing / over lapping time period because your single core is just dedicating some time to one task and then some time to other.

Actual parallelism means you execute multiple task on multiple cores parallely.

Note: - “Concurrency is a broader term and Parallelism is subset of it”.

Mapping to the real world the left image depicts parallelism the right image depicts concurrency.

Why a feel of parallelism and why not actual?

In order to achieve actual parallelism we need dedicated cores, separate memory and so on.

Let’s say we want to show a progress bar for some task completed. Now we really do not want to have separate cores allocated to display the progress.

We do not want PERFORMANCE here we want that physiologically the end user feels both tasks are happening simultaneously.

We want to just beat the human eye capability of 100 FPS and give an illusion of parallelism without stressing our computer resources. But let’s say we want to process big excel files with million records then yes we would love to have actual parallelism to achieve performance.

Concurrency is about design, parallelism is about hardware’s

In order to achieve concurrency we need to compose our application logic independently. For instance let’s say you want to process employee data where you want to increment the salary by x% and bonus by x%.

So you can decompose the application in to logical units by following different designs:-

Design 1

  • Divide data in to 50% size each.
  • Process each 50% as separate unit.

Design 2

  • Process bonus calculation as separate unit.
  • Process salary calculation as separate unit.

Design 3

  • Divide data in to 50% size each.
  • For every 50% data process bonus calculation separately and salary calculation separately.

There can be many such designs and combination. So when you say your application is supporting concurrency your application should be composed in to small independent units.

Now you take these units and run on one core (Concurrency) or you run on multiple cores (Parallelism). So concurrency is about design while on parallelism we talk more from hardware perspective, 2 core, 3 cores and so on.

If you try to run every concurrent code as parallel you have resource starvation unnecessarily. So ask yourself do you want an illusion (concurrent) or do you performance (parallel).


Concurrency Parallelism
Basic definition Executing multiple task on same core using overlapping or time slicing. Executing multiple tasks on different core.
Goal Feeling of parallelism without stressing out resources. Actual parallelism for performance.
Perspective Software design: - Composition of independently executing computations in a co-operative fashion. Hardware: - Executing computation parallel.
Resource utilization Light Heavy
  • Parallelism is a subset of concurrency.
  • Concurrency enables parallelism.
  • Concurrency is more about software design while parallelism is more about hardware’s.
  • Concurrency gives an illusion of parallelism while parallelism is about performance.
  • Concurrency just needs one core while parallelism needs at least 2 cores.

Further readings

Concurrency is not parallelism by Rob pike https://www.youtube.com/watch?v=cN_DpYBzKso

Nice discussion on concurrency and parallelism


Monday, June 11, 2018

bin vs obj folders in C#.

When we compile C# programs you would see 2 folders “bin” and “obj”. In this article we will try to understand the difference and importance of these folders.

Both these folders have compiled IL code, but the question comes is why not just one folder and why two folders?.

We have two folders because the compilation process goes through two steps compiling and linking. See the below diagram.

  • In compiling phase every code file is compiled in to individual compiled units. So if you have two code files two independent compiled codes will be generated.
  • In linking phase all these compiled code files are linked and compiled in to single unit assembly which can be a DLL or EXE.

If you compare both the folders ( see below image) you will find more files in “obj” folder as compared to “bin” folder. We have more files in “obj” folder because it creates seperate compiled code files for each source code file.

So the next question which comes to our mind why do we need compiling in two phase, why not just do it one go. By doing the two phase compiling we achieve incremental or conditional compiling.

When we work with big projects we will have lot of code files and we would like to only compile those code files which have changed. In the “obj” folder we have entry of each code file compilation. So we can know from the same which files exactly have changed , thus making compiling fast.

In summary in “obj” folder have compiled files for each source code file and in “bin” folder we have a single unit which links all individually compiled code files.

Below is 10 minutes youtube video which demonstrates how these both folders look like and how incremental compilation happens.

Friday, November 3, 2017

What is the best project folder structure for Angular projects?

There no straight answer to this as every project is different, developers are of different mindset and architecture have their own though process.

But whatever it is you will end up with some kind of nearby folder structure for angular as shown below:-

  • You will need two root folders one for the server code and the other for client code. The client code folder sometimes is also named as “app”.
  • If you have a big project then inside in the client folder you can create sub folder which represent modules of your project. Normally developers divide project in modules for better management so these subfolders represent those modules.
  • In this those module folder you can have separate folder for component, model, module and routing.
  • A common folder is also needed where in you can push your common utilities like common pipes, filters, http components, injectables and so on.
  • Server folder will have its own folder structure depending on whether you are doing ASP.NET or JSP or PHP. In this discussion we will restrict only to client side angular folder structure.

This question was raised in our Mumbai angular training of 86th batch http://stepbystepschools.net/

Monday, August 21, 2017

What is package-lock.json file in Node NPM? (Learn Angular Step by Step)

When you are doing development in Angular, Node NPM is your tool for package management. In simple words we have a “package.json” file and all dependencies are listed inside it. When you are doing NPM you will always find “package-lock.json” file.

So in this Angular tutorial we will unleash the importance of this lock file.

To understand the importance of lock lets understand how software versioning works.

Most software versions follow semantic versioning. In semantic versioning, versions are divided in to three distinct numbers as shown in the image below.

The first number is termed as “major version” , second “minor version” and third “revision”.

Major version: - Any increment in major version is an indication that there are breaking changes in the software functionality. It’s very much possible that the old code will not work with these changes and have to be tested properly.

Minor version: - This version is incremented when we add new features but the old code still works.

Revision:- This version is incremented when we are just doing bug fixes. So there are no new functionalities added, no breaking changes and back ward compatible with old code .

NPM follows semantic versioning but it also has some more special characters like “^”, “~”, “>” and so on. They dictate how NPM get latest should behave for Major and Minor versions.

For these formats 3 formats are very primary let’s understand each them.

Exact (1.6.5) , Major/Minor ( ^1.6.5) or Minor(~1.6.5).

Exact (1.6.5): - This will do a get latest of exact version 1.6.5 not more or not less. If that version is not available it will throw up an exception.

Major/Minor(^1.6.5): - The carrot sign will get minimum 1.6.5 and if there are any higher MINOR / REVISION versions it will get that. It WILL NEVER GET HIGHER MAJOR VERSIONS. So if 1.6.5 has 1.6.7 it will get that, if it has 1.7.7 it will that , but if it as 2.0 it will NOT get that.

Minimum or lower (~1.6.5): - The tilde sign will get HIGHER REVISIONS. For if 1.6.5 has 1.6.7 it will get that , but if it has 1.7.5 it will not be installed , if it has 2.0 it will not be installed.

As discussed in the previous sections package.json has “^” and “~” versioning mechanism. Now suppose in your package.json you have mentioned "jquery": "^3.1.0"and Jquery has a new version “3.2.1”. So in actual it will install or in other words LOCK DOWN to “3.2.1”.

So in package.json you will have “^3.1.0” but actually you will be using “3.2.1”. This entry of actual version is present in “package-lock.json”. So package lock files have the EXACT versions which are used in your code.

Below is the image snapshot of both the files.

Do not miss our Learn Angular Step by Step in 8 hours video training series: -


You can also catch me teaching Angular in Mumbai at Andheri at http://stepbystepschools.net/?page_id=622.

Monday, July 11, 2016

== VS Equals in C#



Point 1 :- Comparison on the basis of Equality

  • Scenario 1:- Value type comparison
  • Scenario 2:- Reference types comparison
  • Scenario 3:- String comparison, interning and object type casting
  • Point 2 :- Compile time VS RunTime

    Point 3 :- The NULL Situation

    When to use what :- Technical comparison VS Semantical comparison

    The Full Picture


    To compare equality between variables C# has provided two ways of doing comparison “==” and an overloaded method “equals()”. Most of the developers use “==” and “Equals” is hardly discussed.

    So in this small note we will discuss about differences between them and when to use what.

    Point 1 :- Comparison on the basis of Equality

    Answering to the point “There is no difference between equality comparison using “==” and “Equals()”, except when you are comparing “String” comparison.

    The common comparison Rule :-Whenever youare comparing variables they are either value types or reference types. When values types are compared they are compared on the basis of “Content” when reference types are compared they are compared on the basis of “Reference”(memory location) and not “Content”.

    The above rule is respected by both “==” and “Equals”.

    Scenario 1:- Value type comparison

    When you compare value types / primitive data types ( int , double etc) either by using “==” or “Equals” it’s always based on content. In the below code you can see both comparison methods will show as “true”.

    int i = 10;
    int y = 10;
    Console.WriteLine(i == y); // true
    Console.WriteLine(i.Equals(y)); // true

    Scenario 2:- Reference types comparison

    Now when you compare objects they are compared on the basis of reference (internal memory pointer). Below obj and obj1 comparison either through “==” or “Equals” will be false. So in the below code even though both the object have property name as “Shiv” still it shows unequal. Because the comparison is based on internal memory reference which is different for “obj” and “obj1”.

    Customerobj = newCustomer();
    obj.Name = "Shiv";
    Customer obj1 = newCustomer();
    obj1.Name = "Shiv";
    Console.WriteLine(obj == obj1); // false
    Console.WriteLine(obj.Equals(obj1)); // false

    But the below code will display true as the pointer points to same object.

    Customerobj = newCustomer();
    obj.Name = "Shiv";
    Customer obj1 = obj;
    Console.WriteLine(obj == obj1); // true
    Console.WriteLine(obj.Equals(obj1)); // true

    Scenario 3:- String comparison, interning and object type casting

    Now strings are immutable objects or reference types so they should be checked using the rules of reference types. In other words in the below scenario when we assign value to “str” it creates a string object and in heap has “test” stored. When you now assign “str1” this a different object so it should be a different instance.

    But look at the value, it the same. So C# string follows interning rule. In other words if the content is same “str” and “str1” they point to the same memory location and data. So both “==” and “Equals” will be true.

    objectstr = "test";
    object str1 = "test";

    But now look at the below code where we are explicitly creating new separate objects of string with same value. We are forcing and overriding interning behavior of string.In the below code “==” will return false even though the content is same while “Equals” will return true. This is one place where the equality behavior differs.

    objectstr = newstring(newchar[] { 't', 'e', 's', 't' });
    object str1 = newstring(newchar[] { 't', 'e', 's', 't' });
    Console.WriteLine(str==str1); // false
    Console.WriteLine(str.Equals(str1));  // true

    Point 2 :- Compile time VS RunTime

    The next point which makes them different is when do type checks happen. “==” does type checking during compile time while “Equals” is more during runtime. You can see in the below code how “==” is showing a warning message with green sign saying that you are comparing different types and you can have issues. “Equals” does not show any such warnings.

    Point 3 :- The NULL Situation

    “==” works with nulls but “Equals” crashes when you compare NULL values , see the below print screen.

    When to use what :- Technical comparison VS Semantical comparison

    “==” is a C# operator while “Equals” is a polymorphic method. So in other words “==” is a language feature while “Equals” is an object oriented programming feature which follows polymorphism.

    Now comparison is of two types one is purely based on content and reference, means computer based comparison and other is based on semantics. Semantics means the actual meaning of a thing. For example 1 <> 70 numerically ( technically) but 1 $ = 70 Rs in real world semantically.

    Some more examples:-

    • Technically: - 1 is equal to 1.
    • Semantically: - 1 Dollar is not equal to 1 Rs.
    • Technically: - “Destination “ word is not equal to “Last Stop”.
    • Semantically: - “Destination” means same as “Last Stop”.

    So technical comparison is computer based while semantic comparison is business based or we can say there is some kind of domain rule for comparison purpose.

    So now when to use “==” and when to use “Equals”:-

    • If you are looking for technical comparison then use “==” and most of the time “==” suffices as developers mostly do technical comparison.
    • If you are comparing semantically then you need over the “equals” with the semantic comparison logic and you need to use “equals” method while comparing.

    The Full Picture

    So if we list down all the point the final conclusion is the below table.

    == Equals
    Usage Technical based. Semantic based.
    Value types Content based Comparison Content based Comparison
    Objects Reference based Comparison Reference based Comparison
    String Content based Comparison Content based Comparison
    String with no interning Reference based Comparison Content based Comparison
    Type checking Compile time Run time
    Nulls Works Can crash

    See the following practical video on understanding the difference between == and .Equals :-

    Friday, March 11, 2016

    Learn ASP.NET MVC step by step: - Part 2


    Learn ASP.NET MVC step by step: - Part 2


    Step 11: Understanding Model binder

    Step 12: Create model Binder Class.

    Step 13: Attach the binder with the object and execute

    Step 14: Implementing validation

    Step 15: Check If the validations are proper

    Step 16: Displaying error message

    Step 17: Client side validation with Jquery


    This complete article is a guest post written by Mr. Prathamesh mestry https://www.facebook.com/prathamesh.mestry?pnref=story . If you think he is doing a good job send him a silent thanks to his FB.

    In part 1 we saw http://www.codeproject.com/Articles/1081437/Learn-ASP-NET-MVC-Step-by-Step-Part basics of how to start MVC. In this session we will see model binder and validations. In validation we will do server side validations using data annotations and client side using Jquery.

    In case you are completely new to MVC we would suggest you to watch this video series Learn MVC in 16 hours given in the below youtube video.

    Step 11: Understanding Model binder

    Model binder helps to connect the UI controls to the model objects of MVC. In the previous article our UI text box names where same as customer class names so we did not have issues. But what if you text box names are different from class names.

    For example below is a customer UI which has text box names start with “txt” and class property names are without word “txt”.

    You can see the below class is not in synch with UI text box names.

    publicstringStudentRollNo{ get; set; } //Student Roll No.
    publicstringStudentName{ get; set; } //Student Name.
    publicstringStudentStd{ get; set; } //Student Standard.
    publicstringStudentDIV { get; set; }//Student Division.

    This is where model binder class comes to picture. Model binder class binds the UI textboxes with model.

    Step 12: Create model Binder Class.

    To create a model binder class the first thing we need to do is implement interface “IModelBinder”.

    Right Click on ModelBinder Class  Implement Interface  Implement Interface.

    In the class we need to put the binding code in “BindModel” method as shown below.

    public class StudentBinder : IModelBinder
    public object BindModel(ControllerContextcontrollerContext, 
    HttpContextBaseobjContext = controllerContext.HttpContext;
    stringstuRoll = objContext.Request.Form["txtStudentRollno"];
    stringstuName = objContext.Request.Form["txtStudentName"];
    stringstuStd = objContext.Request.Form["txtStudentStd"];
    stringstuDiv = objContext.Request.Form["txtStudentDiv"];
                Student obj = new Student()
    StudentRollNo = stuRoll,
    StudentName = stuName,
    StudentStd = stuDiv,
    StudentDIV = stuDiv,

    Step 13: Attach the binder with the object and execute

    Once the binding code is completed attach the binder with the object and execute to see the results.

    public ActionResult Submit([ModelBinder(typeof(StudentBinder))] Student obj)
    return View("Student",obj);

    Step 14: Implementing validation

    Now that we have the student screen and the output is displayed it’s time to put validations. So we will be applying the following validation for our student class:-

    • Student name and roll number is required.
    • Student Rollno should be alphanumeric with 7 characters with first three letter characters and last four letter numeric for example :-ABC1234, XYZ5678.

    In order to implement validations we need to import “data annotation” namespace and decorate the attributes on the student properties.

    using System;
    public class Student
    public string StudentRollNo{ get; set; } //Student Roll No.
    public string StudentName{ get; set; } //Student Name.
    public string StudentStd{ get; set; } //Student Standard.
    public string StudentDIV { get; set; }//Student Divsion.

    Step 15: Check If the validations are proper

    When the object is filled the final output whether the validation is passed or failed is updated to the “ModelState” object. You can see in the below code we are checking if the “ModelState” object is valid by using the “IsValid” property.

    Depending on the property value its redirecting to the views.

    public ActionResult Submit(Student obj)
    if (ModelState.IsValid)
    return View("Student", obj);
    return View("EnterStudent");

    Step 16: Displaying error message

    To display error message we need to use Helper class as shown in the below code.

    Step 17: Client side validation with Jquery

    Install Jqurey validation from Nuget.

    33.1 In Solution Explorer Right Click on MyApplication (HelloWorld).  Click on Manage NuGet Packages

    Search jQuery Validation.  Select Microsoft jQuery Unobtrusive Validation  install

    Using Helper class Create UI elements like Textboxes.

    Output Screen of EnterStudent View using Html helper Class...

    Validation Output 2(incorrect RollNo)