|
Marc Clifton wrote: The vast majority of examples are related to logging which becomes irrelevant when you use a good messaging architecture which can centralize the logging
Perhaps we have a different definition of "logging" but what happens when the messaging service itself fails?
|
|
|
|
|
It is clear that your VerifyToken should only do that job. I would write the if and else block, because that is clean code. DRY has to step back.
Another solution is to overload the the VerifyToken function, but it can get weired.
Press F1 for help or google it.
Greetings from Germany
|
|
|
|
|
KarstenK wrote: It is clear that your VerifyToken should only do that job. I would write the if and else block, because that is clean code. DRY has to step back.
On that I agree as well, though I much rather like the idea that all these requests go to a specific pre-route handler that does the validation.
Marc
Latest Article - Create a Dockerized Python Fiddle Web App
Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny
Artificial intelligence is the only remedy for natural stupidity. - CDP1802
|
|
|
|
|
Name it VerifyTokenX and you are out of Problem
Bruno
modified 19-Jan-21 21:04pm.
|
|
|
|
|
I'm all for the SRP (Single Responsibility Principle, now you know the acronym ), but you can take it too far.
Like my former "technical director" who heard about it from me and then told me how to implement it.
The result was a whole lot of single method classes that, I'll give him that, did only one thing
At some point in your code you just can't help but doing multiple things.
The only thing you can do is to minimize it and when you do multiple things at least make it things that belong together, like checking authentication and returning a response.
It's like "purely functional" languages, like Haskell. At some point they have to have side-effects, but they wrap it in a single function and keep the rest of the application "pure".
|
|
|
|
|
Hmm. For my money, I'd prioritize DRY (Don't Repeat Yourself) and SR (single responsibility) based on which option resulted in less code.
If you've got 100 discrete calls to VerifyToken() that you'd have to add else { error-handling-stuff; } to, I'd leave the error handling where it is.
On the other hand, if you've only got a couple calls to VerifyToken() , but the error handling is different for each case, then obviously you don't want it inside the function.
Software Zen: delete this;
|
|
|
|
|
It's understandable that you wrote VerifyToken that way. Seems logical and "Clean" - you're not repeating yourself. But at the cost of introducing a dependency on 'IContext' in VerifyToken that has nothing to do with it's job. So, in my way of thinking, that's not "Clean". Consequently you can repeat yourself or find another way, maybe more correct, to report the error.
Another option is to refactor the code a bit. Have one function VerifyToken() and have another VerfyTokenAndRespondIfBad(). The second would call the first.
#SupportHeForShe
Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson
You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun
|
|
|
|
|
Just throw an exception if the token validation fails, catch it and send your error response in the catch block.
simples
Your VerifyToken() method at the very least should be renamed RespondWithErrorOnInvalidToken() - which will avoid the mistake you made...
PooperPig - Coming Soon
|
|
|
|
|
Call Accenture in for a consultation.
Aren't they the experts in Best Practices that management relies on?
|
|
|
|
|
Marc Clifton wrote: "a method does one thing only" (whatever the acronym for that is)
There's no Acronym for it I know of, it's simply the "Single Responsibility" principle and thus the S in the SOLID principles of object oriented programming. It goes for the whole class though not just for the method itself. It's essentailly the UNIX philosphy[^] either way.
In your special case you really might want to look into the "OData and Authentication" Articles [^] to give you a slightly different approach into authentication of some sorts or rather: a different place to hook up your authentication (assuming that's what you want to use the token for).
But coming back to the topic, there's many priciples like DRY, KISS, YAGNI, SOLID and they all make sense and are perfectly clear when it comes to a clean and maintainable program that's rather self-documenting. And it is definitely worth it to stay true to these priciples for as long as you can. They are however still principles and not rules and you might not always, or rather can't always follow them as closely as you might want to. Especially if you factor in time constraints you usually have if you develop an application in a business environment.
In the end it is a real shame, since they exist for a reason. 
|
|
|
|
|
|
I call time travel shenanigans
|
|
|
|
|
There meant to be.
If you do it, you love it. If you love it, put a pride with it (DON!).
|
|
|
|
|
Met my doppleganger. Too bad for him only one Most Handsome Man in the World can exist. 
|
|
|
|
|
|
Don't worry - the UK still hasn't fully converted either.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Bottles hold ml, beer comes in pints.
Food is sold by the Kg, but people are weighed in stones and pounds.
We buy cloth by the m, but drive by miles.
That's pretty much metric, right there - Human stuff in human units, manufactured stuff in metric (except some of the beer, obviously).
And when will the world adopt the metric DateTime?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
OriginalGriff wrote: Food is sold by the Kg,
But everyone over 35 still complains about that, and has to mentally convert it to pounds and ounces.
OriginalGriff wrote: metric DateTime?
Obligatory xkcd[^].
But it still takes nine paragraphs to explain the difference between "12:00 AM" and "12:00 PM":
12-hour clock - Confusion at noon and midnight - Wikipedia[^]
Maybe we should all switch to decimal time[^]?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Looking at the current situation, I would like to have my weight in Euros, not Pounds.
|
|
|
|
|
That could be a stone around your neck!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Do you mean a mill-stone, or 14lbs?
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack.
--Winston Churchill
|
|
|
|
|
A milli-stone, or possibly microstone.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
Some people I see around, should be weighed in KiloStones!
Get me coffee and no one gets hurt!
|
|
|
|
|
You know we wraiths don't like to get wet[^]. Still, even in the worst case we only need to get ourselves a new black shroud and something to ride.
|
|
|
|