我认为,从法典的可读性观点来看,你的最佳点是写上一种复读功能。 休养职能是自称的,直至达到不需要指定任何其他职能的地步。
举例来说,N的系数,如N! ,被定义为1×2×3×......x ...... x n(如果N是积极的惯性),可以比较容易地以下述回馈方式界定。
public int factorial(int n)
{
if (n < 0)
{
throw new Exception("A factorial cannot be calculated for negative integers.");
}
if (n == 0 || n == 1)
{
// end condition, where we do not need to make a recursive call anymore
return 1;
}
else
{
// recursive call
return n * factorial(n - 1);
}
}
NB:0!
同样,也可以重新界定在特定道路下列举所有档案和文件的方法。 这是因为档案和文件夹有复读结构。
因此,如下方法行之有效:
public static List<FileSystemInfo> GetAllFilesAndFolders(string folder)
{
// NOTE : We are performing some basic sanity checking
// on the method s formal parameters here
if (string.IsNullOrEmpty(folder))
{
throw new ArgumentException("An empty string is not a valid path.", "folder");
}
if (!Directory.Exists(folder))
{
throw new ArgumentException("The string must be an existing path.", "folder");
}
List<FileSystemInfo> fileSystemInfos = new List<FileSystemInfo>();
try
{
foreach (string filePath in Directory.GetFiles(folder, "*.*"))
{
// NOTE : We will add a FileSystemInfo object for each file found
fileSystemInfos.Add(new FileInfo(filePath));
}
}
catch
{
// NOTE : We are swallowing all exceptions here
// Ideally they should be surfaced, and at least logged somewhere
// Most of these will be security/permissions related, i.e.,
// the Directory.GetFiles method will throw an exception if it
// does not have security privileges to enumerate files in a folder.
}
try
{
foreach (string folderPath in Directory.GetDirectories(folder, "*"))
{
// NOTE : We will add a FileSystemInfo object for each directory found
fileSystemInfos.Add(new DirectoryInfo(folderPath));
// NOTE : We will also add all FileSystemInfo objects found under
// each directory we find
fileSystemInfos.AddRange(GetAllFilesAndFolders(folderPath));
}
}
catch
{
// NOTE : We are swallowing all exceptions here
// Ideally they should be surfaced, and at least logged somewhere
// Most of these will be security/permissions related, i.e.,
// the Directory.GetDirectories method will throw an exception if it
// does not have security privileges to enumerate files in a folder.
}
return fileSystemInfos;
}
一份行政指示是,这一办法将“按照......全程结构”进行,直至“......”全程“......”。 因此,如果发现许多物体,则可能需要很长时间才能返回。
值得注意的是,使用拉布达语和广度方法可以进一步改善这种方法的可读性。
NB:使用名录的困难。 GetFiles and Directory. 要求再造分数位数的人是,如果存在任何例外(例如,与安全许可有关),这种方法将不退回,而人工休养则允许人们处理这些例外情况,并仍然获得一套档案。