Computer
FAA Fines SpaceX for Launch Violations, Company Fires Back with Lawsuit
Read more of this story at Slashdot.
CodeSOD: A Managed Session
Some time ago, Roald started an internship on a ASP .Net application. It didn't take long to find some "special" code.
public string RetrieveSessionString(string sessionName) { try { return Session[sessionName].ToString(); } catch (NullReferenceException) { return null; } }The Session variable is a session object for this user session. Each request carries a token which allows us to pair a Session with a user, making a cross-request per-user global object. That is what it is- but it's weird that we call the parameter sessionName. Maybe that's just a bad parameter name- it might be better called sessionKey or something like that.
Of course, the real issue here is it's null handling. Calling ToString on a key that doesn't exist throws a NullReferenceException, so we handle it just to return a null, thus making future NullReferenceExceptions somebody else's problem. Arguably, an empty string would be a better behavior. Still, I hate it.
But Roald also found this function's evil twin:
public Dictionary<string, string> RetrieveSessionDictionary(string sessionName) { try { return (Dictionary<string, string>)Session[sessionName]; } catch (NullReferenceException) { return null; } }This is the same function, but instead of fetching a string, it fetches a dictionary of string/string pairs. It does the same null handling, but notably, doesn't do any error handling for situations where the cast fails.
And suddenly, this makes more sense. They're using the word "session" in two different contexts. There's the Session- a series of HTTP requests sharing the same token- and there's a user's session- settings which represent a unit of work. They're storing a dictionary representing a session in the Session object.
Which leaves this code feeling just… gross. It makes sense, and aside from the awful null handling, I understand why it works this way. It's just awkward and uncomfortable and annoying. I dislike it.
Also, functions which are name RetrieveBlahAsType are generally an antipattern. Either there should be some generics, or type conversions should be left to the caller- RetrieveSession(sessionName).ToString() is clearer with its intent than RetrieveSessionString(sessionName). Maybe that's just my hot take- I just hate it when functions return something converted away from its canonical representation; I can do that myself, thank you.
[Advertisement] ProGet’s got you covered with security and access controls on your NuGet feeds. Learn more.Patents For Software and Genetic Code Could Be Revived By Two Bills In Congress
Read more of this story at Slashdot.
Snapchat Reserves the Right To Use AI-Generated Images of Your Face In Ads
Read more of this story at Slashdot.
Apple A16 SoC Now Manufactured In Arizona
Read more of this story at Slashdot.
X Circumvents Court-Ordered Block In Brazil
Read more of this story at Slashdot.
20 Years Later, Real-Time Linux Makes It To the Kernel
Read more of this story at Slashdot.
Microsoft and Abu Dhabi's MGX To Back $30 Billion BlackRock AI Infrastructure
Read more of this story at Slashdot.
Chinese Spies Spent Months Inside Aerospace Engineering Firm's Network Via Legacy IT
Read more of this story at Slashdot.
House Committee Approves Bill Requiring New Cars To Have AM Radio
Read more of this story at Slashdot.
YouTube Launches Communities, a Discord-Like Space For Creators and Fans
Read more of this story at Slashdot.
OpenAI Threatens To Ban Users Who Probe Its 'Strawberry' AI Models
Read more of this story at Slashdot.
23andMe Board Resigns in New Blow To DNA-Testing Company
Read more of this story at Slashdot.
IBM is Quietly Axing Thousands of Jobs
Read more of this story at Slashdot.
Apple and Google Diverge on Photography Philosophy
Read more of this story at Slashdot.
Global Police Dismantle Encrypted Messaging App Used By Criminals
Read more of this story at Slashdot.
Federal Reserve Cuts Rates By Half a Point and Signals Era of Easing Has Begun
Read more of this story at Slashdot.
US Government 'Took Control' of a Botnet Run by Chinese Government Hackers, Says FBI Director
Read more of this story at Slashdot.
LinkedIn Is Training AI on User Data Before Updating Its Terms of Service
Read more of this story at Slashdot.
Fossil Fuel Companies Sponsor $5.6 Billion in Global 'Sportswashing' Deals
Read more of this story at Slashdot.