Queen’s attack: Over optimization and Over Engineering
Problem statement
Problem statement from hackerRank
“A queen is standing on an n x n chessboard. The chess board’s rows are numbered from 1 to n, going from bottom to top. Its columns are numbered from 1 to n, going from left to right. Each square is referenced by a tuple, (r, c), describing the row, r, and column, c, where the square is located.” …
So we are left to decide and build the correct algorithm to help our beloved Queen to travese the board.
The algorithm
A naive but in practice good implementation could lead us to think that we have to traverse the all board to count for each attacking position:
We started traversing at queen.row + 1 position, and if not obstacle is found we continue our journey and increase the counter by 1. We keep doing so until one of two things happens: found and obstacle or reach the border of the chess board.
So we can on and makes the final implementation by just adding a for loop in each direction, right?
But how about if we can think of a faster way to get the result?
The optimization
Instead of looping over the board and then looping again through the obstacles array. I though at first find the closest obstacle in each direction could make it run faster.
Besides being a lot more harder to read, the gain in performance was not good enough. So maybe the more straforward solution is always the best. Keep it simple enough you can get through the code with your friends and mates.
Over optimization
Over Optimization and over engineering are two common problems that can occur when developing software. Over Optimization is when a developer spends too much time and effort optimising a piece of code, even though the performance gains are not significant. Overengineering is when a developer builds a system that is more complex than necessary, often in an attempt to future-proof it.
What are the risks of over engineering and over optimization?
- Increased costs: Over engineered systems and products are often more expensive to develop, maintain, and operate.
- Longer development times: Over engineering can lead to longer development times, as engineers spend more time designing and building complex systems.
- Difficulty in maintaining and evolving: Over engineered systems and products can be difficult to maintain and evolve, as they may be complex and difficult to understand.
- Reduced usability: Over optimized systems and products may be less user-friendly, as they may be too complex or difficult to use.
- Reduced flexibility: Over engineered systems and products may be less flexible, as they may be difficult to adapt to changing requirements.
The conclusion
Keep it simple. Addy Osmani stated not long ago:
Stick to boring architecture for as long as possible