Showing posts with label Theory. Show all posts
Showing posts with label Theory. Show all posts

Tuesday, January 17, 2017

The great developer divide

This will be non interesting to someone who is looking for code snippets. This is gonna kind of theory or history class with some predictions. As always predictions may come true not guaranteed. Lets come to the point. What is meany by developer divide? In simple sense, the developers are going to be divided into 3 species.
  • Application integration developers
  • AI / Algorithm developers
  • System / infrastructure developers
    • Cloud  developers  - includes device driver writers.
Before we think oh there are already 2 groups of developers and are named the same for at least last 10-15 years. What is new?

Evolution in Biology

Lets look at how new species are evolved in biology. If you are an opponent of evolution, you may better stop reading as without understanding the biological evolution, its difficult to understand any evolution including the formation of new languages and all. In biological evolution when some group does different things than the other group of same species or they are separated geographically a new species is formed. If the divide is for small amount of time, they can mingle together and continue as one species. In the other case, there will be 2 species.
Another reason for survival or destruction of species is the ability to suit to the environment. For example, consider the famous evolution scenario explained in England related to industrial revolution.
This is kind of easy and old explanation. The real reason is the gene mutation, which goes to the next level. Only the members who got useful gene mutations will produce more off springs and after a long time they becomes new species which has clear distinction from the old.

Characteristics of these developer species

Coming back to the software, what are characteristics of these new species.

Application integration developers

These are the majority of developers. They write applications which accepts data from user and store somewhere. When user wants the data they just show them or send to them periodically by means of some push mechanism, Some transformations on the data will be done by them which are more towards formatting the data. They never create new data ie knowledge from the data except generation of logs.

More importantly this species of developers will be vanished or become low profile when other species of developers become stronger. It will be difficult for these application developers to convert to the other species though some may succeed and survive. Whatever these developers were doing will become a task of business people. For example business analyst will be able to assemble applications and do most of the customizations. 

Currently there are some environments where we can see this is happening. Visual Studio LightSwitch is one to name.

AI / Algorithm developers

This species will write complex algorithms and expose them as service with the help of infrastructure developers. Application integration developers will consume the algorithms written by this group before they get extinct with the invention of AI programs which does coding.

This type of developers will be high profile, high in number and gets job safety for longer time. They will be knowing basics of application integration but never know what and how the infrastructure developers work.

We can see examples already such as Algorithmia, Azure Machine learning etc...

System / infrastructure developers

This species will know how to deal with the bare metal machine whether it is silicon based or quantum based even carbon based biological machines. They cleverly abstract the hardware from the application integration developers and AI developers.

There will be less number of developers in this species. They control most of the things. They may consume the algorithms written by the other species without knowing how its done.

Reasons for this prediction.

No one can just predict something and run. They should have some kind of reasons for the prediction.

Bare metal is going away from developers

This is something everyone is agreeing now a days. There are many many abstractions coming over the hardware and the main reason is to code once and run everywhere. Another reasons we can hear is the reusability, less development cost etc...

Frameworks

When we were developing for Intel 8085, we had to know what are the CPU registers, their size, memory addressing and port addresses. Now if we ask how many registers are there in the CPU to a new gen .Net or Java or JavaScript developer, they will not have any clue. If we further go back in time, we know there was punch cards and developers had to know so many mechanical & electrical properties of the system. After 8085 days, we got assemblers, compilers and we entered into managed world.
Now from the managed world we are going to the world of integration. Connect some dots and the software will be ready. Even for device programming JavaScript is used which completes the story.

Cloud is abstracting

Another abstraction is happening in the distributed systems world. Distributed means something which is done with 2 Turing machines. or somewhere data is serialized and passing through. Cloud started slowly with IaaS where we got virtual machine. Then came PaaS where we don't need to worry about machines anymore. It is now reached to FaaS where each function is a service and can be deployed independently. Another sweet name is Serverless. Now people are telling that though the name Serverless, there are servers underneath but developer don't need to worry. 

But think of a time where the underlying machine is a quantum biological computer. Still we can call it a server but the underlying system has completely changed and there will be no clue to the consumers how the FaaS executes code.

AI Algorithms are too complex to be understood by all

Earlier colleges were teaching sorting and searching algorithms. But people who studied those were not coding those algorithms everyday as those came as part of the standard libraries. That helped the developers concentrate on the real business problem. That is good. After those days the business requirements got a new face of data analytics. Everyone wanted to analyze their data. Some at least know what they want from data. Some are just fiddling with data for treasure. That brought a good momentum to the field of algorithms. Many algorithms were developed but the same question came again. Should we just consume those algorithms like how we are consuming sorting and searching or should we learn those algorithms. Obviously the route is same. The consumption. 

Another reason is their complexity. It is very difficult for normal application developer to know how face recognition algorithm works as it requires prior knowledge in different fields. So the consumption is easy. This eventually boosts the developer divide.

