Which programming designs leads to more problems?

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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>For ws in Worksheets
remove duplicates
append remaining data to separate workbook
Next ws
</code>
<code>For ws in Worksheets remove duplicates append remaining data to separate workbook Next ws </code>
For ws in Worksheets
    remove duplicates
    append remaining data to separate workbook
Next ws

versus

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>For ws in Worksheets
remove duplicates
Next ws
For ws in Worksheets
append remaining data to separate workbook
Next ws
</code>
<code>For ws in Worksheets remove duplicates Next ws For ws in Worksheets append remaining data to separate workbook Next ws </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>For ws in Worksheets
remove duplicates
append remaining data to separate workbook
Next ws
</code>
<code>For ws in Worksheets remove duplicates append remaining data to separate workbook Next ws </code>
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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật