Friday, May 25, 2012

Why Every Developer Needs to Learn Web Programming and What it Takes

Be a web programmer in less than four hours, yes that is my claim and I am assertive on that. learning web programming isn't rocket science, but people often get lost on their way as they start learning web programming since they are deluged with many technologies targeting the web platform.


OK, don't be deceived, this article isn't a tutorial on how to do web programming, because there is already opulent self explanatory resources on web, and adding any single line further will be redundant.

So, What is this article about ?
This article is basically intended to give an heads up to developers starting with web programming by delineating a systematic approach to learn basic web programming and explaining them why is it inevitable to learn web programming.
First part of the article speaks about the growing significance of web technologies which is leaving every programmer no choice but to learn web programming. And the second part unravels a step by step approach towards learning web programming and few links to free web resources available, making learning quick and easy.

Why every developer needs to learn web programming ?
Couple of reason, but to quote a few prominent ones.
  • Windows 8 and an increasing popularity of metro style applications.
  • Introduction of chrome OS and the popularity of chrome app store.
  • Re-engineered and elegant HTML5 UI
  • Bestowing browser with complex capabilities of native Multimedia playback, Canvas drawing, Geo positioning etc.
You like it or not, web technologies would be the most  hot and happening technologies of the future and it is coming in a big way. Developers working on OS components and other crucial system modules always had a misconception that web programming, which is basically mistaken as scripting is not for geeks like us. But in reality these misconceptions have become defunct.

To shed light on the facts mentioned above, significant number of metro apps will be coded in javascript by developers across the world, since it is quick and not a onerous task unlike other technologies like C++ and C#. Though the metro apps can be built using well known VB, C#, C++ no one would really acclaim this until there is something which can't be achieved by javascript. To quote an example metro style gaming apps using DirectX must be coded in C++ and there is no alternative for it.
With the release of Chrome OS which is complemented by chrome web store, and increasing number of web apps are built everyday. and if a developer is not there already, it leaves with very less opportunities to catch up with expedited technological growth.

With HTML5, the UI is revamped making web look more elegant and less cluttered. Browsers supporting HTML5 are fettered under the rules of W3C HTML working group to support native Video playback, Canvas drawing, Geo positioning and many other signification features. Once HTML5 and in turn Web Runtime can support these features it encourages significant number of app development with HTML5 UI and back end being built using other languages like C++, C#, VB.
Considering all the facts mentioned above, it makes it inevitable for every developer to learn and start programming for the web to be in concomitant with technological advancements.

Let's move on to the second part, 

What it take to be a web programmer ?
As mentioned earlier web programming is lucid, and not a painstaking task like other technologies. All which is needed is a proper guidance for a developer about what to start with and how to take it forward. This is very crucial since, when we speak about web technologies developers are deluged with hundreds of languages, frameworks, third party components  etc. Just to name a few we have HTML, XSLT, XML, XHTML, DHTML,CSS, JavaScript, PHP, DOM, Jason, JQuery, AJAX, ASP .. The list just goes on leaving developer confused about where to start with.

Below is my own experience when I started learning programming for web. Even I followed the well accepted approach of starting  with HTML, JavaScript, CSS before moving on to advanced topics, believe me it had its returns. Building a web page is basically an amalgamation of three technologies which results in an elegant web page. and they are,

HTML - Defines the basic structure of a web page.
HTML is an acronym for HyperText Markup Language. and it is basically a "Markup" language rather a programming language. A markup language, is collection of stylistic instructions, issuing the target rendering engine three basic instructions on,
  • What needs to be painted, 
  • Where it needs to be painted and 
  • How. it needs to be printed.