There may be many more reasons which are not listed here. However the developers are getting unavoidable division which occur in every growing science and technology fields. The best practice is to select own field and be an expert in it.

Thanks for reading.

Tuesday, August 9, 2016

Why its called as Virtual Machine?

Introduction

We might have heard the term virtual machine a lot of times when we work with languages which compiles to intermediate format instead of machine code. Some examples are Java, C#, Vb.Net etc...

We might have also complained that application is slow because it is running in virtual machine. There are 2 types of virtual machines in software engineering. One is the virtual machines which are running an operating system inside another operating system. Such as Linux running inside Windows machine. The machines we are getting in cloud providers are virtual machines. There are managing softwares such as Hyper-V and all to manage those machines.

But this is not the virtual machine when we talk in terms of programming languages and platforms. Lets get an understanding about those virtual machines

Machine

What is a machine refers here? Its nothing but an execution hardware which can handle a finite set of instructions. In another words a turing machine which takes one instruction process it and takes next and go on. Instructions may be simple 'add A,B' for adding 2 CPU registers to complicated GPU instructions. 

Unmanaged world / the bare metal machine

The software execution world was running without a manager in the beginning.  In the beginning programmers were talking in terms of 1's and 0's to each other and to machine. It was very difficult to deal with. If we want to add 2 numbers we had to use something like

11001100 1010,0011 // To add 10+3. Assume 11001100 is the instruction for add.

This instruction is commonly known as opcode too. Short form of operation code. This tells the executor here its the CPU/Processor chip to perform an add operation and place the result in a predefined location. Mostly it will be accumulator.

Machine language

The above notation can be called as machine language. Only 1's and 0's which machine can understand as presence or absence of voltage. This was similar to days when developers were using punch cards to enter their programs to computers.

Soon developers started denoting the numbers in hex where the above machine language can be written as

CC 0A,03

There are still some universities having 8085 based programming paper which gives this mode of entering programs in to computer. Even today we can open executable files and see it in hex format.
Everything in computer is actually in binary but for better understanding purpose, we can convert it to hex and display. Don't get confused with the ASCII representation at the right side. That is just coincidence. For example the opcode for add and ASCII code for 'D' may be same. Here the viewer application from where the screen shot is taken is trying to convert to ASCII for more readability.

Assmembly language

Soon people started using more human friendly instructions instead of hex instructions. That was called as assembly language. The above code can be written in assembly language as follows

Add 0A,03

Only the instruction / opcode changed data remained in hex.

Smart people created text editors in assembly language to write assembly language. That made the life of new developers easy as they can write programs editor which is built using assembly language itself.

There are no loops or most of the machines doesn't supported it. But we had an option to change the execution order based on conditions. Instructions such as JMP, JC JNC (Jump, Jump on Carry, Jump on No Carry) are some examples. Using these instructions developers were able to get loops implemented.

Till this point the developers were directly writing in the language the machine / hardware understands. After the assembly language, there were some programs started involving in between the program and machines.

Programs that prepare programs for execution

The concepts like macros, pre-processors etc...introduced the need for particular programs to prepare other programs for execution. In other words, if we are writing assembly language using English opcodes that needs to be converted to corresponding machine code. If we consider the above sample of

Add 0A,03

Someone has to convert this text 'Add' opcode to CC which is the binary equivalent of 11001100 for add instruction. The programs which are doing this conversion is called Assembler.

Still the assmebly language has instrcutions which can be directly mapped to an instruction which machine support. ie we have to read the manual of processor chip to know what are the instructions it supports.

High level languages

One to one relation between assembly language instruction to machine instruction was some kind of limitation and later people thought of more intuitive languages to program. A more human readable approach where instructions in program doesn't needs to have one to one relation to the machine code. Like assemblers, the program can be converted to machine language before execution. When converting, one instruction in this language can be converted as one more more machine instructions.

Yes the high level languages are born. Excellent example is C. We write in high level more human readable format and converted / compiled to machine instructions before execution.

This produced the concept of loops, functions variable types etc...Earlier it was all just bits and bytes with a jump instruction.

Internally loops might be using jump instructions, but to the developer, the details of machine was hidden. This helped more and more people to become developers. The don't need to how the machine works in the chip level or what is voltage or binary but still able to write programs.

Summary - unmanaged world

We can summarize this as a model where we prepare machine instructions either writing directly, or converting / compiling assembly or high level languages. Once it is ready just start execution. There is no one to manage its the machine and it just executes our code. 

Pros

  • Talks directly to the machine. Able to leverage full power as there is no additional overhead

Cons

  • Needs comparatively high skill
  • Development is time consuming as there are less reusable things and abstractions.
  • Needs to worry about many things such as memory allocation security etc...
    • Security here refers to access data from concurrently running program in case there is no operating system level process separation.
  • The machine instructions vary by the macnufactuer of the chip. The instructions supported by Intel chip may not be available in the chip produced by AMD and viceversa. So the developer need to compile their program multiple times to produce the executable for different chips. Also the instructions may vary by the 32 bit and 64 bit.

