Feed aggregator
ISPs Tell Supreme Court They Don't Want To Disconnect Users Accused of Piracy
Read more of this story at Slashdot.
Cruise Robotaxis Return To the Bay Area Nearly One Year After Pedestrian Crash
Read more of this story at Slashdot.
Torvalds Weighs in On 'Nasty' Rust vs C For Linux Debate
Read more of this story at Slashdot.
1 In 10 Orgs Dumping Their Security Vendors After CrowdStrike Outage
Read more of this story at Slashdot.
Apple's New macOS Sequoia Update Breaking Major Security Tools
Read more of this story at Slashdot.
Google Passkeys Can Now Sync Across Devices On Multiple Platforms
Read more of this story at Slashdot.
'Dead Internet Theory' Comes To Life With New AI-Powered Social Media App
Read more of this story at Slashdot.
Americans Can Now Renew Passports Online
Read more of this story at Slashdot.
Tech Jobs Have Dried Up - and Aren't Coming Back Soon
Read more of this story at Slashdot.
Amazon's New 'Shark Tank'-Style Show Gives Winners Top Billing in Its Store
Read more of this story at Slashdot.
FTC Study Finds 'Vast Surveillance' of Social Media Users
Read more of this story at Slashdot.
Palmer Luckey Is Bringing Anduril Smarts To Microsoft's Military Headset
Read more of this story at Slashdot.
Cisco's Second Layoff of 2024 Affects Thousands of Employees
Read more of this story at Slashdot.
Kenya, US Sign Historic Pact On Nuclear Plans
Read more of this story at Slashdot.
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.