Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
2  
using VBA for Excel leads to more problems... ;) –  Steven A. Lowe Aug 7 '12 at 3:05
1  
Lol - well I was using it here as an example for other languages too. P.s. it would be good if people explained why they downvoted my question (downvoting is ok, but please remember to explain as I am a beginner) –  nebffa Aug 7 '12 at 3:47
    
Hi @nebffa, I didn't downvote your question, but your question has 3 close votes that are because "As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or specific expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, see the FAQ for guidance". I would guess that the downvotes are because some users see this as "soliciting debate, arguments, polling, or extended discussion". –  Rachel Aug 7 '12 at 14:51
    
Thanks @Rachel, I suspected it might be as such (I have used more of stackoverflow.com than programmers.stackexchange). Sorry about asking a discussion-type question. In future - if I want to ask questions of this type, where should I go??? At this stage in my development I am having lots of these questions. I am continuously practicing as well as reading Code Complete v2 which has been extremely useful by even chapter 6, but sometimes it is good to ask questions to people - not just a book. –  nebffa Aug 8 '12 at 4:03
    
@nebffa If you're looking for discussions with other programmers, I typically recommend chat. Sometimes its quiet there, but your question will usually get answered eventually. Both Programmers and SO have chat rooms - The link above is to the main Programmers chat room –  Rachel Aug 8 '12 at 11:18
show 1 more comment

3 Answers

up vote 2 down vote accepted

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.

share|improve this answer
    
Thankyou - I have accepted your answer because it is the one that strokes my ego and makes me feel right. Ok I'm joking. Your answer explains what it will be like in the future as a developer, which is what I was looking for (tskuzzy's answer was good but lacked this part). Thankyou very much for your answer –  nebffa Aug 7 '12 at 3:13
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.