简体   繁体   中英

C# Minimize if statement

I am still a little bit new to C# and I was wondering how i can minimize this code.

This is what I have so far:

private void CheckFiles()
{
    if (!File.Exists(ProgramLocation + "\\Server Files\\" + "Bukkit.jar"))
    {
        DownloadBukkitJar();
    }
    else
    {
        Close();
    }
    if (!File.Exists(ProgramLocation + "\\dlls\\" + "HtmlAgilityPack.dll"))
    {
        DownloadHtmlAgilityPackDll();
    }
    else
    {
        Close();
    }
}

So how could I make this in less code?

private void CheckFile(string path, Action actionIfMissing)
{
    if (!File.Exists(path))
    {
        actionIfMissing();
    }
    else
    {
        Close();
    }
}

public void CheckFiles()
{
    var bukkitPath = Path.Combine(ProgramLocation, String.Format("{0}{1}{2}", "Server Files", Path.DirectorySeparatorChar, "Bukkit.jar");
    CheckFile(bukkitPath, DownloadBukkitJar);
    var htmlAgilityPackPath = Path.Combine(ProgramLocation, String.Format("{0}{1}{2}", "dlls", Path.DirectorySeparatorChar, "HtmlAgilityPack.dll");
    CheckFile(htmlAgilityPackPath, DownloadHtmlAgilityPackDLL);
}

Note - be wary when hard-coding file paths, you should use Path.DirectorySeparatorChar if you are building up a path, or when combining paths use Path.Combine . This will ensure your paths are platform specific which would make your code more portable. I have updated the example to demonstrate how to do this.

I'm not sure what the close() or downloadfunctions do but personally i would aim for something like this :

private void CheckFiles()
{
    DownloadIfNeeded(ProgramLocation + "\\Server Files\\" + "Bukkit.jar");
    DownloadIfNeeded(ProgramLocation + "\\dlls\\" + "HtmlAgilityPack.dll");
}

private void DownloadIfNeeded(string s)
{
    if (!File.Exists(s))
    {
        DownloadFile(s);
    }
    else
    {
        Close();
    }
}

Depending on your taste and coding style guidelines, the following might be a reasonable attempt:

private void CheckFiles()
{
    bool bukkit = File.Exists(string.Format("{0}\\Server Files\\Bukkit.jar", ProgramLocation));
    bool htmlap = File.Exists(string.Format("{0}\\dlls\\HtmlAgilityPack.dll", ProgramLocation));

    if (!bukkit)           DownloadBukkitJar();
    if (!htmlap)           DownloadHtmlAgilityPackDll();
    if (bukkit || htmlap)  Close();
}

Notes

  • I assumed that calling Close twice was really not meaningful
  • Look at the use of String.Format (an example of code clarity before performance)

From what i understand you're trying to consolidate the two calls to Close() in a single block, here's a way to do so, assuming that it does not matter how many times Close() is called

bool bukkitExists=File.Exists(ProgramLocation + "\\Server Files\\" + "Bukkit.jar");
bool htmlAgilitPackExists=File.Exists(ProgramLocation + "\\dlls\\" + "HtmlAgilityPack.dll");
if(bukkitExists||htmlAgilitPackExists){Close();}
else{
if (!htmlAgilitPackExists){DownloadHtmlAgilityPackDll();}
if(!bukkitExists){DownloadBukkitJar();}
}
private void CheckFiles()
{
    bool bukkitExists = File.Exists(ProgramLocation + "\\Server Files\\" + "Bukkit.jar");
    if (!bukkitExists)
    {
        DownloadBukkitJar();
    }
    bool agilityExists = File.Exists(ProgramLocation + "\\dlls\\" + "HtmlAgilityPack.dll");
    if (!agilityExists)
    {
        DownloadHtmlAgilityPackDll();
    }

    if (bukkitExists || agilityExists)
    {
        Close();
    }
}

Are you closing a Form? Did you really want to close it twice?

Some things:
- Concatenate paths using Path.Combine ( http://msdn.microsoft.com/en-us/library/fyy7a5kt.aspx )
- Use the @ sign in front of paths, regex patterns, ... so you don't have to escape 'special characters' ( http://en.csharp-online.net/CSharp_FAQ%3A_What_does_at_sign_identifier_mean )
- Curly brackets { and } are not needed if only one line follows the preceding statement (though some people see this as bad code!)

More tips depending on the context:
- Depending on the function of the Close method, it might be possible to always call it, so you only have two if statements without the else statements and just call Close() at the end of the CheckFiles() method.
- When concatenating strings (if not paths), always use the String.Format method instead of the '+' sign ( http://msdn.microsoft.com/en-us/library/system.string.format.aspx )

In your example:

private void CheckFiles()
{
    if (!File.Exists(Path.Combine(ProgramLocation, @"Server Files\Bukkit.jar")))
        DownloadBukkitJar();
    else
        Close();

    if (!File.Exists(Path.Combine(ProgramLocation, @"dlls\HtmlAgilityPack.dll")))
        DownloadHtmlAgilityPackDll();
    else
        Close();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM