Skip to content

Instantly share code, notes, and snippets.

@edgesmash
Last active August 29, 2015 13:56
Show Gist options
  • Save edgesmash/9328531 to your computer and use it in GitHub Desktop.
Save edgesmash/9328531 to your computer and use it in GitHub Desktop.
A case for needing else-all
public override void RenderContent(DataContext dataContext)
{
// Note: Given the framework/tech, I don't have full control over all of this. Maybe I should refactor?
var dataItem = this.GetDataItem(dataContext);
// If we're right-aligned, either set a new data item or don't do anything
if (this.IsRightAligned)
{
var rightAlignedDataItem = GetRightAlignedDataItem(dataContext, currentItemDetails, mediaList);
if (rightAlignedDataItem == null)
{
return;
}
dataItem = rightAlignedDataItem;
}
// Do stuff to the dataItem
}
protected Item GetRightAlignedDataItem(PrintContext printContext, MediaItemLayoutDetails currentItemDetails, Item dataItem)
{
if (!dataItem.IsFullWidth && !dataItem.IsRightAligned)
{
// Don't try to get the next item if there isn't one
if (dataItem != mediaList.Last()))
{
var nextItemDetails = mediaList[mediaList.IndexOf(dataItem)+1];
// if the next item is right-aligned and not full-width, then we should render it
if (nextItemDetails.IsRightAligned && !nextItemDetails.IsFullWidth)
{
return this.GetNewDataItem(dataContext);
}
}
}
return null;
}
@bbguitar77
Copy link

Sweet, good call breaking it into its own function, makes the main method simpler and easier to follow

@edgesmash
Copy link
Author

Also, I'm going to log the crap out of it. This code is part of a larger project that scares the bananas out of me.

@codeimpossible
Copy link

Looks good but I don't get a lot of chances to write c# these days so I did a quick edit to this: https://gist.github.com/codeimpossible/9336568

I refactored to have only one return and combined the two outer IFs from GetRightAlignedDataItem, also I inverted the null check in RenderContent. If that was there to short-circuit and avoid extra work then ignore my changes :D.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment