In a recent CodeSOD installment on The Daily WTF, Remy Porter presents a snippet submitted by reader Frederick A from a web conferencing tool that uses WebRTC to connect chat clients.
The method in question, IsCalling on a ConferenceService class, is documented as checking whether a conference is active. Its entire logic consists of returning m_ConnectionService.Core.State.IsWebRTCConnected wrapped in a try block, with a bare catch that swallows any exception and returns false. The reasoning: several objects in that chain may not exist at call time, so any thrown NullReferenceException conveniently signals "not connected."
Frederick proposes the idiomatic C# fix: m_ConnectionService?.Core?.State?.IsWebRTCConnected ?? false, using the null-conditional and null-coalescing operators instead of exception-driven control flow.
Porter accepts that fix but notes it is not a true solution. Connection state is really a state machine — connected, disconnected, and likely further states going unchecked — and burying it as a boolean flag deep inside an object graph is the underlying smell. He stops short of recommending a rewrite, but urges developers to think more deliberately about how they model state.
Comments
No comments yet — be the first.
Open the discussion
No account or password needed — just enter your e-mail and we’ll send you a one-time sign-in link. First time here? You’re set up automatically.
Your rating will be applied automatically after you sign in.
Check your inbox
We’ve sent a sign-in link to …. Open it on this device — this tab will sign you in automatically.
Nothing arrived? Check your spam folder — and mark the mail as "Not spam" so it lands in your inbox next time.