Managed world

Normally when people say managed, they refer to the technologies such as JVM or CLR based execution where there is a program which runs our program. To be more specific,
  • Developer write the programs in any of the programming language supported.
  • The compilation produces an intermediate code / byte code which the hardware/machine cannot understand .
  • There will be another program (JVM / CLR) which is in machine code, reads intermediate code  produced by developer and execute instructions one by one.
  • This executor program acts like a hardware machine which can understand a specific set of instructions.

So what is virtual machine?

Under the hood its the same machine instructions which are executed by the processor chip. But it is hidden from an end programmer such as a .Net / Java developer. Developers see only the instructions supported by intermediate / byte code as the machine instructions ie a machine which is not there physically. So they target their code written in high level language to the byte code / intermediate code which is understood by the executor program / ie a virtual machine. In .Net world its called as CLR and in Java its JVM. Executor program reads it and carry out the task by giving required machine instructions to the chip. 

The executor program need to be compiled for different chips because it is in the machine language. But that needs to be done by less development teams such as the JVM and CLR. The rest of the development world don't need to worry about compiling their code against different types of chips. So more development teams take the route of managed world.

Since the executor program is reading the developers program to execute, it can easily enforce so many rules and regulations to manage the execution. Some limits the developers capabilities and example are the lack of pointers. Some reduces the overhead of developers and increase productivity. Examples are the memory management (garbage collection), standard libraries. Standard libraries, even we had in unmanaged world as well.

If the chip manufactures were having a worldwide standardization consortium and they produced chips which supports same instructions the managed world might not have been invented.

Pros

  • Portability of intermedite code makes the deployement easy
  • The virtual machine can enforce rules
  • Multiple languages can be compiled to the intermediate code if there are compilers.
  • The above helps to refer an internediate language library written one language by another language.

Cons

  • Little overhead on converting the intermediate code to machine code during runtime,
  • Lack of features such as pointers and limits developer from talking to the bare machine though it can be worked around.
  • Delays the usage of new hardware features till that is supported by the virtual machine. eg: Parallel instructions in new processors

Is Operating System a virtual machine?

If we are writing windows programs we feel that OS is an virtual machine as we are targeting the Windows APIs. Also when we look at the process boundaries and limited access to memory, we can see OS as virtual machine which kind of hides the hardware. But since our programs are compiled to machine language, OS is not truly a virtual machine.

Reference

http://c2.com/cgi/wiki?VirtualMachine

Tuesday, July 26, 2016

What is side effect of a function in programming?

Introduction

This articles explains what is side effect of function and advantages of avoiding side effects

Background

We all work with object oriented programming for years and our normal thinking goes to desinging application as similar to real world as much as possible. We use object oriented design principles to achieve the same. Shoud we really make everything object with its own class level properties? We are trying to investigate is there any better way to write code which is maintainable

What is side effect of a function

When we write a function / method in the first place, what is our intention? It does a job. It may or may not accept parameters. Sometimes it returns a value. That is the effect of the function invocation.
But there might be situations, that a function may start doing things which are not controlled by that  function. Look at the below code. 

//
// ES6 code which has a function with side effect.
//
class Calculator {
  constructor() {
    this.op = "add";
  }
  calculate(n1, n2) {
    if (this.op === "add") {
      this.op = "sub";
      return n1 + n2;
    } else {
      this.op = "add";
      return n1 - n2;
    }
  }
}


Here what is the effect? As consumers we call the calculate() the effect we are expecting is the calculated value. But what is happening here?

We get the calculated value. But in the mean time its doing something else on class level variable which we as consumers, don't expect.

It makes our next call gives different output for the same input values. Those are considered as side effect of the function. Side effect will be seen, if we look from the consumer's view.

Below is the code to invoke the function 3 times with same input.

var calc = new Calculator();
alert(calc.calculate(20, 12));
alert(calc.calculate(20, 12));
alert(calc.calculate(20, 12));

Drawbacks on functions with side effect

The functions with side effects make it difficult to reason about code. What is reasoning about code? If we simulate the working of code, it is little difficult as we need to remember the state too along with the steps. This makes it difficult to achieve development speed and often cause bugs as it is difficult to think about all the possibilities.

This again brings constraint on calling order especially when there are multiple threads using same object to call the methods. When those kind of scenarios occur, it would be difficult to reproduce the bug at the developer end. Normally when developers encounter these types of thread issues, they simply put a lock around critical section. That simply brings us the effect of old single core processors. Our program will not be able to take advantage of multi cores if we just put locks in our code as it make the threads wait for each other. This may lead to deadlocks as well.

What else brings side effect to a function

Not only changing the global state is considered side effect. Whatever we are doing to the state of outside world is considered side effects. Consider the modified code.

