DEV Community

loading...

Discussion on: Write better code and be a better programmer by NEVER USING ELSE statements

Collapse
lonelymoon profile image
Hung Luong

I don't find the else worth targeting for the sake of readability. In fact, I welcome else as an explicit sign of "this or that" logic.

Guard clauses are fine. Nested conditionals are another problem entirely.But I don't think breaking a simple function with 3 parameters into at least a combinations of small function with a long chain of get by this and by that is anymore readable.

Collapse
drmdodd profile image
DrMDodd

It does however make you think about reusability and keeping consistency in code, in the getCharities example there, is something that could be reused elsewhere.

Collapse
lonelymoon profile image
Hung Luong

Sorry, but I don't see why reusability is relevant here - when it was suggested to create multiple 'versions' of getCharities with different combinations of parameters to avoid branching. One could even argue that it is the exact opposite of reusability: duplication.

Unless we have wildly different logic for getting by country or tier - in which case it isn't even about branching as they would be two different functions in the first place, what we would most likely have is this:

getCharitiesByCampaignAndCountry -> return getCharities(country, campaign, defaultTier)

Why make the reader jump an extra hoop just to get to the actual logic? And even if we were to force reusability on this, what is more reusable: getCharities or getCharitiesByCampaignAndCountry?

I know the example are meant to be simple, sometimes contrived, but this is precisely why I don't see why eliminating else helps in any way with readability.