lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Petite Abeille wrote:
A bit off topic, but... out of curiosity, I was wondering about the use of 'while true do' + 'if not chunk then break' vs. 'while aChunk do' + 'aChunk = aFile:read()', e.g.:
All things consider, I would rather use an iterator anyway, but perhaps that's just me:

local aReader = function() return aFile:read( 16384 ) end
for aChunk in aReader do
    aHash:update( aChunk )
end

Yes, I like your iterator version best.

But I do prefer the 'while true do .. break .. end' construct over multiple :read() calls. When things are repeated, they often become broken later. I've been bitten by the latter construct in more painful ways than I have the former. This is mostly because an infinite loop almost always shows up earlier and in a more obvious way than an interaction caused by some subtle linking between the two repeated lines (especially when one is later changed without the other). Those sorts of bugs look like they work but miss the last read, say, under some strange circumstance.

Of course in this limited example, it doesn't really matter. Either way is clear and readily understood.

Doug