internal void AddChild(ArchiveTreeEntry child)
{
if (child == null) { return; }
mChildren.Add(child);
}
internal void AddChild(ArchiveTreeEntry child)
{
if (child == null) { return; }
mChildren.Add(child);
}
an additional check with !Any()
will make the programm flow more prominent because it clearly states to add only if ther are any items to add.
internal void AddChildRange(IEnumerable children)
{
if (children == null || !children.Any()) { return; }
mChildren.AddRange(children);
}
___
internal void AddChildRange(IEnumerable<ArchiveTreeEntry> children)
{
if (children == null || !children.Any()) { return; }
mChildren.AddRange(children);
}
- In the
CreateRootEntries()
method you have a copy&pasta bug. The return statement saysreturn rootArchiveTreeEntries;
instead ofreturn rootEntries;
In the
CreateRootEntries()
method you have a copy&pasta bug. The return statement saysreturn rootArchiveTreeEntries;
instead ofreturn rootEntries;
- you really should use the
var
type if it is obvious from the right hand side of the assignment which type is assigned like for instance hereyou really should use the
var
type if it is obvious from the right hand side of the assignment which type is assigned like for instance hereList<ArchiveTreeEntry> rootEntries = new List<ArchiveTreeEntry>();
List rootEntries = new List();
like so
var rootEntries = new List();
___
var rootEntries = new List<ArchiveTreeEntry>();
Implementing the mentioned points together with the mentioned points from [@Pimgd][1] [answer][2] will lead to
public class ArchiveBuilder
{
public static IEnumerable Build(IEnumerable archiveDefinitions)
{
if (archiveDefinitions == null || !archiveDefinitions.Any()) { return Enumerable.Empty(); }
IEnumerable localEntries = new List(archiveDefinitions);
var rootArchiveTreeEntries = new List(CreateRootEntries(localEntries));
localEntries = RemoveRootArchiveDefinitions(localEntries);
foreach (var rootEntry in rootArchiveTreeEntries)
{
HandleEntriesForParent(localEntries, rootEntry);
}
return rootArchiveTreeEntries;
}
private static IEnumerable CreateRootEntries(
IEnumerable archiveDefinitions)
{
var rootEntries = new List();
rootEntries.AddRange(
archiveDefinitions.Where(e => e.TypeOfArchive == ArchiveType.Archive)
.Select(d => new ArchiveTreeEntry(d)));
return rootEntries;
}
private static IEnumerable RemoveRootArchiveDefinitions(
IEnumerable archiveDefinitions)
{
return archiveDefinitions.Except(
archiveDefinitions.Where(e => e.TypeOfArchive == ArchiveType.Archive));
}
private static void HandleEntriesForParent(
IEnumerable archiveDefinitions,
ArchiveTreeEntry parent)
{
if (!archiveDefinitions.Any()) { return; }
var children = GetChildren(archiveDefinitions, parent.Id);
AddChildrenToParent(parent, children);
RemoveChildren(archiveDefinitions, parent.Id);
foreach (ArchiveTreeEntry nextParent in parent.Children)
{
HandleEntriesForParent(archiveDefinitions, nextParent);
}
}
private static IEnumerable GetChildren(
IEnumerable archiveDefinitions, string parentId)
{
return archiveDefinitions.Where(e => e.ParentId == parentId);
}
private static void AddChildrenToParent(ArchiveTreeEntry parent,
IEnumerable children)
{
parent.AddChildRange(children.Select(x => new ArchiveTreeEntry(x)));
}
private static void RemoveChildren(
IEnumerable archiveDefinition,
string parentId)
{
archiveDefinition.Select(e => e.ParentId != parentId);
}
}
[1]: http://codereview.stackexchange.com/users/49350/pimgd@Pimgd
[2]: http://codereview.stackexchange.com/a/58995/29371answer will lead to
public class ArchiveBuilder
{
public static IEnumerable<ArchiveTreeEntry> Build(IEnumerable<ArchiveDefinition> archiveDefinitions)
{
if (archiveDefinitions == null || !archiveDefinitions.Any()) { return Enumerable.Empty<ArchiveTreeEntry>(); }
IEnumerable<ArchiveDefinition> localEntries = new List<ArchiveDefinition>(archiveDefinitions);
var rootArchiveTreeEntries = new List<ArchiveTreeEntry>(CreateRootEntries(localEntries));
localEntries = RemoveRootArchiveDefinitions(localEntries);
foreach (var rootEntry in rootArchiveTreeEntries)
{
HandleEntriesForParent(localEntries, rootEntry);
}
return rootArchiveTreeEntries;
}
private static IEnumerable<ArchiveTreeEntry> CreateRootEntries(
IEnumerable<ArchiveDefinition> archiveDefinitions)
{
var rootEntries = new List<ArchiveTreeEntry>();
rootEntries.AddRange(
archiveDefinitions.Where(e => e.TypeOfArchive == ArchiveType.Archive)
.Select(d => new ArchiveTreeEntry(d)));
return rootEntries;
}
private static IEnumerable<ArchiveDefinition> RemoveRootArchiveDefinitions(
IEnumerable<ArchiveDefinition> archiveDefinitions)
{
return archiveDefinitions.Except(
archiveDefinitions.Where(e => e.TypeOfArchive == ArchiveType.Archive));
}
private static void HandleEntriesForParent(
IEnumerable<ArchiveDefinition> archiveDefinitions,
ArchiveTreeEntry parent)
{
if (!archiveDefinitions.Any()) { return; }
var children = GetChildren(archiveDefinitions, parent.Id);
AddChildrenToParent(parent, children);
RemoveChildren(archiveDefinitions, parent.Id);
foreach (ArchiveTreeEntry nextParent in parent.Children)
{
HandleEntriesForParent(archiveDefinitions, nextParent);
}
}
private static IEnumerable<ArchiveDefinition> GetChildren(
IEnumerable<ArchiveDefinition> archiveDefinitions, string parentId)
{
return archiveDefinitions.Where(e => e.ParentId == parentId);
}
private static void AddChildrenToParent(ArchiveTreeEntry parent,
IEnumerable<ArchiveDefinition> children)
{
parent.AddChildRange(children.Select(x => new ArchiveTreeEntry(x)));
}
private static void RemoveChildren(
IEnumerable<ArchiveDefinition> archiveDefinition,
string parentId)
{
archiveDefinition.Select(e => e.ParentId != parentId);
}
}
internal void AddChild(ArchiveTreeEntry child)
{
if (child == null) { return; }
mChildren.Add(child);
}
an additional check with !Any()
will make the programm flow more prominent because it clearly states to add only if ther are any items to add.
internal void AddChildRange(IEnumerable children)
{
if (children == null || !children.Any()) { return; }
mChildren.AddRange(children);
}
___
- In the
CreateRootEntries()
method you have a copy&pasta bug. The return statement saysreturn rootArchiveTreeEntries;
instead ofreturn rootEntries;
- you really should use the
var
type if it is obvious from the right hand side of the assignment which type is assigned like for instance here
List rootEntries = new List();
like so
var rootEntries = new List();
___
Implementing the mentioned points together with the mentioned points from [@Pimgd][1] [answer][2] will lead to
public class ArchiveBuilder
{
public static IEnumerable Build(IEnumerable archiveDefinitions)
{
if (archiveDefinitions == null || !archiveDefinitions.Any()) { return Enumerable.Empty(); }
IEnumerable localEntries = new List(archiveDefinitions);
var rootArchiveTreeEntries = new List(CreateRootEntries(localEntries));
localEntries = RemoveRootArchiveDefinitions(localEntries);
foreach (var rootEntry in rootArchiveTreeEntries)
{
HandleEntriesForParent(localEntries, rootEntry);
}
return rootArchiveTreeEntries;
}
private static IEnumerable CreateRootEntries(
IEnumerable archiveDefinitions)
{
var rootEntries = new List();
rootEntries.AddRange(
archiveDefinitions.Where(e => e.TypeOfArchive == ArchiveType.Archive)
.Select(d => new ArchiveTreeEntry(d)));
return rootEntries;
}
private static IEnumerable RemoveRootArchiveDefinitions(
IEnumerable archiveDefinitions)
{
return archiveDefinitions.Except(
archiveDefinitions.Where(e => e.TypeOfArchive == ArchiveType.Archive));
}
private static void HandleEntriesForParent(
IEnumerable archiveDefinitions,
ArchiveTreeEntry parent)
{
if (!archiveDefinitions.Any()) { return; }
var children = GetChildren(archiveDefinitions, parent.Id);
AddChildrenToParent(parent, children);
RemoveChildren(archiveDefinitions, parent.Id);
foreach (ArchiveTreeEntry nextParent in parent.Children)
{
HandleEntriesForParent(archiveDefinitions, nextParent);
}
}
private static IEnumerable GetChildren(
IEnumerable archiveDefinitions, string parentId)
{
return archiveDefinitions.Where(e => e.ParentId == parentId);
}
private static void AddChildrenToParent(ArchiveTreeEntry parent,
IEnumerable children)
{
parent.AddChildRange(children.Select(x => new ArchiveTreeEntry(x)));
}
private static void RemoveChildren(
IEnumerable archiveDefinition,
string parentId)
{
archiveDefinition.Select(e => e.ParentId != parentId);
}
}
internal void AddChild(ArchiveTreeEntry child)
{
if (child == null) { return; }
mChildren.Add(child);
}
an additional check with !Any()
will make the programm flow more prominent because it clearly states to add only if ther are any items to add.
internal void AddChildRange(IEnumerable<ArchiveTreeEntry> children)
{
if (children == null || !children.Any()) { return; }
mChildren.AddRange(children);
}
In the
CreateRootEntries()
method you have a copy&pasta bug. The return statement saysreturn rootArchiveTreeEntries;
instead ofreturn rootEntries;
you really should use the
var
type if it is obvious from the right hand side of the assignment which type is assigned like for instance hereList<ArchiveTreeEntry> rootEntries = new List<ArchiveTreeEntry>();
like so
var rootEntries = new List<ArchiveTreeEntry>();
Implementing the mentioned points together with the mentioned points from @Pimgd answer will lead to
public class ArchiveBuilder
{
public static IEnumerable<ArchiveTreeEntry> Build(IEnumerable<ArchiveDefinition> archiveDefinitions)
{
if (archiveDefinitions == null || !archiveDefinitions.Any()) { return Enumerable.Empty<ArchiveTreeEntry>(); }
IEnumerable<ArchiveDefinition> localEntries = new List<ArchiveDefinition>(archiveDefinitions);
var rootArchiveTreeEntries = new List<ArchiveTreeEntry>(CreateRootEntries(localEntries));
localEntries = RemoveRootArchiveDefinitions(localEntries);
foreach (var rootEntry in rootArchiveTreeEntries)
{
HandleEntriesForParent(localEntries, rootEntry);
}
return rootArchiveTreeEntries;
}
private static IEnumerable<ArchiveTreeEntry> CreateRootEntries(
IEnumerable<ArchiveDefinition> archiveDefinitions)
{
var rootEntries = new List<ArchiveTreeEntry>();
rootEntries.AddRange(
archiveDefinitions.Where(e => e.TypeOfArchive == ArchiveType.Archive)
.Select(d => new ArchiveTreeEntry(d)));
return rootEntries;
}
private static IEnumerable<ArchiveDefinition> RemoveRootArchiveDefinitions(
IEnumerable<ArchiveDefinition> archiveDefinitions)
{
return archiveDefinitions.Except(
archiveDefinitions.Where(e => e.TypeOfArchive == ArchiveType.Archive));
}
private static void HandleEntriesForParent(
IEnumerable<ArchiveDefinition> archiveDefinitions,
ArchiveTreeEntry parent)
{
if (!archiveDefinitions.Any()) { return; }
var children = GetChildren(archiveDefinitions, parent.Id);
AddChildrenToParent(parent, children);
RemoveChildren(archiveDefinitions, parent.Id);
foreach (ArchiveTreeEntry nextParent in parent.Children)
{
HandleEntriesForParent(archiveDefinitions, nextParent);
}
}
private static IEnumerable<ArchiveDefinition> GetChildren(
IEnumerable<ArchiveDefinition> archiveDefinitions, string parentId)
{
return archiveDefinitions.Where(e => e.ParentId == parentId);
}
private static void AddChildrenToParent(ArchiveTreeEntry parent,
IEnumerable<ArchiveDefinition> children)
{
parent.AddChildRange(children.Select(x => new ArchiveTreeEntry(x)));
}
private static void RemoveChildren(
IEnumerable<ArchiveDefinition> archiveDefinition,
string parentId)
{
archiveDefinition.Select(e => e.ParentId != parentId);
}
}
ArchiveTreeEntry
Returning early will enhance readability. So the AddChild()
and AddChildRange()
method should look like so
internal void AddChild(ArchiveTreeEntry child)
{
if (child == null) { return; }
mChildren.Add(child);
}
an additional check with !Any()
will make the programm flow more prominent because it clearly states to add only if ther are any items to add.
internal void AddChildRange(IEnumerable children)
{
if (children == null || !children.Any()) { return; }
mChildren.AddRange(children);
}
___
ArchiveBuilder
- In the
CreateRootEntries()
method you have a copy&pasta bug. The return statement saysreturn rootArchiveTreeEntries;
instead ofreturn rootEntries;
- you really should use the
var
type if it is obvious from the right hand side of the assignment which type is assigned like for instance here
List rootEntries = new List();
like so
var rootEntries = new List();
___
Implementing the mentioned points together with the mentioned points from [@Pimgd][1] [answer][2] will lead to
public class ArchiveBuilder
{
public static IEnumerable Build(IEnumerable archiveDefinitions)
{
if (archiveDefinitions == null || !archiveDefinitions.Any()) { return Enumerable.Empty(); }
IEnumerable localEntries = new List(archiveDefinitions);
var rootArchiveTreeEntries = new List(CreateRootEntries(localEntries));
localEntries = RemoveRootArchiveDefinitions(localEntries);
foreach (var rootEntry in rootArchiveTreeEntries)
{
HandleEntriesForParent(localEntries, rootEntry);
}
return rootArchiveTreeEntries;
}
private static IEnumerable CreateRootEntries(
IEnumerable archiveDefinitions)
{
var rootEntries = new List();
rootEntries.AddRange(
archiveDefinitions.Where(e => e.TypeOfArchive == ArchiveType.Archive)
.Select(d => new ArchiveTreeEntry(d)));
return rootEntries;
}
private static IEnumerable RemoveRootArchiveDefinitions(
IEnumerable archiveDefinitions)
{
return archiveDefinitions.Except(
archiveDefinitions.Where(e => e.TypeOfArchive == ArchiveType.Archive));
}
private static void HandleEntriesForParent(
IEnumerable archiveDefinitions,
ArchiveTreeEntry parent)
{
if (!archiveDefinitions.Any()) { return; }
var children = GetChildren(archiveDefinitions, parent.Id);
AddChildrenToParent(parent, children);
RemoveChildren(archiveDefinitions, parent.Id);
foreach (ArchiveTreeEntry nextParent in parent.Children)
{
HandleEntriesForParent(archiveDefinitions, nextParent);
}
}
private static IEnumerable GetChildren(
IEnumerable archiveDefinitions, string parentId)
{
return archiveDefinitions.Where(e => e.ParentId == parentId);
}
private static void AddChildrenToParent(ArchiveTreeEntry parent,
IEnumerable children)
{
parent.AddChildRange(children.Select(x => new ArchiveTreeEntry(x)));
}
private static void RemoveChildren(
IEnumerable archiveDefinition,
string parentId)
{
archiveDefinition.Select(e => e.ParentId != parentId);
}
}