The Daily WTF
Follow
CodeSOD: Connection State
Frederick A. shares a null-checking problem in the IsCalling method of a ConferenceService class. This method attempts to determine if a web conference is active by accessing IsWebRTCConnected through a potentially null object chain: m_ConnectionService.Core.State.IsWebRTCConnected. The original code uses a try/catch block to handle potential NullReferenceExceptions, returning false if any part of the chain is null. This approach effectively masks the underlying issue of uninitialized objects. Frederick proposes using the C# null-coalescing operator (?.) as a direct fix, which would concisely return false if any object in the chain is null, like so: m_ConnectionService?.Core?.State?.IsWebRTCConnected ?? false. While this is a functional solution, the author argues it's not a fundamental fix for the deeper architectural problem. The core issue lies in how connection state is managed, suggesting it should be handled by a proper state machine. Relying on a deep object chain with boolean flags for critical state information is indicative of a poor design choice. Although a full state machine implementation would be a more robust solution, the author acknowledges it requires significant refactoring. However, the example serves as a strong reminder for developers to carefully consider their state management strategies to avoid such null-checking complexities.