Wednesday, January 16, 2013

Working with Metals & the Source Code

I'm a Mechanical Engineer & have always loved studying Metallurgy during my studies & profession. I'm writing software for Engineering Applications for about an decade now. There are interesting parallels between working with code & the metals. Computer scientist drew many inspirations from Metallurgy in the form of algorithms like Simulated Annealing, however this post is more about some simple stuff.

Here are a few most important properties of metals:

  • Strength --> Maximum stress a metal can withstand before fracture

  • Ductility --> Ability of a material to deform under tensile load
  • Hardness --> Ability to withstand surface indentation
  • Fracture Toughness --> Impact Energy absorbed by unit area before fracture



Strength matters to the code as well. Just like metals, the code is subjected to various forces. Forces of changing user requirements, changing hardware & software environment, adopting to different third party environment & so on. Not to mention, the pressure from investors to deliver QoQ profits and managers trying to squeeze everything in tiny budget & time!

Here is how the Stress-Strain curve looks like: (taken from Wikipedia)



What happens when you want to stretch a thin wire out of a metal - say Aluminium or Steel?


As the metal gets stretched (& so the code), it starts developing internal stress. The original design (Lattice structure) though gets deformed & strained, is able to absorb the force of changing requirements & environments.

As we stretch along, it becomes harder & harder to stretch it further. This phenomenon is called Strain Hardening. Software was not designed to accommodate unanticipated changing conditions. The grain boundaries between various interacting components resist the ductility.

A point is reached when its extremely hard to work with it & it starts breaking. A ductile code shows early signals before failure, while the brittle code & the metals fail abruptly!

So what do you do? We need wire to be stretched & sheets to be hammered. 
Our investors want to continue to make money from the software & that's how we get our salary cheque! Customers want us to produce more usable features & our managers want all these in the modest budget!  

Yes, there is a way to go forward - Heat Treatment! We put the metal into a furnace, heat it & cool down at a controlled slow rate. If its a steel, we heat it something beyond called as "Austenitizing Temperature". This is the temperature at which steel transforms into "Austenite" - from  body-centred cubic (BCC)  to face-centred cubic (FCC). With this new design of internal structure, it is further able to withstand the tensile forces without getting fractured.

Can we do this with code? Can we change its internal structure so that it can be further worked upon for some more time? I wish I could put the code into some furnace & heat it!
Indeed there exists such a furnace - that's Refactoring Furnace!

Malleability of steel & its ability to get heat treated depends mainly on its constituents called "Alloying elements". Steel is primarily made of Iron & Carbon, but it also contains alloying elements like Mn, Ni.

Our Refatoring Furnace can heat treat our code & improve its internal structure so that it can be stretched & hammered further. But its not possible without adding alloying element. We need some alloying element that would make easy to heat treat the code. That alloy is called "Testability & Tests"!

Your code is working, but do you think its badly designed? Do you have fear to change it further? And are you getting dragged because of enormous amount of Technical Debt carried over? Don't loose heart! You can still refactor it. You just need to add this alloying element called "Testability". Thanks to "Michael Feathers" for his beautiful book "Working Effectively with Legacy Code".






No comments:

Post a Comment