Being a ‘Disruptive’ Software engineer

There are great Software engineers. The ones who can knock everything that you give them out of the park. Any great challenging work will be a piece of cake for them. But then there are those other engineers, who innovate ways to make their lives easier. These people disrupt the way the job is done. Even when you give them the most mundane job, they know how to make it the most interesting thing that they have worked on. In this post , im trying to cite some instances of how some engineers that i have worked with changed the game. Even I had attempted some small stuffs in my experience, im not citing any of them here. I will attribute credits to the people involved in each of these instances based on their consent.

Instance 1

Here is a fresher who walked into one of my previous organizations and changed the product completely, forever for good. He walks in to a team of extremely competent senior engineers (top 1 percentile in the world) who are dealing with a massive application with lot of performance issues trying to re-architect and make it simpler. But there are always these out of box ways to do it. He goes ahead and comes up with ways of implementing javascript based AJAX calls and makes everything simple, even without rearchitecting (all of this when jQuery was not as famous as it is now). Then he goes ahead and writes a Javascript framework that will ease things out for every other developer to implement common features. He did not stop there. He went ahead and changed the UI completely and made it more smooth and highly performant. All of this without breaking his heads over how to rearchitect the whole system for 6 months.

Another instance the same developer has to implement a functionality, where there are 1000s of Input controls that needs to be added. These are grouped in to User controls. Normally anyones reaction at this would be that ‘uff such a boring task’. This guy did not start implementing the user controls. He went ahead and wrote a desktop tool that will churn out user controls based on the inputs that he gives. A program manager could go and churn out the user controls based on his requirement and it getts integrated . User controls were not vanilla UI. it had validations, integration with the C# objects and stuff. and all of it done.

Instance 2

There was a mundane task of creating SQL scripts from a big excel sheet. It cannot be done easily just with the Excel scripting, because its a combination of multiple excel sheets and requires a lot of validation. One developer goes and figures out that it needs to be automated and gives the idea to the management. What used to be a 4 person month effort suddenly reduced to 4 person days effort.

Instance 3

One of my colleagues had to implement a graph of relationships behind the sharepoint. and the SQL server way of doing things proved to be really slow. This guy gets to hear about Neo4J, goes ahead works over a weekend and monday comes up with a fork of neo4J that is highly optimized for the current environment. He is the best Neo4J developer that i have known. What could have ended up as a boring Sharepoint work, turned out to be the most exciting journey.

There is no point spending all hours writing code that will eventually get thrown away after few years. Great engineers can finish the work in 1/4 of the time a good engineer would do. But these Disruptive engineers are those ones who can do it in 1/8th of that time, and make it more ubiquitous and completely out of the box.

If you find yourself doing that regular architecture stuff or coding for the whole day, trust me you are in the wrong path.if you work on your 8 hour planned task for the whole of 8 hours , then there is something seriously wrong. try to figure out a way to complete that 8 hour task in 2 hours everyday and spend the rest of your time in the ways that finds you happiness. Might be coffee with 10 different people (This is such a good thing. A great learning experience) or work on something really big. Try out a completely different technology. Thought leadership is what is important. We need great Thought leaders along with some great executioners. Tomorrow when you walk into your work, try not to work on that mundane task for 8 hours. Change the way it is done.

Instilling Confidence – The Hanuman Way

The person whom you talk to  needs to be confident about what you say. He/She should start believing that you are worth believing. Lets see some of the learning’s(if not all)  that could be taken from the conversation between Hanuman and Sita in the Ashoka gardens.

Hanuman went in search of Sita in Ashoka Garden. Sita was very worried and she was about to commit suicide. Now Hanuman decided to talk to Sita and convince her that Ram will come and save her. But Sita is in a very paranoid state and Hanuman has to be careful in choosing the words and the dialect he has to speak. He decides to use a normal human-being dialect( and not a brahmin or sanskrit) so that it will suit his appearance and Sita. – Speak the language of the listener.

Then to gain the confidence of Sita that he is actually a messenger from Ram, he started describing the details about Ram that Sita knew. He also narrated some incidents that had happened between Ram and Sita. This gave confidence to Sita that Hanuman is trustable. – State the verifiable facts(/facts that could be validated) that gives an assurance of the authenticity of what you say.

Then Hanuman, describes himself, narrates his story and the things that he had done in the past. including his story of trying to swallow the Sun, how he met Ram/Lakshman and his own prowess. – Give a clear picture of what you are, your abilities and your experience

