处理条件语句的最佳方法

时间:2012-02-15 05:26:47

标签: c#

我在我的代码中使用以下条件但看起来效率不高,这是否有更好的处理方式?

if (ic = filename.Contains(".wmv"))
{
    if (bitnumber > 400)
    {
        path = "ftp://" + ftpServerIP + "/" + "media" + "/" + "lib" + "/" + programName + "/" + date + "/";
        UploadCondition(path, filename);
        //return path;
    }
}

if (ic = filename.Contains(".wmv"))
{
    if (bitnumber < 400)
    {
        path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
        UploadCondition(path, filename);
        //return path;
    }
}

if (ic = filename.Contains(".m4v"))
{
    path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
    UploadCondition(path, filename);
}

if (ic = filename.Contains(".mp4"))
{
    path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
    UploadCondition(path, filename);
}
if (ic = filename.Contains(".flv"))
{
    path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
    UploadCondition(path, filename);
}
if (ic = filename.Contains(".mpg"))
{
    path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
    UploadCondition(path, filename);
}
if (ic = filename.Contains(".aac"))
{
    path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "audio" + "/" + "podcast" + "/";
    UploadCondition(path, filename);
}
if (ic = filename.Contains(".mp3"))
{
    path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "audio" + "/" + "podcast" + "/";
    UploadCondition(path, filename);
}

7 个答案:

答案 0 :(得分:3)

在其他类中分解:

public class AudioFileValidator
{
    private List<string> _extensions = new List<string>{".aac", ".mp3"};
    public bool IsValid(string filename)
    {
        if (!_extensions.Contains(Path.GetExtension(filename))
            return false;

        //validate bitrate etc
    }
}

用法:

var audioValidator = new AudioFileValidator();
if (audioValidator.IsValid(filename)) 
{
    path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "audio" + "/" + "podcast" + "/";
    UploadCondition(path, filename);
}

var videoValidator = new VideoFileValidator();
if (videoValidator.IsValid(filename)) 
{
    path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
    UploadCondition(path, filename);
}

通过这样做,你将获得一个单一责任的课程,可以在其他地方重复使用,并且易于单元测试。


你甚至可以更进一步,并引入一个名为IMediaFileValidator的新接口,所有验证器都会实现。并做一些类似的事情:

foreach (var validator in validators)
{ 
    if (validator.IsValid(filename))
    {
        // use info from the validator to build the path
        var mediaName = validator.MediaName; 
        path = "ftp://" + ftpServerIP + "/" + mediaName + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
        UploadCondition(path, filename);
        break;
    }
}

这也会使您的代码遵循开放/封闭原则。

答案 1 :(得分:2)

你需要进行大量的重构。以下是一些可以帮助您入门的想法:

  • 使用String.Format并仅传入更改的值以保存所有重复文本
  • 构建Extension / Ext-Combination键的字典,并将值设置为目标路径。如果 - else语句
  • ,那么您只需要一次查找而不是大嵌套
  • 使用Path.GetExtension而不是Contains更准确

EG。

  string formatStringNews = "ftp://{0}/news/{1}/";
  string formatStringMedia = "ftp://{0}/media/{1}/";
  dictionary["wmv"] = formatStringMedia;
  dictionary["mp3"] = formatStringNews;
  ....
  string key = Path.GetExtension(filename);
  path = string.Format(dictionary[key], serverName, programName);

答案 2 :(得分:1)

这样的问题对你的问题来说是一个很好的简短解决方案,我相信它会处理你的if语句正在处理的所有情况。

String[] videoExtensions = { "wmv", "m4v", "mp4", "flv" };
String[] audioExtensions = { "aac", "mp3" };

String ext = Path.GetExtension(filename).ToLower();
String path = "ftp://" + ftpServerIP + "/";

if (-1 != Array.IndexOf(videoExtensions, ext)) {
  if ("wmv".equals(ext) && bitnumber > 400)
    path += "media/lib/" + programName + "/" + date + "/";
  else 
    path += "mpegmedia/news/" + programName + "/video/podcast/";
}
else if (-1 != Array.IndexOf(audioExtensions, ext)) {
  path += "mpegmedia/news/" + programName + "/audio/podcast/";
}​​​​​​​​​​​
else {
  // handle unknown extension types as desired
}

UploadCondition(path, filename);

答案 3 :(得分:1)

使用switch声明和System.IO.Path.GetExtension

select (System.IO.Path.GetExtension(filename))
{
    case ".wmv":
        if (bitnumber > 400)
        {
            path = "ftp://" + ftpServerIP + "/" + "media" + "/" + "lib" + "/" + programName + "/" + date + "/";
            UploadCondition(path, filename);
            //return path;
        }
        else
        {
            path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
            UploadCondition(path, filename);
            //return path;
        }
        break;

        case ".m4v":
        case ".mp4":
        case ".flv":
        case ".mpg":
        case ".mp3":
        default:
            path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
            UploadCondition(path, filename);
            break;
    }
}

我猜你会想要最后一个版块的变体,但这应该很容易修改。

答案 4 :(得分:0)

至少你可以把它转换成if / elseif语句:

if (ic....)
{
    ...
} else if (ic...) {
    ...
}

答案 5 :(得分:0)

我假设,您的filename将是.m4v,.flv,.mp4等......所以这里是代码..

    if (ic = filename.Contains(".wmv"))
{
    if (bitnumber > 400)
    {
        path = "ftp://" + ftpServerIP + "/" + "media" + "/" + "lib" + "/" + programName + "/" + date + "/";
        UploadCondition(path, filename);
        //return path;
    }

        else
        {
        path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
        UploadCondition(path, filename);
        //return path;
    }
}

    else if (ic = filename.Contains(".m4v"))
{
    path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
    UploadCondition(path, filename);
}

else if (ic = filename.Contains(".mp4"))
{
    path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
    UploadCondition(path, filename);
}
else if (ic = filename.Contains(".flv"))
{
    path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
    UploadCondition(path, filename);
}
else if (ic = filename.Contains(".mpg"))
{
    path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
    UploadCondition(path, filename);
}
else if (ic = filename.Contains(".aac"))
{
    path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "audio" + "/" + "podcast" + "/";
    UploadCondition(path, filename);
}
else if (ic = filename.Contains(".mp3"))
{
    path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "audio" + "/" + "podcast" + "/";
    UploadCondition(path, filename);
}
    else
    {
        //No Match found
    }

最好的方法是使用Switch(fileExtn)

答案 6 :(得分:0)

你可能会让它变得更简单,比如

if (filename.Contains(".wmv"))
    // path =  set the path as you require

并在所有ifs结束后调用您的方法

UploadCondition(path, filename);

最好从文件名中提取扩展名.wmv, .m4v,并将其设置为一个开关,用于设置路径。