C
C#8mo ago
stigzler

Refactor this if statement without an additional method

Sorry folks. I'm a bit codeblind atm. What's the neatest way to do the below without another Method (which would contain the repeated code). This feels like a code smell atm:
KryptonWorkspaceCell? cell = SpaceControl?.ActiveCell;
if (cell == null)
{
// ...create a new cell and place at the end of the root collection
cell = new KryptonWorkspaceCell();
SpaceControl!.Root.Children!.Add(cell);
}
else if (cell.Pages.Count() == 0)
{
// first remove the empty cell
SpaceControl!.Root.Children!.Remove(cell);
// now create a new cell and place at the end of the root collection
cell = new KryptonWorkspaceCell();
SpaceControl!.Root.Children!.Add(cell);
}
// cell is then used from hereon in
KryptonWorkspaceCell? cell = SpaceControl?.ActiveCell;
if (cell == null)
{
// ...create a new cell and place at the end of the root collection
cell = new KryptonWorkspaceCell();
SpaceControl!.Root.Children!.Add(cell);
}
else if (cell.Pages.Count() == 0)
{
// first remove the empty cell
SpaceControl!.Root.Children!.Remove(cell);
// now create a new cell and place at the end of the root collection
cell = new KryptonWorkspaceCell();
SpaceControl!.Root.Children!.Add(cell);
}
// cell is then used from hereon in
19 Replies
Omnissiah
Omnissiah8mo ago
the only thing i can think of right now -- not tested at all
var anyPages = cell?.Pages.Any();
if (anyPages != true) {
if (anyPages == false)
SpaceControl!.Root.Children!.Remove(cell);
cell = new KryptonWorkspaceCell();
SpaceControl!.Root.Children!.Add(
}
var anyPages = cell?.Pages.Any();
if (anyPages != true) {
if (anyPages == false)
SpaceControl!.Root.Children!.Remove(cell);
cell = new KryptonWorkspaceCell();
SpaceControl!.Root.Children!.Add(
}
Clément
Clément8mo ago
if (cell is not null)
{
return; // I guess ?
}

if (cell?.Pages.Count == 0)
{
SpaceControl!.Root.Children!.Remove(cell);
}

// ...create a new cell and place at the end of the root collection
cell = new KryptonWorkspaceCell();
SpaceControl!.Root.Children!.Add(cell);
if (cell is not null)
{
return; // I guess ?
}

if (cell?.Pages.Count == 0)
{
SpaceControl!.Root.Children!.Remove(cell);
}

// ...create a new cell and place at the end of the root collection
cell = new KryptonWorkspaceCell();
SpaceControl!.Root.Children!.Add(cell);
Jimmacle
Jimmacle8mo ago
i was about to do the exact same thing ^
stigzler
stigzlerOP8mo ago
ah dammit, sorry folks, forgot to add the cell initiator. Updated OP. Think that kyboshes Clement's Yep - thought of something similar, but wanted to avoid the nested if
Omnissiah
Omnissiah8mo ago
there's more stuff that you could do, maybe, i don't think i would make code more difficult to understand than that
Clément
Clément8mo ago
The original could be better, but at least is readable and is clear on what it's doing
stigzler
stigzlerOP8mo ago
There's this, but it still looks smelly:
if (cell == null || cell.Pages.Count() == 0)
{
if (cell?.Pages.Count == 0) SpaceControl!.Root.Children!.Remove(cell);

// ...create a new cell and place at the end of the root collection
cell = new KryptonWorkspaceCell();
SpaceControl!.Root.Children!.Add(cell);
}
if (cell == null || cell.Pages.Count() == 0)
{
if (cell?.Pages.Count == 0) SpaceControl!.Root.Children!.Remove(cell);

// ...create a new cell and place at the end of the root collection
cell = new KryptonWorkspaceCell();
SpaceControl!.Root.Children!.Add(cell);
}
Omnissiah
Omnissiah8mo ago
exactly if you make an extension you could ortographically avoid the if, like cell?.RemoveSpaceControlChildren(); or something but at that point i would probably rethink the whole thing instead of doing that
stigzler
stigzlerOP8mo ago
Yeah. Problem is, it's someone else's code - working on a github fork bug shoot Don't really know the etiquette for that
Omnissiah
Omnissiah8mo ago
then, you know, "keep it stupid simple"
stigzler
stigzlerOP8mo ago
Think I'll stick with my second version - less repetative
Clément
Clément8mo ago
From my perspective the second version is harder to read
stigzler
stigzlerOP8mo ago
hmm - which is conventionally the bigger sin? More repetition or less readable?
Clément
Clément8mo ago
I think the truth is somewhere between
stigzler
stigzlerOP8mo ago
😆 Well, I'll post it with the alt version and see what the devs think
Clément
Clément8mo ago
:Ok:
Omnissiah
Omnissiah8mo ago
i feel like this SpaceControl!.Root.Children!.Remove(cell); shouldn't go 4 levels deep to do his thing that's why to me it looks like it could have its own class with a specific method to do this
stigzler
stigzlerOP8mo ago
yeah - that's the original dev's code
br4kejet
br4kejet8mo ago
This is one way I find myself doing a lot:
KryptonWorkspaceCell? cell = SpaceControl?.ActiveCell;
bool hasZeroPages = false;
if (cell == null || (hasZeroPages = cell.Pages.Count() == 0))
{
if (hasZeroPages)
SpaceControl!.Root.Children!.Remove(cell)

cell = new KryptonWorkspaceCell();
SpaceControl!.Root.Children!.Add(cell);
}
KryptonWorkspaceCell? cell = SpaceControl?.ActiveCell;
bool hasZeroPages = false;
if (cell == null || (hasZeroPages = cell.Pages.Count() == 0))
{
if (hasZeroPages)
SpaceControl!.Root.Children!.Remove(cell)

cell = new KryptonWorkspaceCell();
SpaceControl!.Root.Children!.Add(cell);
}
Want results from more Discord servers?
Add your server