Last active
August 29, 2015 13:56
-
-
Save edgesmash/9328531 to your computer and use it in GitHub Desktop.
A case for needing else-all
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
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
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.