Properties of Code: Safe refactoring without tests

Matthew Carlson
Jan 2, 2018 · 7 min read

Identity Properties

a + 0 = 0 + a = ab(1) = 1(b) = b

Adding a Local Variable

void foo(){    bar(a + b + c);}
void foo(){    var d;    bar(a + b + c);}

The Associative Property

a + (b + c) = (a + b) + ca * (b * c) = (a * b) * c
a * (b + c) != (a * b) + c

Introduce Explaining Variable

void foo(){    var d = a + b;    bar(a + b + c);}

The Substitution Property

if a = bb + c = a + c

Introduce Explaining Variable cont.

void foo(){    var d = a + b;    bar(d + c);}

Rename Variable

void foo(){    var totalPrice;    var d = a + b;    bar(d + c);}
void foo(){    var totalPrice = a + b;    var d = a + b;    bar(d + c);}
void foo(){    var totalPrice = a + b;    var d = totalPrice;    bar(d + c);}
void foo(){    var totalPrice = a + b;    var d = totalPrice;    bar(totalPrice + c);}
void foo(){    var totalPrice = a + b;    bar(totalPrice + c);}

Conclusion

Notes

References

Matthew Carlson

Written by

Sr. Consultant — Industrial Logic

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade