Code Smell 129 — Structural Optimizations

We love to improve time and space complexity by guessing not real scenarios

Problems

Solutions

  1. Cover your scenarios with tests.
  2. Write readable (and possible non-performant) code.
  3. Do a real benchmark with real user data. (No, iterating your code 100,000 times might not be a real use case).
  4. If you have conclusive data, you need to improve benchmark’s found bottlenecks using Pareto principle.
  5. Attack the worst 20% problems causing 80% bad performance.

Context

Sample Code

Wrong

for (k = 0; k < 3 * 3; ++k) {
i = Math.floor(k / 3);
j = k % 3;
console.log(i + ' ' + j);
}
//This cryptic piece of code iterates a
//two dimensional array
//We don't have proofs this will be useful
//In real contexts

Right

for (innerIterator = 0; innerIterator < 3; innerIterator++) {
for (outerIterator = 0; outerIterator < 3; outerIterator++) {
console.log(innerIterator + ' ' + outerIterator);
}
}
// This is a readable double for-loop
// 3 is a small number
// No performance issues (by now)
// We will wait for real evidence

Detection

Tags

  • Premature Optimization

Conclusion

Relations

More Info

Credits

--

--

I’m a senior software engineer specialized in declarative designs. S.O.L.I.D. and agile methodologies fan. Maximilianocontieri.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Maximiliano Contieri

I’m a senior software engineer specialized in declarative designs. S.O.L.I.D. and agile methodologies fan. Maximilianocontieri.com