When Hanuman  offers to take Sita to Ram that instant, She doubts his capability and ridicules that its the nature of a monkey to get excited and think of impossibilities.  Hanuman, takes Vishwaroop (a big form of himself) exhibiting his capabilities. – Exhibit your capabilities.

When Sita rejected his offer to take her along and asks Hanuman to bring Ram /Lakshman, Hanuman analyses the reasoning, ready to pay heed to the other persons reasoning and behaves with poise. – Listen to the other person

Hanuman – an Ideal Employee

This is inspired by Dr.Devdutts article on Hanuman –http://devdutt.com/articles/leadership/everybody-loves-hanuman.html

The facts about Hanuman as written by Valmiki. Hanuman is ambitious and noble. He even went ahead and wanted to swallow the Sun. Thats how he got the name Hanuman.

Hanuman was first a very devoted member of Sukreevas kingdom. He did everything to make sure his master sukreeva does not fail. When Ram and Lakshman entered Kishkinta, Sukreeva was frightened and it was Hanuman who went to enquire about them.

He was always a member of Vanara team. He did not allow any of his ego or his power to come in the way of his team. He enjoyed his life , playing along with other vanaras, respected everyone in the group and gave helping hands. He could have easily said that he is more powerful than Sukreeva and Angadha but he never did it.

Hanuman went to Lanka after promising that he will find Sita. He got demoralized when he could not find Sita in the palaces of Ravana. Then he boosted his morale thinking about Lord Ram and continued his search. When he met Sita, He respected her and offered to take her to Lord Ram at that instant itself. He was confident about his capabilities. But since Sita (an Ideal Wife) said that she should not be taken back stealthily but should be taken back in a rightful manner by Ram fighting out Ravan , Hanuman did not take her back that instant itself. Its a righteous thought.

Then after finding the whereabouts of Sita, he could have left Lanka. (that was what is asked by Sukreeva). But he goes that extra mile and tries to find out the strength of Ravana and his Troops. Thats why he destroys Ashoka garden and fights with the army of Ravana. And when he fights Indrajit and gets bound by Brahmastra , even when, by principle, the brahmastra effect on him is lost (because they tie him with another rope and brahmastra loses its powerr the moment you bind the person with the rope), he remained bound voluntarily, just because he promised Lord Brahma that he would remain bound to that brahmastra for 1 Muhurta. This exhibits devotion and his tendency to keep up his promises.

He commits mistakes , like, without much of a thought he burns down Lanka. But he prays to Lord Ram and hopes that Sita is not burnt down too.

When he returns back from Lanka, he could have directly gone to Lord Ram and conveyed the news. However he first meets his team (Vanara members). He discusses with them and Angada (Vanara minister) decides the course of action, celebrates the occasion and goes to Ram as a team and conveys the news as a team. This shows his tendency to celebrate achievements as a team and not bring the individualism before the larger good of the team.

Eventually, Hanuman during Coronation of Ram, gets awarded by sita the golden chain considering him as one of the best person. When Lord Ram was leaving the world and when Hanuman also wants to go along with him, Ram accorded the status of Chiranjeevi (being alive for the entire Kaliyug) , though Hanuman was entire upset about that, he just obeyed it and remains alive. It is believed that whenever some one reads Ramayan, Hanuman comes and sits near them to listen to it.

Take-aways for a moden day employee from this
1. its not always about exhibition of your own prowess. its a larger good that matters
2. Hanuman could have easily got carried away thinking that he is as powerful as Ram, but he knew his limits and accepted Ram as his ultimate superior.
3. When an employee is asked to do something, think about the rights and wrongs and go that extra mile. Knowing Indrajits prowess was very important in winning that war against Ravana by Rama.
4. Play as a team and do not bring in your own agenda ahead of the team.

Disclaimer : These are my personal opinion and does not necessarily reflect the views of my employer

Inside String Concatenation

n this post we will discuss about how the concatenation happens when you call string.concat() and how the concatenation happens when you call Stringbuilder.Append().

Here I have analysed it based on the Rotor .Net V2.0 code (some may not apply to .Net 1.1).

Generally, it is of the opinion that for any 2-4 strings use String.Concat() and for more than 4 strings use stringbuilder.Append(). The only difference that the string builder brings in is that it reduces the number of string allocations during the append operation , thereby reducing the GC effort.

How does a string concat work:-

If two strings are passed as arguments, it will find out the length of the resultant string, allocate memory for it and then copy these strings one by one.

If more than 4 strings are passed, it will consider it as an array of strings to be concatenated, spin through the array elements to find out the length of the resultant string. Then it allocates the memory for the resultant string and copies the strings one by one into that memory location.

For the C# code below:-

string s1=“hello”;
string s2=“world”;
string s3=” My”;
string s4=” Program”;

string result = s1+s2+s3+s4;

it generates the IL code as

IL_0001: ldstr “hello”

IL_0006: stloc.0

IL_0007: ldstr “world”

IL_000c: stloc.1

IL_000d: ldstr ” My”

IL_0012: stloc.2

IL_0013: ldstr ” Program”

IL_0018: stloc.3

IL_0019: ldloc.0

IL_001a: ldloc.1

IL_001b: ldloc.2

IL_001c: ldloc.3

IL_001d: call string [mscorlib]System.String::Concat(string,

string,

string,

string)

Now when the concatenation changes to

string result = s1+s2+s3+s4+s1+s2;

The IL code results in a call to the overload method of concat.

call string [mscorlib]System.String::Concat(string[])

This is a fastest way of concatenating strings. And thisworks perfectly fine for small number of strings. Then when do we go for stringBuilder.?

You should go for Stringbuilder when you are not sure of how many concatenations are going to happen. i.e., when the string concatenation is going to happen in a loop.

 

String Concatenation:-

String.Concat(string,string)

  1. The inputs are string_A, string_B;

  2. Total length of the result string = Length(string_A)+ length(String_b);

  3. Call Fast allocate string. What it does is take in the length and allocates memory in the heap and returns a string which is there in that memory.

  4. Then it calls the FillStringChecked function. It takes 3 parameters.

    1. The end result string

    2. Destination position

    3. Source string

  5. First string A is put in to the result. Then String b is copied into this result from the position length(StringA)

  6. This happens in the following way

String.Cs

 

fixed(char *pDest = &dest.m_firstChar)

fixed (char *pSrc = &src.m_firstChar) {

wstrcpy(pDest + destPos, pSrc, length);

}

pDest contains the address of the first character of the string ‘dest’.

pSrc contains the address of the first character of the string ‘src’.

  1. Here fixed key word is used so that , garbage collector does not move the string object when it is half way through the concat operation. This is called pinning and this can be used when the method is marked unsafe.

String Builder:-

String builder class represents a mutable string. It is convenient for situations in which it is desirable to modify a string, perhaps by removing, replacing or inserting characters without creating a new string subsequent to each modification.

What happens when you call the constructor of String Builder:-

stringBuilder strbConcatenation = new stringBuilder(string);

It allocates a memory space of 16 characters by default. However you can initialize the capacity of the string. The max capacity that can be allocated is 2147483647. ( this is nothing but Int32.MaxValue)

Now if the passed on string length is greater than the capacity then the capacity =capacity * 2. This ensures that there is a predicted growth rate. Then the Memory of that capacity is allocated. Then the input string is copied into the start of the new allocated memory and returns the reference to the memory space.

Now what happens when you call StringBuilder.Append(value) :-

Stringbuilder maintains the reference to the allocated memory space. Now it will find out if the value can be appended in place in the same memory space. If it can be allocated in the same memory space, then the wstrcpy happens.

What if the string builder does not have enough space to accommodate the newly concatenated string?.

In that case, it has to allocate a new memory space, copy the old string and also the new string. All this happens in the fixed regions of memory or pinned memory. This is the reason, that providing an optimal capacity when instantiating the string builder, reduces the number of new allocations. This will dramatically improve the performance as the number of garbage collections will be less.

This is more explicit from the SSCLI code

if (NeedsAllocation(currentString,requiredLength)) {

// here it allocates more memory and gets a new string.

String newString = GetNewString(currentString,requiredLength);

newString.AppendInPlace(value,currentLength);

ReplaceString(tid,newString);

} else {

// the append is carried out in place.

currentString.AppendInPlace(value,currentLength);

ReplaceString(tid,currentString);

}

 

Conclusion:-

  1. String builder gives an edge in the performance by the way that it is not going to make more Allocations often and deallocations.

  2. String concat is good for small string because the way it concats is pretty much faster when compared to string builder except for the memory allocation overhead and the time required for that memory allocation.