Question:
I heard that it is not very good when there are a lot of nested constructs in a method. Apparently, they say so because the code becomes unreadable.
For instance,
public void Do(int a, bool b, List<X> c, object d = null)
{
if ()
{
foreach ()
{
if ()
{
}
else if ()
{
if ()
{
foreach ()
{
if ()
{
}
}
}
}
}
}
}
What approaches exist, such as refactoring methods in already written code? Especially in cases where the method has many input parameters and they are all somehow intertwined inside it, so you won’t guess how to break it into different methods. Moreover, when the code cannot be rewritten, because you don’t understand everything there, there is a risk that it will stop working correctly. Are there any boilerplate approaches?
Comes to mind:
- use keywords like
return
,continue
,break
For example, instead of
if (x != null)
{
//code
}
write like this:
if (x == null)
return;
//code
Any other advice?
Answer:
Check out Robert Martin's book Clean Code.
Not all of the author's thoughts are indisputable, but it is definitely worth reading. At least after reading, many things will be perceived a little differently.
PS The electronic version is easily googled.