Re: Readability question.
Francisco Olarte <[email protected]> Sun, 7 Jun 2026 13:22:46 +0200
| Newsgroups | gmane.comp.lang.lua.general |
|---|---|
| Message-ID | <CA+bJJbxY91ts+j=zzTvAew_LG3qSKx=u81gjji9GWSOKaS2=YA@mail.gmail.com> |
Sean: On Fri, 5 Jun 2026 at 22:11, Sean Conner <[email protected]> wrote: ... > I would write this as: > > if a then > if b then > return combine(a,b) -- a b > else > return a -- a /b > end > elseif b then > return b -- a /b > else > return -- /a /b > end > > I personally don't like comparing directly against nil unless 'false' is a > valid thing to return (for instance, a = false b = nil). Yep, I did not clarify it, but when thinking on packing it in a "guard" func this was specifically the case, as I have lots of tables where I must distinguish false from absent. Were not this the case I probably would have gone your way, positive testing first, which I use normally except when trying to avoid deep nesting, typically in error cases. if error_condition_ then > But that aside, > this reads better to me as if we have an a and not a b, we return a. I've > marked the four cases this covers (where /a means "not a"). For just-falseness, this is just the case. My version, nilness of falseness versions, can have the if swapped to do it this ways. The reason I tested for invalid a/ return b first, unexplained again, if that for my usual combinator cases I many times start with nil initial values, so I went for what I thought faster. Anyway, if we have not a, b I think my version skips first return, returns a in second. As I tested nils, returns will be equal. And yes, for the full library version I would go for your solutions or some of the variants, I think I wrote some too, with double nested if-then-else, as I need 4 returns ( In fact your version, switching conditions to ~= nil ( which I dislike, for being a negative test and someone changing the default keyboard handling for ES to ~ being a modifier which leads to uncountable ≃ being put by me in the source code ( maybe useful for other language, but in spain we only use it for ñÑ and we have a dedicated key for it ) ) lends itself well to make use of the implicit return at function end, which to me is pleasing ( nothing to combine, do nothing and fall of, but just is weird and personal ). Francisco Olarte. -- You received this message because you are subscribed to the Google Groups "lua-l" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion visit https://groups.google.com/d/msgid/lua-l/CA%2BbJJbxY91ts%2Bj%3DzzTvAew_LG3qSKx%3Du81gjji9GWSOKaS2%3DYA%40mail.gmail.com.