简体   繁体   English

在这种情况下我可以避免重复自己(Java)

[英]Can I avoid repeating myself in this situation (Java)

 if (openFile == null) {

      new AppFileDialog().chooseFile("Save", appFrame);

 }

 if (openFile == null) {

      return;

 }

Here I need to check to see if the user has already chosen a file. 在这里,我需要检查用户是否已经选择了一个文件。 If not, they are given a prompt to. 如果没有,他们会得到提示。 If the file is still null, the function returns without saving. 如果文件仍为null,则函数将返回而不保存。 The problem is the two identical if statements, can I avoid it? 问题是两个相同的if语句,我可以避免吗? I take DRY very seriously, but at the same time KISS. 我非常认真地对待DRY,但同时KISS。 Ideally the two go hand in hand, but in a situation like this, it seems they are mutually exclusive. 理想情况下,这两者是相辅相成的,但在这种情况下,似乎它们是相互排斥的。

Not quite, although I think a different structure would make the issue more obvious: 虽然我认为不同的结构会使问题更加明显,但并不完全如此:

// If no file, give the user a chance to open one
if (openFile == null) {
    new AppFileDialog().chooseFile("Save", appFrame);

    // still no file, user must not want to do this
    if (openFile == null) {
        return;
    }    
}

Put it in a loop? 把它放在一个循环中? The file the user selects should never be null though 用户选择的文件永远不应为null

However you've cut out too much code give any concrete answer. 但是你已经删除了太多的代码给出了具体的答案。 All I see is two identical checks which I would merge into one, but I think you coming here for something more. 我所看到的只是两张相同的支票,我会合并成一张,但我想你来这里是为了更多。

Ill do something like: 生病做的事情如下:

int tries = 0;
int maxTries = 3;
do {
   openFile = new AppFileDialog().chooseFile("Save", appFrame);
   if (openFile != null) 
      tries = maxTries;
   tries++;
} while (tries < maxTries);

if (openFile == null)
   return;

I would try to get rid of that side effect (setting openFile in the chooseFile method), because it makes the code hard to follow. 我会尝试摆脱那种副作用(在chooseFile方法中设置openFile),因为它使代码很难遵循。 Can't you return it? 你不能退货吗?

It will not solve the double null check though. 但它不会解决双重空检查。

They are actually different conditions. 它们实际上是不同的条件。 What I think you really mean is: 我认为你的意思是:

if (openFile == null) {
    openFile = new AppFileDialog().chooseFile("Save", appFrame);

    if (openFile == null) {
        return;
    }
}

Which shows that they don't mean the same thing, but yours is more elegant if you wish to add more conditions (more ways that the file could be opened, like using a default filename if the user doesn't supply one himself) 这表明它们并不意味着相同的东西,但如果您希望添加更多条件(更多可以打开文件的方式,例如使用默认文件名,如果用户自己不提供),则更优雅

However I'd prefer: 不过我更喜欢:

openFile = getOpenFile()
if(openFile == null)
    return;

public File getOpenFile() {
   if(openFile == null)
       openFile = new AppFileDialog().chooseFile("Save", appFrame);

   return openFile;
}

This allows the getOpenFile() method to control the openFile variable completely, never accessing the openFile variable from any other methods (except perhaps a closeFile() method. I use this trick sometimes, making a variable that is "Logically private" to just a couple methods in order to reduce complexity a little. 这允许getOpenFile()方法完全控制openFile变量,从不从任何其他方法访问openFile变量(可能除了closeFile()方法。我有时使用这个技巧,使一个“逻辑上私有”的变量只是一个耦合方法以减少复杂性。

You may try something like this, but this assumes that chooseFile will return the file. 您可以尝试这样的方法,但这假设chooseFile将返回该文件。

if ((openFile == null ? new AppFileDialog().chooseFile("Save", appFrame) : openFile) == null) 
  return;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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