I am a hobbyist programmer and am trying to improve my skills. To that end, I have been reading 'The Pragmatic Programmer' and 'Code Complete' as recommended by some in this forum and I have to say that it has improved my approach and skills. That said, one aspect of programming I am struggling with is the 'Don't Repeat Yourself' (DRY) mantra espoused in 'The Pragmatic Programmer' and echoed in Code Complete. Let me explain.
I recently wrote a small (~2500 lines of code) software program for a friend. Armed with my new knowledge from the books I have been reading I resisted the temptation to write code straight away but instead took some time (~3 days) to design my software in an abstract way e.g. what does the software need to achieve, what does the end user require etc.
Having done my design I then wrote down the the core functions / procedures that I would need to deliver the software e.g. IsFileOpen(), getEmployeeName(), WriteAddressToDatabase() etc. I felt pleased with this and happily set on my way coding.
Half way through my coding I realised that there were functions / procedures that I required that weren't in my initial draft. On the fly I would add these functions / procedures. However, after a day or so I'd realise that the new functions / procedures that I had added had a lot of code repetition. So I'd try and make a generic function / procedure with additional input parameters to handle the nuances of different situations.
What struck me is that this process is almost an infinite loop i.e. you can just go on and on condensing code to remove repetitions. Also, it seems to me very difficult to plan so thoroughly in advance that you know all the functions / procedures that you will need and remove all the repetitions before hand. My questions are:
- Am I really just not doing enough initial planning of my software design and this is why I keep having to add new functions / procedures?
- Or is this a common experience that you just get better at over time?
- There seems to me a contradiction between 'The Pragmatic Programmer' and 'Code Complete'. The former suggests the 'DRY' principle whereas the latter seems to suggest building functions / procedures that only do one thing i.e. don't build functions that return two or three different things. So, as an example, suppose I have a data table with three variables ('Employee Name', 'Age', 'Salary') and I want to retrieve each one at different times. Then 'Code Complete' says build three functions [ getEmpName(), getAge(), getSalary() ] to keep each function specific whereas 'The Pragmatic Programmer' says there is repetition in that code and one should build a single function getEmpDetails(Name, Age, Salary) which would return the variable you want.
For clarity, I am only half way through 'Code Complete' so maybe there is an answer to resolve the contradiction later in the book. Can anybody give me some guidance please.
Alex