// ES6 code which has a function with side effect. It doen't alter class level variable.
class MyCalculator {
  calculate(n1, n2, op) {
    var result;
    if (op === "add") {
      result = n1 + n2;
    } else {
      result = n1 - n2;
    }
    console.log("success");
    return result;
  }
}

This seems like side effect free. But if we look at the logging code, we can see its changing the outer world. So has side effect. If somebody has run the below code before invoking above method, only alternate calls will succeed.

// Redirect the console.log
var shouldSuccess = true;
console.realLog=console.log;
console.log = function(msg) {
  if (shouldSuccess) {
    shouldSuccess = !shouldSuccess;
    console.realLog(msg);
  } else {
    shouldSuccess = !shouldSuccess;
    throw "I am tired!!"
  }
};

In the real scenarios, there might be situations similar to this and that will cause the function fail inconsistently.

Can we write everything without side effects

The world is stateful. Its not possible to write every method in production code to be side effect free. But we should try to minimize the side effect as much as possible. Especially when we are writing sequential algorithms, transformations etc...Also we can we immutable objects to avoid risks.

Points of Interest

There are concepts like pure functions, idem potent functions which are related to side effects. The function programming concept too trying to solve the side effects to an extent.

Tuesday, April 15, 2014

Code for yourself before you code for others

I got a strike from the quote "Code for yourself before you code for others" when I attended Google IO Extended 2012 event. Even though I was developing tools for me, I never took that as serious in determining success of a Software engineer. After 2 years of experiments related the quote, I could see that this is something important for a software professional. Now I am utilizing this quote for hunting talent for my current company.

Usually in software services companies (I would like to call as code factories), the ultimate aim is to deliver the project with working features. If the delivery mode is "code delivery" there are chances that there will be an acceptable level of quality. But in the other way "features delivery", we can never expect quality in the code. The features will be working. That's all. In such companies the hiring pattern is as follows. I know the point here is about code for ourselves, but through this I can show the importance of, one coding for himself.

Hire worker people to increase head count

This happens in big companies in below situations
  • Show their strength for marketing and to get more projects
  • Show that they are stable, still growing and not affected by recession etc.
This also happens during the period a small company tries to become a medium company or when medium company is becoming large.

Type of People

In this type of recruitment companies normally goes to campus to recruit people from even non computer science branches such as Civil, Agriculture,Accounting etc...who sometimes doesn't know how to shut down the computer.
 
But the main focus in this hiring will be common sense of the candidates, so that they can be moulded as workers who at least don't harm the software systems.
 
If we think from the candidate perspective, these people will not be having particular aim in their life except to get equal status of others and earn money. When they complete their schools, they just listen to others(relatives & friends) words and select Electronics or Mechanical engineering, instead of hearing their own mind. Their mind may be telling them to become an accountant. But as the factor of common sense and adaptability is more, they will get more marks in the exams even though they don't know anything practically about their main stream subject. Believe or not I had multiple situations where people who scored more than 80% marks in Electrical engineering department don't know, why they are not getting electric shock, when they use tester to touch wires. These people again take others words and attend the interview conducted by software development companies.
 
Also these people won't read much about theory part computer science. If they know RegEx & parsing, I would say its really great. One of the benefit for them is they can switch to any other job at any time as they don't have the bonding with computer science. This is really helpful during recession days.
 
As these type of people don't have any particular aim except money, these people can be moulded to work on anything, provided they get right instructions.

Hire own people (Relatives & Friends)

This happens in most of the companies. The manager / higher level executives inject their own people into the company with their influence in 95% cases and rest through proper channel. They do it due to various reasons
  • His friend is really good in particular technology and the company needs the talent
  • He wants a strong team who can take his orders without fail or complaints.
  • His relative / friend is not getting job in other companies after he was fired from previous job or passed out of college.
  • He needs a spy in the team so that he can know the heart beat of the team.
  • He want to take his person to abroad. Let this be the way.

Type of people

95% of the people who are hired in this way will not be meeting the hiring criteria and will be a burden to their colleagues which in turn reduce the team's productivity and demotivate the loyal people. If these candidates are really skilled they should have joined in other companies. That itself shows these people are not worth.
 
The real trouble will be to the middle level leads to whom these hired people are reporting. He cannot keep the person in the team as he is not capable of doing the tasks, but afraid to escalate as he is backed by the manager. This in turn increase pressure to the rest of the team in meeting the deliveries. The pressure eventually makes the loyal and talented people, leaving the project or company.

Hire the talent to think & innovate

Worker people alone cannot run the company. There needs some smart people to think and take decisions and finding those people is really a tough job for interviewers. Within the 30-45 minutes how to identify whether the candidate is really talented or not? The future of the company relies in the hands of these people as these people are going to think and take decisions for the company.
 