HTML is built on DOM architecture, where each HTML element is nothing but a DOM node. instructing web browser about the content to be rendered and its attributes.
CSS - Makes web pages look elegant with less work
By now you have a basic structure of web page which can convey the intended information, but let's present it in an eye catching way, use CSS, an acronym for Cascading Style Sheets.
CSS constitutes styling information to be used for different elements, and the reason it reduces significant amount of work is, once a CSS style is defined for an element it is applicable to all the occurrences of that elements hence avoiding specifying the styling information redundantly.
JavaScript - Makes web pages come alive by adding interactivity
Few people have a misconception that JavaScript is a variant of JAVA programming language, but the truth is far from reality. JavaScript and Java programming language share no resemblances and neither of them is derived from the other.
JavaScript adds intelligence to webpages so that, pages can talk back to the user. Rich web applications can be easily built using JavaScripts. Introspecting JavaScript it can be found that JS is in turn made up of two technologies, and they are,
  • ECMAScript - which defines the core language constructs, and
  • DOM elements - on which script acts on
And there needs an interface between these two technologies to work in synchronization and this is provided by WebIDL specifications. WebIDL is an interface definition language which prominently defines  javascript constructs for web runtime.

I hope this article, could elicit the intensity of need to start with web programming, and how to take it up further. for more resources kindly refer,
Happy Coding...

Sunday, May 13, 2012

Compiler's Optimization Techniques - Virtual Inheritance - Part 2

This post is continuation to Part -1 of this article, hence kindly skim through the first part if not already, before you  begin with part - 2 for better understanding.

As mentioned in the earlier part, in this post we get more insight into object layout , implicit pointer conversions,  pointer offsetting and virtual base class pointers in the case of multiple inheritance. And in the process we get good understanding of Virtual Inheritance. And a disclaimer before we proceed, the examples considered here are just hypothetical cases and not to disparage anyone.

Further extending the inheritance hierarchy mentioned in part -1, consider that now the company has decided to introduce an open source mobile OS, based on existing "Mobile_OS" of the company but enhancing it further to favor open source development with a strong eco-system of contributors and names it "Android".
 class Mobile_OS   
 {   
   public:   
    float iKernalVersion;   
    float iReleaseVersion;   
    char* strVendor ;
    virtual float GetKernalVersion();   
    virtual float GetReleaseVerison();   
    Mobile_OS();   
    ~ Mobile_OS();   
 };  
 class Android : public Mobile_OS   
  {   
   private:   
     char* strVendor;   
     char* strProjectCode ;    
   public:   
     int iAndroidCustomRomVersion ;   
     float GetKernalVersion();   
     float GetReleaseVerison();   
     Android ();   
     ~ Android ();   
  };   

The memory layout of Android class will be exactly same as of "WindowsPhone" class mentioned earlier, since both of these two classes are derived from the same base "Mobile_OS"

The design looks good till this point, with reusable modules and well defined class hierarchy. Now consider, the same company which had produced Windows and Android phones has now plans to come up with "Tablet" devices, with a hybrid operating system, based on superior features of both windows and android phones and making itself a unique OS by adding exclusive unique features and calls it " Hybrid_Tablet "

Do you foresee any problems with this design ?
Yes, we run into issues of data redundancy and inconsistency by deriving a " Hybrid_Tablet " from " WindowsPhone" and "Android" classes.

What caused issues of data redundancy and inconsistency ?
The cause of these issues is rooted in the fact that, both "WindowsPhone" and "Android" classes are derived from a common base "Mobile_OS" and hence both of these two classes persist its own instance of  "Mobile_OS" class leading to issues of data redundancy and inconsistency.

How expensive is this fault ?
It depends on size of the base class, in our case it is the size of "Mobile_OS" class. Even if the developer cautiously resolves issues of data inconsistency by object reference for class data access,  there is no solution to curb data redundancy. And if the base class is huge with many data members this design decision proves expensive.

What is the solution ?
The only solution to problems like above is to use Virtual Inheritance.

In a system of classes, belonging to a single hierarchy, if a class is derived from multiple base classes which are in-turn derived from a single base class, the concept of Virtual Inheritance is used to ensure that there is only a single copy of the common base class in the most derived class.

Deploying virtual inheritance in the current issue, we can ensure that the "Hybrid_Tablet" will have only a single instance of "Mobile_OS" which is commonly shared between "WindowsPhone" and "Android".

