I populate a ListView with Items. Each Item has data attached to its .Tag
property.
Over time, I have needed to handle many cases where something would be null or missing or corrupt or whatever.
And so was born this beast:
private void PlayRecording()
{
if (list.Items.Count >= 1)
{
if (list.SelectedItems.Count >= 1)
{
if (((Item)list.SelectedItems[0].Tag).Attachments.Count >= 1)
{
if (((Item)list.SelectedItems[0].Tag).Attachments[0].AudioStreams.Count >= 1)
{
if (((Item)list.SelectedItems[0].Tag).Attachments[0].AudioStreams[0].AudioStream != null)
{
byte[] recording = ((Item)list.SelectedItems[0].Tag).Attachments[0].AudioStreams[0].AudioStream;
if (recording != null)
{
using (MemoryStream memoryStream = new MemoryStream(recording))
{
if (memoryStream != null)
{
soundPlayer.Stream = memoryStream;
soundPlayer.Play();
AudioState = AudioStates.Playing;
}
else status.Text = "The Memory Stream returned is null. The stream may be corrupt or non-existent.";
}
}
else status.Text = "Notes is unable to play the recording.";
}
else status.Text = "The selected item's Audio Stream is null. The recording may be corrupt or missing.";
}
else status.Text = "The selected item does not contain any recordings to play.";
}
else status.Text = "The selected item does not contain any attachments.";
}
else status.Text = "You haven't selected an item in the list.";
}
else status.Text = "There are no items in the list.";
}
Yeah, sure, I could just get rid of the if
's and else
's and replace ((Item)list...)
with Item item = list...
and wrap it all in a single Try{}Catch{}, but I really do hate using Try{}Catch{} unless there is something I am not expecting. And to be honest, using Try{} Catch{} makes me feel lazy and like I'm not doing it properly.
Is there any way I can improve this code and make it a little smaller (while still handling all of the cases where I would need to convey a message)?