'Nightmare' traffic as 5,600 new homes set to be built around Essex village
Mystery of abandoned 'rainbow baby' who first featured in the Daily Mail 65 years ago is finally solved
Trump reacts after 'heavily armed' man is arrested on his California golf course ahead of his visit as suspect is pictured
Brad Pitt asks court to ORDER Angelina Jolie to turn over financial records after she claimed she put 'career on hold'
New Boeing finally gets going, 15 years after debut
Pesticide warning as study finds link to terrifying motor neurone disease - exposure could raise risk by up to 70%
Blow for Letby families as prosecutors stand by decision against further charges involving six more infants following review
CodeSOD: Connection State
Frederick A sends us a bit of null checking code, and offers us a better solution.
class ConferenceService { /// <summary> /// Checks if conference is active /// </summary> public bool IsCalling() { try { return m_ConnectionService.Core.State.IsWebRTCConnected; } catch { return false; } } }This is for a web conferencing tool, which uses WebRTC to set up connections between clients in the chat. This function checks if the chat is active by checking a IsWebRTCConnected flag. But as you can see in this code, that flag is on a long chain of objects, some of which may not exist when this function is called. Thus, we wrap the whole thing up in a try/catch. If anything throws an exception, we know we can just return false. It's probably fine.
The obvious and easy fix, which Frederick proposes, is to use the C# coalescing operator: ?. m_ConnectionService?.Core?.State?.IsWebRTCConnected ?? false would solve this problem just fine.
That said, I wouldn't say that's a true fix. We're talking about a state machine here, though admittedly with two states under discussion (connected/disconnected), though there are probably more not being checked here. This information should be managed via a state machine, not via boolean flags stuffed deep in an object chain. The fix isn't a WTF, but it definitely hints at a better way to manage all of this. Now, my solution likely requires a lot more modification and code changes than what we have here, so I'm not suggesting anyone go off and rewrite this from scratch just to have a cleaner way of managing state. But folks definitely should think more carefully about how they manage state.
.comment { border: none; }Dreaming of a new life abroad? The world's best countries to relocate to have been revealed
'She's a nice girl who's determined not to be a nepo baby': ALISON BOSHOFF has all the exclusive details of Suri Cruise's new acting career - and why there will be no leg-up from her dad Tom: 'As long as she sides with her mother, there will be no…
Fire crews assist Essex farmer after sheep flock get stuck in river
Fire crews assist Essex farmer after sheep flock get stuck in river
John Lewis has all your workout essentials! Here's how to look cool in the gym (from just £22)
Inheritance tax raid undermines faith in pensions, warns retirement giant Standard Life
Watch a SpaceX Rocket Crash Into the Moon
Read more of this story at Slashdot.