I have an class which has a enum property and a boolean property, based on that it calls a specific method with specific parameters. I use a switch statement for the enum and an if for the boolean within each case of the switch. It is a long list and doesn’t feel to me to be the most elegant solution. Anyone got a more elegant or simpler way to implement this?
switch (ReadDecision) { case ReadDecisions.ReadNext: { if (UseTimeout) { Message = queue.Receive(Timeout); } else { Message = queue.Receive(); } break; } case ReadDecisions.PeekNext: { if (UseTimeout) { Message = queue.Peek(Timeout); } else { Message = queue.Peek(); } break; } case ReadDecisions.ReadMessageId: { if (UseTimeout) { Message = queue.ReceiveById(Id, Timeout); } else { Message = queue.ReceiveById(Id); } break; } case ReadDecisions.PeekMessageId: { if (UseTimeout) { Message = queue.PeekById(Id, Timeout); } else { Message = queue.PeekById(Id); } break; } case ReadDecisions.ReadCorrelationId: { if (UseTimeout) { Message = queue.ReceiveByCorrelationId(Id, Timeout); } else { Message = queue.ReceiveByCorrelationId(Id); } break; } case ReadDecisions.PeekCorrelationId: { if (UseTimeout) { Message = queue.PeekByCorrelationId(Id, Timeout); } else { Message = queue.PeekByCorrelationId(Id); } break; } default: { throw new Exception('Unknown ReadDecisions provided'); } }
An often used convention is that a timeout of zero means no timeout. Maybe you could drop the UseTimeout (property?) and use the value zero instead. That’d eliminate some stuff.