Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Implementing the mentioned points together with the mentioned points from @Pimgd @Pimgd answer answer will lead to

Implementing the mentioned points together with the mentioned points from @Pimgd answer will lead to

Implementing the mentioned points together with the mentioned points from @Pimgd answer will lead to

deleted 9 characters in body
Source Link
Heslacher
  • 50.9k
  • 5
  • 83
  • 177

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 says return rootArchiveTreeEntries; instead of return rootEntries;

    In the CreateRootEntries() method you have a copy&pasta bug. The return statement says return rootArchiveTreeEntries; instead of return 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

    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<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 says return rootArchiveTreeEntries; instead of return 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);

}

}

[1]: http://codereview.stackexchange.com/users/49350/pimgd

[2]: http://codereview.stackexchange.com/a/58995/29371

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 says return rootArchiveTreeEntries; instead of return 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<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);
 }
}
Source Link
Heslacher
  • 50.9k
  • 5
  • 83
  • 177

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 says return rootArchiveTreeEntries; instead of return 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);

}

}

[1]: http://codereview.stackexchange.com/users/49350/pimgd

[2]: http://codereview.stackexchange.com/a/58995/29371

lang-cs

AltStyle によって変換されたページ (->オリジナル) /