I faced the challenge so many times. Earlier I used to give various scenarios and ask them to think about alternatives. If you are an interviewer, you don't need to worry about these type of questions. Just take one of the scenario in your project and explain them in a different way. If they think smart and provide better solution than what you implemented, you can even refine your solution :) .But don't get into trouble.
 
Now I have added one more criteria to the list and its nothing but "The solutions they developed for themselves". If they have solved their own problems successfully, 50% we can say he suits to the talent criteria.

Type of people

Here comes the passion. These people will be passionate about software engineering. They will not be just programmers of particular language. They try to understand what the user needs and think about various solutions and picks up the best one. They will be continuous learners and brave enough to remove or add new tools & technologies into the project. The toughest part is they don't compromise quality to meet delivery timeline.
 
In short if a company have at least one person like this, it can have 50 worker people.

Monday, January 27, 2014

Programming for daily life - Introduction to programming

It was my great ambition to take training to non-programmers about programming. I had taken training sessions to programmers about various languages and technologies. When I talk to programmers mostly I feel that I and the audience are in the same frequency. I was always wondering, what will be the experience if I explain "what is programming" to non-programmers such as the HR team, Admin team, QA team, UI team, etc... Recently in my company, I got a chance to conduct such as session. We named it "Programming for daily life" and split into 3 sessions.

  • Introduction
  • Debugging
  • More on scripting (Office automation using Excel, Word, etc...)
Due to 3 rescheduling of the first session itself, there were not many attendees. But I believe they got something. At least what is a program, who is a programmer and his duties, and how a program is executed in the machine. This was one of the training I got satisfaction as a trainer. The ppt I used is attached below. If you are not able to view it go directly to the slideshare.

I am really interested to take the session again and again.

Monday, August 26, 2013

Calling explicitly implemented interface methods from other instance methods in C#

In one of my previous post related to interface usage, I had asked to explicitly implement interfaces always to achieve the principle of "Program to interface not to implementation". If we follow the advise you may enter into scenarios where you need to call an explicitly implemented method from another method. Another method can be interface method or normal method. Below is the situation.


    public interface IMyInterface
    {
        void Foo();
        void Foo(string value);
    }
    public class MyClass : IMyInterface
    {
        void IMyInterface.Foo()
        {
            Console.WriteLine("Value is {0}", "Default Value");
        }
        void IMyInterface.Foo(string value)
        {
            Console.WriteLine("Value is {0}",value);
        }
    }


In this scenario its good to call the second Foo method (parameterized) from the first one. But if you try to simply call it, you will end up in compilation error. Also you cannot do it using this keyword even though the method resides in same class. So what should we do? 

If you really studied the theory behind the explicit interface implementation before coming into the programing field, you would have known this scenario. But still there are people who needs google to tackle this situation. The solution is to cast this object to IMyInterface and call the method. Below is the code.


    public class MyClass : IMyInterface
    {
        void IMyInterface.Foo()
        {
            (this as IMyInterface).Foo("Default Value");
        }
        void IMyInterface.Foo(string value)
        {
            Console.WriteLine("Value is {0}",value);
        }
    }

Happy coding...

Monday, June 3, 2013

Online Karel Simulator based on HTML5 & Javascript

Karel is an educational programming language for beginners, created by Richard E. Pattis in his book Karel The Robot: A Gentle Introduction to the Art of Programming @wiki
If you are not familiar with what is Karel in programming, please read this wiki page. As I mentioned in one of my previous blog post, I got to know about Karel, while preparing for a session of Programming for non programmers. It's such a simple language that, anybody can teach programming or use for self study. Universities such as stanford are using this simple language to teach programming methodology to it's students. If you studied / studying in new generation Indian engineering colleges, sorry you don't get this easy option of studying programming. I don't want to use the term 'self financing colleges' where your teachers will be your seniors who just passed in the previous year. I am sure on this, because I studied in this type of college, where I had seen people repeatedly saying 'public static void main...public static void main' during the lab exam days.

Whatever it is, this post is written to not to describe about the colleges, but to talk about Karel language, and a small HTML5 based Karel simulator, I wrote in the javascript language to execute Karel programs. The story which lead me to develop this simulator is as follows.
  • I found the language very simple & interesting to teach programming.
  • The main attraction is the way, we can teach programming to non programmers by simply saying we are controlling a Robot named Karel by some instructions.
  • I could see some interesting simulators where an animated character Karel, is acting as per the program execution.
  • None of those simulators are easy to set up in our environment.
    • Either they are written in Java or other language which is not easy to set up in my environment.
    • Got one .Net simulator using Visual J++, and that story I explained in previous post
  • No HTML JS based simulators, which can be easily set up as browsing a web site.
  • After more google, I decided to create the first one myself.

Karel the robot learns javascript

If you go to the Stanford page, you can see "Karel the robot learns Java". I just borrowed that and changed it to javascript, on a belief that, they will not file patent case on me. This means the simulator is not supposed to run the original Karel language. It can run the Javascript, which will take effect on the Karel standing at the right side simulation area.

