Thursday, January 17, 2013

Working with Metals & the Source Code - III

Now, let's talk about something more interesting - Case Hardening.

Hardness is an extremely desirable property of most mechanical moving components. If gears of transmission system of your car are not hard, then they will wear out quickly. Making the steel hard; is not that hard. Increasing amount of carbon & other alloying elements increase the hardness, as well as its grain structure which can be controlled by proper heat treatment.

However Hardness comes with a gift - Brittleness. Typically hard components have low toughness. They fail in brittle manner, without giving proper symptoms before failing. We cannot afford to break a gear!

So what we do? One who invented Case Hardening must be very innovative. We combine high-carbon hard material with low-carbon ductile one into a single component! There are various ways to do it - one is carburizing. Heat the steel component in a furnace that has controlled high carbon environment. Material absorbs the carbon, but the depth of its penetration is very carefully controlled. As a consequence; component becomes harder only at its surface up to certain case depth, while its core remains soft.

What does it mean?

When a case hardened component is subjected to impact, it initiates a crack - as the case is brittle. However the soft core doesn't allow the crack to propagate. That helps component not to fail.

Why are we talking about this, which everyone knows already? Well, just want to share an interesting analogy, though not a perfect one. Mechanical components require hardness as they need Wear Resistance. Software doesn't wear after use, but the other part of crack propagation is important.

When certain component (say library or class or function etc) fail due to unanticipated impact force such as Bad Input, we don't want that crack to propagate throughout the software. We want our software to detect & stop it. Earlier it detects, easier it can handle it. Even if it fails, it fails elegantly giving useful information to the users & developers of the software.

Yes, we are talking about Assertions.
If I have to say two most powerful tools I use in writing code, then they are (1) Commit Early, Commit Often AND (2) ASSERTs.

Asserts detect the crack right at the point where it is formed. It fails fast. This tool is useful not only while writing code, but also in personal life. After all, code reflects personality of the developer, it cannot be separated. Well, this is another interesting thing that we will talk when we see in person.

No comments:

Post a Comment