Ask HN: What do you do before you start writing code?
7 points| mpblampo | 8 years ago
On a couple of recent occasions, I've written a bunch of code, only to have my colleagues correctly point out some decisions I'd made in writing it that made the code more complicated, and harder to reason about, than necessary (e.g. which logic should belong in which function, or what data structure I should use for something). I'm sure that this has amounted to a lot of wasted time. If I could have foreseen these decisions before writing code, or even just realized while writing code that I was making decisions that I ought to stop and think about, I wouldn't have made these more complicating choices, and wouldn't have had to spend all this time reworking my code.
(Of course, if I can learn a good lesson from this, the time will not have been wasted. Hence this post.)
The thing is: We'd spent significant time planning the structure of this project, with (I'd thought) clear documents and diagrams explaining what components we needed and how they would interact. When I started coding, I felt pretty well prepared, and confident that the thing would basically write itself.
So, HN: What steps do you take before setting out to write code? Are there principles that you use to decide that your plan/design is sufficiently detailed?
And, assuming there will be cases when the plan does not anticipate all design decisions: When you're coding, how do you avoid making decisions that will waste a lot of your time?
Smudge|8 years ago
Secondly, the best way to avoid making painful design decisions is... to make them enough times that you instinctively remember them. There aren't many shortcuts to developing that instinct, so take this opportunity to try and generalize your learnings -- what do you recognize now that you didn't see when you started the project? A healthy amount of retrospection will make you a better engineer, and you'll also get better at identifying uncertainties and risks before you start coding.
Lastly, up-front planning and design documents can only get you so far. They're important, sure, but at some point you'll be down in the nitty gritty details, and you'll need to make unanticipated course corrections. As you gain experience you'll start being able to fill in more of those gaps on your own, but until then, you'll want to loop in other engineers more frequently.
One of the qualities of a senior engineer is that they act as a force multiplier for their team. So as a relatively junior engineer, don't be afraid to make use of your senior engineers! They should be there to support you and make you better, beyond just helping you plan out a project. If possible, pair on some of the trickier parts with them, so that you can see how they'd approach the problem and start picking up on things that might not seem so obvious to you right now.
Hope that helps!
itamarst|8 years ago
Here's vague model of what I do:
1. Figure out what's really important to achieve: ask why you're doing something, what the constraints are. Figure out if you really have best solution, maybe there's different way to solve real problem.
2. Sketch out a design, in my head or in writing depending on complexity.
3. Figure out riskiest assumptions.
4. Try to build smallest possible end-to-end proof-of-concept implementation. This will validate or invalidate assumptions, and usually influence design in some way.
5. Start on real coding.
(If you'd like to learn from mistakes I've made over the years, in design and testing and writing software, I write a weekly email with a mistake and what I learnt from it: https://softwareclown.com)
wdiamond|8 years ago