DEV Community


Discussion on: Writing Clean Code

dvanherten profile image
Dave van Herten

The initial example really separates the 3 ways you can "view".

The shorter example still does that, but the condensing makes it harder to separate the content IMO. For instance if the 3rd line was a bit longer that the if check needed two lines, it might require some more mental power to keep things separate in your head. First way wouldn't have had this problem as much.

That said, to support at a glance, what does this do, I may opt to pull the conditions into their own functions such that the check becomes:

return IsPublic(...) || IsAdmin(...) || IsOwner(...);

At a glance I can tell what my conditions are for when I canView. It captures essentially the requirements in very plain english. If I care about how those rules are defined I can look into each one explicitly to find what I might be looking for.