Click here to Skip to main content
15,892,005 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Why I hate "Friendly" Pin
Fabio Franco19-Jul-17 2:23
professionalFabio Franco19-Jul-17 2:23 
GeneralRe: Why I hate "Friendly" Pin
ClockMeister19-Jul-17 1:44
professionalClockMeister19-Jul-17 1:44 
GeneralRe: Why I hate "Friendly" Pin
Slow Eddie19-Jul-17 3:17
professionalSlow Eddie19-Jul-17 3:17 
GeneralRe: Why I hate "Friendly" Pin
W Balboos, GHB19-Jul-17 3:23
W Balboos, GHB19-Jul-17 3:23 
GeneralRe: Why I hate "Friendly" Pin
agolddog19-Jul-17 4:20
agolddog19-Jul-17 4:20 
GeneralRe: Why I hate "Friendly" Pin
TNCaver19-Jul-17 6:22
TNCaver19-Jul-17 6:22 
GeneralThought of the Weak Pin
MacSpudster18-Jul-17 4:14
professionalMacSpudster18-Jul-17 4:14 
GeneralWhen Worlds (or best practices) Collide Pin
Marc Clifton18-Jul-17 3:33
mvaMarc Clifton18-Jul-17 3:33 
I try to follow the best practices of DRY (don't repeat yourself), "a method does one thing only" (whatever the acronym for that is), and good naming conventions. Sometimes, they conflict. For example, I have a method for a web service that verifies an authentication token something like this:
VerifyToken(IContext context, Guid token)
{
  bool ok = token == session.AuthenticationToken;
  if (!ok)
  {
    RespondWithError(context);
  }
  return ok;
}

So, this method actually does two things -- it verifies the token and calls the error response if authentication fails. Oops -- fails the "do one thing only" test, and not the best name. But why? Because in my web service handlers, of which I have several, I have things like:
Handler(IContext context, RequestData data)
{
  if (VerifyToken(context, data.Token) {...happy response...}
}

The idea being, I didn't want to repeat myself with every handler, which would then look like this:
Handler(IContext context, RequestData data)
{
  if (VerifyToken(context, data.Token) 
    {...happy response...}
  else
    {...not so happy response...}
}

ok, so one best practice suffers to meet more of the goals of the other best practice. And perhaps I'm taking DRY too far.

And of course, this bit me yesterday, because I couldn't figure out why I was getting all sorts of bizarre errors, like connection already closed, content type already set, bytes being sent differs from content length, etc. It was really quite random. Turns out, on a new service call I was adding, I had written the second form (because I forgot that VerifyToken handled a bad token response, and on a bad token, both VerifyToken and my service handler were trying to write to the response stream, and depending on the state, it was already closed, etc., being that the response is handled on a separate thread.

So to help me not make that mistake again, I renamed the method to VerifyAndRespondIfTokenBad. Sigh | :sigh:

Then again, you might argue that the router should have handled the token validation for these particular service calls, and indeed, that would truly resolve the DRY issue, as the handler itself wouldn't even need to call VerifyToken as it should be guaranteed to be a valid request by the time it gets to the handler. And I agree, but that'll take more refactoring. Smile | :)

None-the-less, I found it an interesting case study, and in particular, how when thinking through the best practices, while one can come to the ideal solution, various compromises are often made.

And if you are going to complain about how bad Guid authentication is, don't -- these calls are being made between two servers over HTTPS which are running my code, and the likelihood of someone guessing the Guid is pretty darn small, unless there's something I don't know, which is definitely possible!

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

GeneralRe: When Worlds (or best practices) Collide Pin
Jacquers18-Jul-17 3:53
Jacquers18-Jul-17 3:53 
GeneralRe: When Worlds (or best practices) Collide Pin
Marc Clifton18-Jul-17 4:18
mvaMarc Clifton18-Jul-17 4:18 
GeneralRe: When Worlds (or best practices) Collide Pin
Jacquers18-Jul-17 4:21
Jacquers18-Jul-17 4:21 
GeneralRe: When Worlds (or best practices) Collide Pin
Marc Clifton18-Jul-17 4:45
mvaMarc Clifton18-Jul-17 4:45 
GeneralRe: When Worlds (or best practices) Collide Pin
Jacquers18-Jul-17 4:56
Jacquers18-Jul-17 4:56 
GeneralRe: When Worlds (or best practices) Collide Pin
Marc Clifton18-Jul-17 5:03
mvaMarc Clifton18-Jul-17 5:03 
GeneralRe: When Worlds (or best practices) Collide Pin
jschell19-Jul-17 8:02
jschell19-Jul-17 8:02 
GeneralRe: When Worlds (or best practices) Collide Pin
KarstenK18-Jul-17 4:32
mveKarstenK18-Jul-17 4:32 
GeneralRe: When Worlds (or best practices) Collide Pin
Marc Clifton18-Jul-17 4:48
mvaMarc Clifton18-Jul-17 4:48 
JokeRe: When Worlds (or best practices) Collide Pin
User 1106097918-Jul-17 5:13
User 1106097918-Jul-17 5:13 
GeneralRe: When Worlds (or best practices) Collide Pin
Sander Rossel18-Jul-17 5:50
professionalSander Rossel18-Jul-17 5:50 
GeneralRe: When Worlds (or best practices) Collide Pin
Gary Wheeler18-Jul-17 6:24
Gary Wheeler18-Jul-17 6:24 
GeneralRe: When Worlds (or best practices) Collide Pin
TheGreatAndPowerfulOz18-Jul-17 6:32
TheGreatAndPowerfulOz18-Jul-17 6:32 
GeneralRe: When Worlds (or best practices) Collide Pin
_Maxxx_18-Jul-17 17:10
professional_Maxxx_18-Jul-17 17:10 
GeneralRe: When Worlds (or best practices) Collide Pin
Vivi Chellappa18-Jul-17 21:14
professionalVivi Chellappa18-Jul-17 21:14 
GeneralRe: When Worlds (or best practices) Collide Pin
Jirajha18-Jul-17 22:52
professionalJirajha18-Jul-17 22:52 
GeneralIdentical Looking Strangers Seated Together on Plane; Same Hotel, Too Pin
ZurdoDev18-Jul-17 3:05
professionalZurdoDev18-Jul-17 3:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.