What Karel language supports

There are fundamental 2 things which Karel can do.They are
  • turnLeft
  • move (move one cell in the direction which Karel facing at the moment)
This turns to 2 methods in the Javascript. These methods can be used to write the program in conjunction with the other javascript language features such as if, loops, methods etc...If you want to turn the Karel to right direction you need to issue 3 turnLeft instructions or write a turnRight method which contains these 3 instructions or a loop which does the same.

Javascript function to Turn Karel right


//Program to turn Karel right
turnRight();
function turnRight(){
    turnLeft();
    turnLeft();
    turnLeft();
}

How to work with Karel simulator

Write the javascript program at the right side text area and click on "Run program" button at the bottom of text area. You can see the Karel is obeying your instructions.

Want to try HTML 5 JS Karel simulator 

I have hosted it in my own website http://joymononline.in . URL link is 

Source code of Karel simulator

If you have experience in Javascript you might have guessed the core technique behind the simulator. It's the same eval() function in Javascript. There are 2 global functions turnLeft() and move() written and those functions are being invoked from the user input program by using eval method.

I will upload the source later to one public repository.

Monday, May 6, 2013

C# v/s VB.Net Program to interface not to implementaion

Why it is said that "Always program to interface not to implementation"?

The reason is simple. If we program to interfaces, we can change it's underlying implementation at any time without changing the code which is using it. For simple example, we all deal with databases on a daily basis. When we are writing DAL (Data Access Layer) and our track codes point to the IDataAcessLayer the implementations can be anything  .ie it Can be SQLDatAccessLayer,OracleDataAccessLayer etc...Which means at a future point if your project stakeholders decide to change the DB there is no need to change your track code it the application logic projects. Only we need to change the object which is created for IDataAccessLayer.

Challenges in enforcing "Program to interface not to implementation" in large projects

Like any other engineering discipline, its easy to manage and keep the quality, if the team is small. We can have frequent reviews. Mentor them, even monitor closely. When the team grows beyond 100s with half of them are just passed out from college, its really difficult to make sure we are following this principle. This will not a problem, if you are developing a product where you have full control on the release timeline or in a project where you have lavish budget to allocate senior developers for code review. But this is not the case in most of the Indian software service companies.

C# - Enforcing "Program to interface not to implementation"

In programming languages like C# its very easy to make all developers follow the principle. Who ever developing the classes, need to implement the interfaces explicitly. Which means the track developers ( fresher category normally fills this position) cannot refer the interface methods using a class variable which they could have otherwise.

It's simple. Isn't it? the interface members are public by default. No question on that. If we implement it implicitly in a class, the other developers can create the variable of that class and invoke the interface members. Lets see the below code snippet which will give more understanding about explicit interface implementation.

    public interface IMyInterface
    {
        void Foo();
    }
    public class MyClass : IMyInterface
    {
        void IMyInterface.Foo()
        {
            Console.WriteLine("MyClass.Foo");
        }
    }
    public class Program
    {
        static void Main(string[] args)
        {
            IMyInterface mI = new MyClass();
            mI.Foo(); //This ensures that the members are only be accessible via interface variable.
            MyClass mC = new MyClass();
            //mC.Foo(); This will cause compilation error.
            //TestMethod();
        }
    }

VB.Net - Enforcing "Program to interface not to implementation"

In C# its easy as there is explicit interface implementation. Even there are more help on the same. But VB.Net doesn't have that concept. So the solution to enforcing 'program to interface..' in VB.Net, is the alternative of explicit interface implementation, similar to C#. And the solution is as follows.

Implement interface members as private in VB.Net to achieve explicit interface implementation.

No need to wonder, thinking that interface members are always public. Read the MSDN page for more details. Below is a code snippet which does the same in VB.Net

Public Interface IMyInterface
    Sub Foo()End Interface
 Public Class MyImplementation
    Implements IMyInterface
    Private Sub Foo() Implements IMyInterface.Foo
        Console.WriteLine("MyImplementation.Foo()")
    End Sub
End Class
 Module Module1
    Sub Main()
        Dim mI As IMyInterface = New MyImplementation()
        mI.Foo()'Works
        Dim mC As MyImplementation = New MyImplementation()
        'mC.Foo() ' Doesn't work
    End Sub
End Module

It took really long time for me to find out a mechanism to enforce the "Program to interface..." principle in VB.Net.

Monday, April 15, 2013

Where is vjslib.dll located?

Recently I had to prepare for a session related to "Programming for non programmers". I thought of starting with what is computer and programming using some day to day examples. But there is no meaning in talking 1 hour about programming to such an audience who comes from non programming background. To obey the famous "A picture is worth a thousand words" saying, I thought of giving a demo of programming to the audience. But it was tougher than showing a demo in a session about Node.JS or DSL because in normal technical sessions we can show demo using the corresponding language and the related tool. Here which language I should choose? In what tool I can select? Will the surroundings of Visual Studio take the attention of these people from the code window? 

