P
Peter Morris
Code before:
private void RemoveCharacter(string characterName)
{
disableEvents = true;
bool found = false;
try
{
for (int catCount = 0; !found && (catCount < treeView.Nodes.Count);
catCount++)
{
for (int charCount = 0; !found && (charCount <
treeView.Nodes[catCount].Nodes.Count); charCount++)
{
if (some condition is met)
{
found = true;
//Some code here
}
}
}
}
finally
{
disableEvents = false;
}
}
Code after:
private void RemoveCharacter(string characterName)
{
disableEvents = true;
try
{
foreach (TreeNode categoryNode in treeView.Nodes)
foreach (TreeNode characterNode in categoryNode.Nodes)
if (some condition is met)
{
//Some code here
return;
}
}
finally
{
disableEvents = false;
}
}
The "before" argument is that there is only 1 point of exit. The "after"
argument is that it is (in my opinion) easier to read.
From being a Pascal coder I used to employe the "1 point of exit" myself,
but since switching to C# and using the return statement I don't mind using
multiple exit points as long as the code isn't too complicated to
understand. These days I don't agree with the "1 point of exit" strategy
because I think it makes the code harder to read and provides no benefit.
What do you guys think?
private void RemoveCharacter(string characterName)
{
disableEvents = true;
bool found = false;
try
{
for (int catCount = 0; !found && (catCount < treeView.Nodes.Count);
catCount++)
{
for (int charCount = 0; !found && (charCount <
treeView.Nodes[catCount].Nodes.Count); charCount++)
{
if (some condition is met)
{
found = true;
//Some code here
}
}
}
}
finally
{
disableEvents = false;
}
}
Code after:
private void RemoveCharacter(string characterName)
{
disableEvents = true;
try
{
foreach (TreeNode categoryNode in treeView.Nodes)
foreach (TreeNode characterNode in categoryNode.Nodes)
if (some condition is met)
{
//Some code here
return;
}
}
finally
{
disableEvents = false;
}
}
The "before" argument is that there is only 1 point of exit. The "after"
argument is that it is (in my opinion) easier to read.
From being a Pascal coder I used to employe the "1 point of exit" myself,
but since switching to C# and using the return statement I don't mind using
multiple exit points as long as the code isn't too complicated to
understand. These days I don't agree with the "1 point of exit" strategy
because I think it makes the code harder to read and provides no benefit.
What do you guys think?