To inform the compiler to use single instance of "Mobile_OS" in " Hybrid_Tablet" the two base classes of " Hybrid_Tablet" which are "WindowsPhone" and "Android" should be derived virtually from "Mobile_OS" as shown below.

 class Mobile_OS   
 {   
  public:   
   float iKernalVersion;   
   float iReleaseVersion;   
   char* strVendor ;   
   virtual float GetKernalVersion();   
   virtual float GetReleaseVerison();   
   Mobile_OS();   
   ~ Mobile_OS();   
 };   
 class WindowsPhone : public virtual Mobile_OS     
 {   
  private:   
   char* strCodeName ;   
   char* iHardwarePlatform ;    
  public:   
   int iCustomRomVersion ;   
   float GetKernalVersion();   
   float GetReleaseVerison();   
   WindowsPhone();   
   ~ WindowsPhone();   
 };   
 class Android : public virtual Mobile_OS    
  {    
   private:    
    char* strVendor;    
    char* strProjectCode ;    
   public:    
    int iAndroidCustomRomVersion ;    
    float GetKernalVersion();    
    float GetReleaseVerison();    
    Android ();    
    ~ Android ();    
  };   

Memory Layout of an object in case of Virtual Inheritance
In the case of non virtual inheritance, it is most certainly accepted design practice that both base class and derived class will have the same starting address since in a derived class the base instance is placed first. For more details please refer Part -1

In the case of virtual inheritance, embedded base virtually floats within the derived object without having a definite fixed displacement. Hence there is an overhead of maintaining this information within the derived virtual object. and this is achieved by maintaining "Virtual Base Table Pointer" and  "Virtual Base Pointer Table" as shown in the diagram below.

Click on the image for high resolution version
The instance of each virtually derived class will have a hidden pointer "vbptr" which is an acronym for " Virtual Base Table Pointer" which points to "Virtual Base Pointers Table" of a class. The Virtual Base Pointers Table contains displacement of the virtual base within the derived class from the address point of "vbptr" in number of bytes.

In the above example "Android::vbptr" points to "Virtual Base Pointers Table" which has two entries. which are displacement values for virtual base, and the acronyms stand for,

 And_Dt_And_vbptr_And = In Android Instance Distance of Android vbptr to Android  
 And_Dt_And_vbptr_Mos = In Android Instance Distance of Android vbptr to Mobile_OS  

Memory Layout of Most Derived class in virtual inheritance.
Now, coming back to our original issue of  designing a " Hybrid_Tablet" by deriving from multiple base classes which are derived from common base class, can be addressed using virtual inheritance. And this is achieved by,

1) Deriving "WindowsPhone" and "Android" virtually from "Mobile_OS", and
2) Deriving " Hybrid_Tablet" from "WindowsPhone" and "Mobile_OS".

 class Hybrid_Tablet : public WindowsPhone, public Android  
 {  
  Private:  
        ..................  
        ..................  
  public:  
        ..................  
        ..................  
 };  

As mentioned earlier, in virtual inheritance the position of base class instance is arbitrary within derived class, and in this case, we can see that, instance of  "Hybrid_Tablet" has only a single instance of "Mobile_OS", "WindowsPhone" and "Android" and reference to "Mobile_OS" is maintained with the help of "Virtual Base Table Pointer" one for each class and which points to a single "Virtual Base Pointers Table"of "Hybrid_Tablet" class. as shown below.

Click on the image for high resolution version
 Hbt_Dt_Wnp_vbptr_Wnp = In Hybrid_Tablet Instance Distance of WindowsPhone to WindowsPhone  
 Hbt_Dt_Wnp_vbptr_Mos = In Hybrid_Tablet Instance Distance of WindowsPhone to Mobile_OS  
 Hbt_Dt_And_vbptr_And = In Hybrid_Tablet Instance Distance of Android to Android  
 Hbt_Dt_And_vbptr_Mos = In Hybrid_Tablet Instance Distance of Android to Mobile_OS  

With this, I hope this article delineates moderately deeper insight of virtual inheritance and its implementation details. Kindly mail me if you have any queries or suggestions.
Also, please let me know if you are further interested in digging deeper into virtual inheritance in understanding "Data Access" and "Function Calling" mechanisms, I can write a post on that as well.

E- Mail : mail2vijaydr@gmail.com
References : MSDN, CodeProject, CodeGuru, and other web resources.

ShareThis