Karel the Robot learns programming

These questions lead me to the famous Karel programming language. The first time, I came to know about Karel the Robot is in the programming paper CS106a of Stanford university syllabus. Don't think that, I studied in Stanford but attended their free online programming course via iTunesU iPad application. But most of the Karel runtime environments are available in Java and Java is not setup in our machines. Googled for online simulators but failed. So decided to go with one .net implementation found in the below site.

http://www.acthompson.net/DotNet/Karel.htm

Location of vjslib.dll

I downloaded the project but it failed during build. The reasons was simple. Its based on Visual J# which was included in earlier days of .Net and slowly removed from Visual Studio suite. It needs a dll named vjslib.dll. I searched for all the folders in my machine related to .Net 2.0 but cannot find. 

Finally google gave me the location of the Microsoft Visual J# Version 2.0 Redistributable Package which contains the vjslib.dll file. Installed it and things started working.Below the location where we can find the vjslib.dll file

C:\Windows\Microsoft.NET\Framework\v2.0.50727\vjslib.dll

Location of Microsoft Visual J# Version 2.0 Redistributable Package 
http://www.microsoft.com/en-us/download/details.aspx?id=4712

I don't think this post will be useful if you are developing a new project. But useful, if you had to maintain or work on legacy code base or ended up in a sample which is created using VJ# assemblies.

Monday, December 31, 2012

What is Domain Specific Language (DSL) in software development

Wish you a very very happy new year to all.Let god make all your ventures a great success.

To me, its more like a technical new year. Last year the main plan was to get married. This year needs to improve more on application architecture side of software engineering. OK. Leave that with me.Lets get into the topic.

It took roughly 2 years to realize that we are using DSL in our project. I don’t know, why I didn't realize it in the first 2 years of project in which I was fixing DSL issues and adding more features to that. Reasons may be
  • In the first 2 years I was / had to concentrating more on the deliverable and the technical (WPF,Silverlight,WCF,Linq etc…) aspects of project rather than its logical architecture design and now I am more focused on Architecture.
  • The trainers (we had few in the beginning) didn’t tell us that we are using DSL in the project. Or nobody thought of the word DSL even though it is using in the project. (I had seen people using Singleton pattern without knowing the technical term ‘Singleton’)
  • There are 2 types of developers in the project. I would say application creators. One group is hard core .Net developers who work on the framework using VB.Net and other team uses a tool to create business screens ,its validation and the business flow.If I spent some time on thinking what is the difference between these 2 groups, I would have realized that we are using DSL in the project.
Its all fine.Now lets see what is DSL in programming ? According to Martin Fowler “DSL is a language for solving a specific problem rather than general purpose language which can aim at any problem.It’s presence is there from the beginning of computing”.The examples are SQL - which is aimed to manipulate the data storage domain, Excel formulae – Aimed to mathematical domain, CSS etc…More details on wiki

Are we using DSL in our day to day projects?

Yes of course .All of us uses DSL in our daily programming. Just think about the below scenarios and confirm yourself that you are using DSLs.
  • The framework you are created to solve the problems in your domain. The track components call the framework to fulfill business need.
  • The business layer methods you are writing to meet specific business requirement.
  • The configuration sections which are specific for business domain.
All the above scenarios are aiming to a particular domain and it’s execution sequence can be altered or configured. A language doesn’t needs to be with line numbers such as VB or enclosed in curly braces like C family languages. “Language is a medium to communicate”. By changing a configuration entry you are talking to the computer.Isn’t it?

Importance of DSL in Application development

Before we see what is the importance of DSL we should know what is special in software development which is not in other engineering branches.

Software development v/s other Engineering_disciplines

Its simple. As everybody knows the major difference is ‘constant requirement changes’. A ship construction or bridge plan will not change, if its on half way. But the software does. Can we say no to those changes? Never. Because if we say no, the project will go to another vendor and we had to look for another job. Can we collect all the requirements from the users and document the same before we start development? 80% of requirements we can collect.But the chance to collect 80% requirement is only 50%. We cannot blame anybody as the users don’t know what they want from software or they don’t know what software can do to make their life easier because they are not familiar with computerized system of their work.

If we take a civil engineering graduate, he learns how to construct a bridge in the class room and the same can be applied in real time when he works for a company project. I agree that there can be very slight variation. But majority are same. But take a look at software engineering graduate. He learns how to find out factorial of a given number during college days and he is assigned to create a site which needs to serve 1000hits/second. This is the case in almost all Indian institutions. No idea about foreign institutions.

