3 ms·
I like the philosophy that “exceptions are for exceptional circumstances.” Not being logged in is not exceptional.
by jagged-chisel 3y ago
I like the philosophy that “exceptions are for exceptional circumstances.” Not being logged in is not exceptional.
- _ZeD_ 3y agofor a function called "getUser" it is.
- withinboredom 3y agoNot exactly. "getUser" not having a user to get is not exceptional unless you only have logged in users. If you have logged-out users, then "getUser" should gracefully handle the case of an unknown, logged-out user (either returning null or some other sentinel value).
- _ZeD_ 3y agoReturning null makes "getUser" a "maybeGetUser" function. Like said in TFA that means that any caller might need to check for nullness
- withinboredom 3y agoYes, no matter what, they have to check if there is an actual user if the application can have anonymous users. There’s no way around that.
- Lvl999Noob 3y agoWhat the exceptional case is depends on the what the pre- and post-conditions of the function are. If a function assumes that the user is logged in then the user not being logged in is indeed exception. Not to say that it is good design though. That function is quite fragile like this. If it must assume that a user is logged in, then it could easily require a user to be given as an argument which will remove the whole possibility.