As a fresh programmer, one of the first thing for me to learn was to learn language and its syntax. Now my next training issue is to design my code heirarchy in such a way it is simple to maintain over time. One example I just encountered is below. It is in VBA for Excel but the concept extends to other languages too.
As someone with very little experience, it seems to me that the 2nd option is much clearer and I should use that style. However, I know sometimes that to a beginner programmer what seems like a relatively benign choice can end up hurting their development in the long run.
Are either of these two styles the better? I am not talking about personal ‘stylistic’ choices, I am talking about which one leads to more problems down the line, likely through over-complicating things.
For ws in Worksheets
remove duplicates
append remaining data to separate workbook
Next ws
versus
For ws in Worksheets
remove duplicates
Next ws
For ws in Worksheets
append remaining data to separate workbook
Next ws
Thankyou!
EDIT: By the way, it is not important if the less-readable design style is faster. A small increase in performance is not worth a loss of readability for my purposes at the moment.
6
As a developer you will come across this situation all the time – I have a problem with two solutions. Both appear to solve the problem, both are of similar complexity, there is no obvious reason to choose one over the other (patterns, practices, personal preference, etc). Which should I do?
The problem with the question you have posed above is that there is not enough context to make an assessment. For example, are there any external business or functional requirement that may help. ie. must all duplicates be removed before data can be appended? If there is a yes to this, then the first solution is the correct one because the second fails the requirements. Alternatively it may be that changes should only be made to worksheets, when additional data is about to be added so that any failure will leave the maximum number of worksheets in their original condition. In this case, the second solution is the best and the first fails.
If there is no such requirements to be factored in then the answer is a purely personal preference. Mine would be the second option. There is no difference in terms of stability or speed between the solutions.
1
I’m not terribly familiar with VBA, but at a glance it seems this syntax would be preferable in every way:
For ws in Worksheets
remove duplicates
append remaining data to separate workbook
Next ws
For one thing, it seems quite readable to me.
For another, you’re doing one loop, not two.
When programming, you want to go with the simplest approach possible while still having sufficient robustness.
The former approach is simpler, while the latter is more robust. If you have good reason to believe that the second action might potentially be decoupled with the first one in the future, then go for the latter. Otherwise, go with the simpler approach.