The same applies to the business cases as well. The business of civil engineering will be same always or atleast engineer is familiar with the terms. But in case of software its is different. He needs to solve the issues of accounting, auditing or insurance and make their life easier without any prior knowledge in the business domains. Yes there are business analysts who are supposed to bridge the gap. But I don’t know how far they are good in bridging. May be I have never seen a real BA. All the BAs I have seen in may programming life are just requirement writers who write down requirements from users. They are not requirement creators. I strongly believe that only requirement creating BAs can bridge the gap between original domain expert users and pure programmers.

How to tackle the requirement change scenario in development

Can we solve all the above said issues using a magic word DSL? No.We can solve the first one and partially the last one. The solution to second problem is with institutions changing their course structure and how interested the student in knowing about industrial software development. Ok let’s concentrate on how to solve the first and last problems using DSL.

Solution – Convert your software users to programmers.

This sounds crazy.Are they going to write C# code?
No.They are going to write DSL programs

Is it practical ?
Yes. It is and we can see so many examples around. By introducing formulae and macros Microsoft excel made so many programmers. By writing cell formulae and macros aren’t the accountants and other general people became programmers? Yes they are.

May I go to users and tell them you need to learn programing?
It depends based on the level of your clients. In most of the scenarios you cannot tell them that they need to program. You need to use the words customization, configuration etc…When client reaches a mature state you may tell that there is a DSL which they can use to modify the application.

So what is the magic : Whenever you had to create / design software, model a domain language and program using it. Thanks to my former colleague Praseed Pai for the sentence.

Example - DSL in salary calculation scenario

Lets look at a real time scenario for a normal developer other than taking scenarios like excel macro .
First we need to see, how it is working without DSL. You first met the customer who is located in Kochi .He told the formulae to calculate the salary according to the on going process / circumstances.  And the formulae is

Salary=Basic+HRA+DA (It’s a small company .So no PF and other factors)

You created the software in C#.Net and the technology used is windows forms . All went well and got a good payment.

After some months Kochi became a metro and your client needs to add one more component into the salary called “Metro allowance”. Client again approached you to modify the software to add the new salary component. You modified the software and got payment for that too. Client was able to reduce (I don’t want to use the word fired) work load of 2 people in the administration department after introducing your software so that they were able to concentrate on growth aspects of company and it grown to a bigger size.
The next change came when they grew. In India if the number of employees is greater than 20, (don’t know the exact number) companies needs to give Provident fund (PF) to the employees. They approached again and this time the change is little more complicated. PF needs employer and employee contribution which leads to calculation of gross salary, deduction and net salary.

From here story can be diverted in 2 ways. You charged more by saying that, we introduced a configurable salary calculation technique so that you can enter the formulae in a particular text area and the system will calculate based on that. For example in the formulae you can refer the Basic pay using _BASIC_ . Obviously client became happy and as they are growing, they decided to create a web site and gave the contract to you. Your client is growing and also you. You will be contacted someday by other companies to purchase the same software. There is no best advertisement than your customer’s.

The other kind of the story can be like this. You charged same or little more and delivered the support for PF. Client is satisfied with the change. But they started thinking, how their competitors and other companies calculate salary. One day they will hear from others that “you know one thing we are using a salary calculation software which is configurable .There is no need to call software guys for formulae changes”. Obviously next time client will purchase that software or ask you to convert existing one in a configurable way. If you say no, that is the end. Your client will grow definitely but without you.

How was the configurable salary calculation happened? Its nothing but a DSL. You modeled / created it knowingly or unknowingly. _BASIC_ is part of that language.

Can I get a sample which uses DSL in C#

Its all good. Theories are always good to listen or sleep. How can I use this in real time as a .Net programmer ? What are the technologies I need to learn? In what language I can write DSLs? Any example ? Am I supposed to create a language and its parser, compiler etc…!!! It seems people are not suggesting that as it involves more effort.  Also when I googled people were suggesting to embed Lua, use Antlr to create parsers,define xml schema to write DSL and .net code to evaluate xml etc…How to pass my domain objects to the DSL code? How to execute the DSL?

Hopefully you can expect a post which answer the same next week same day.With the salary calculation sample of course.

Monday, August 13, 2012

Computer is a Turing machine

Some fact about Turing machine

  • It is a machine which can manipulate data based on instructions given.
  • If a machine can act like or simulate any other Turing machine by accepting different instructions, it is known as Universal Turing Machine.
  • Today’s computers are all works on the above principle except they manipulate data in high speed and parallel.
  • This machine was first described by Alan Turing in 1936.He is known as “Father of computer science”
  • Alan Turing described the machine using tape and head where the tape contains data and a head can move the tape and modify tape contents according to the rules. The difference with modern computers is tape is replaced by main memory (RAM) and head replaced by Processor.
  • Alan Turing also expected that at some time in future there will be intelligent Turing machines (AI machines) and he suggested a test to prove whether its actually intelligent known as Turing test.
  • The above test  can only be conducted by a human.
  • You can see a small Turing machine in action online here.

This is a must have knowledge for every software engineer to create better machine friendly applications.