DEV Community

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

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.