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; | |
} |
I just changed it to the above revision. At least, I think I did that right.
Sweet, good call breaking it into its own function, makes the main method simpler and easier to follow
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.
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
Personal preference, I know you are overriding dataItem in the inner-most if block, but I would assign it to something else and then after the 'if (this.IsRightAligned)' block, do a null check on that variable assignment, and if it's null, then return (so you can avoid all the else statements)