I found in the catalog of Refactoring by Martin Fowler, with Kent Beck
book that they mention Extract Function refactoring.
- It is a good practice to wrap your related code into local functions to make easy the readability and maintenance of code?
I always thought that local functions were created to avoid writing multiple times the same code inside some particular function that has to repeat some lines. One example can be for example modifying a string value like in the following example.
private void CopyOneFileToOtherInSomeDirectory(string path, string filename, string outputFilename)
{
// Reading document
var reader = File.OpenText($"{AppendPathSeparator(path)}{filename}");
var text = reader.ReadToEnd();
// Writing
using (FileStream fs = File.Create($"{AppendPathSeparator(path)}{outputFilename}"))
{
byte[] textBytes = new UTF8Encoding(true).GetBytes(text);
fs.Write(textBytes, 0, textBytes.Length);
}
string AppendPathSeparator(string filepath)
{
return filepath.EndsWith(@"\") ? filepath : filepath + @"\";
}
}
but in the Extract Function refactoring extracting to local function can be applied to refactor the last example to something like this.
private void CopyOneFileToOtherInSomeDirectory(string path, string filename, string outputFilename)
{
string Read()
{
var reader = File.OpenText($"{AppendPathSeparator(path)}{filename}");
return reader.ReadToEnd();
}
void Write(string text)
{
using (FileStream fs = File.Create($"{AppendPathSeparator(path)}{outputFilename}"))
{
byte[] textBytes = new UTF8Encoding(true).GetBytes(text);
fs.Write(textBytes, 0, textBytes.Length);
}
}
string AppendPathSeparator(string filepath)
{
return filepath.EndsWith(@"\") ? filepath : filepath + @"\";
}
var readedText = Read();
Write(readedText);
}
It looks bigger but with one of these awesome IDE like the IntelliJ ones you can collapse the inner methods and get something really clear to follow.
private void CopyOneFileToOtherInSomeDirectory(string path, string filename, string outputFilename)
{
string Read(){...}
void Write(string text){...}
string AppendPathSeparator(string filepath){...}
var readedText = Read();
Write(readedText);
}
What do you think? is it a good practice to chop the functions with multiple steps to inner functions?
2. Can this have some impact in performance terms?