有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何重构代码以删除if语句?

我在任何地方都学到,如果可以避免使用if语句,那么使用if语句是一种不好的做法。我正在努力学习如何编写干净的代码,似乎有些设计模式也会有所帮助,所以我想知道是否有可能重构这段代码,以便从中删除if语句,下面是代码演示:

public class Printer {

    private boolean on=false;
    private Job currentJob=null;
    private String name;

    public String getName(){
        return name;
    }

    public void setName(String name) {
        this.name=name;
    }

    public void setOn(boolean _on){
        this.on=_on;
    }

    public boolean getOn(){
        return this.on;
    }

    public void setCurrentJob(Job _currentJob){
        this.currentJob=_currentJob;
    }

    public Job getCurrentJob(){
        return this.currentJob;
    }

    private boolean getOnStart(){
        setOn(true);
        return getOn();
    }

    public boolean start(){
        setOn(true);
        return on;
    }

    public boolean stop(){
        setOn(false);
        return !on;
    }

    public boolean suspend(){
        if (!isPrinting()) {
            throw new IllegalStateException("Error");
        }
            currentJob.setState(Job.WAINTING);
            return true;
    }

    public boolean resume(){
        if (this.currentJob==null && currentJob.getState()!=0) {
            throw new IllegalStateException("Error");
        }
            currentJob.setState(Job.PRINTING);
            return true;
    }

    public boolean cancel(){
        if (this.currentJob==null && currentJob.getState()!=0) {
            throw new IllegalStateException("Error");
        }
            currentJob = null;
            return true;
    }
    public boolean print(Job aJob){
        if (isAvailable()){
            currentJob=aJob;
            aJob.setPrinter(this);
            aJob.setState(Job.PRINTING);
            return true;
        }
        System.err.println("Error");
        return false;
    }

    public boolean printingCompleted(){
        if (isPrinting()){
            currentJob.setPrinter(null);
            currentJob.setState(Job.COMPLETED);
            currentJob=null;
            return true;
        }
        System.err.println("Error");
        return false;
    }

    public void setSpooler(Spooler spool){
        spool.join(this);
    }

    public boolean isAvailable(){
        return on && currentJob==null;
    }

    public boolean isPrinting(){
        return on && currentJob!=null;
    }
}

共 (3) 个答案

  1. # 1 楼答案

    在海事组织,如果有正当的需要,拥有ifs没有什么错。但是您应该在方法中避免多个退出点。您可以重写:

    public boolean print(Job aJob){
        boolean result = false;
        if (isAvailable()){
            currentJob=aJob;
            aJob.setPrinter(this);
            aJob.setState(Job.PRINTING);
            result = true;
        }
        System.err.println("Error");
        return result;
    }
    
  2. # 2 楼答案

    I learnt everywhere that using if statement is a bad practice when it's possible to avoid them.

    我确实同意这一点。也许我对措辞过于敏感,但如果可以避免,对我来说,意味着一开始就不需要它们。写得不好的代码肯定会包含做同样事情的不必要逻辑

    if是语言的一个重要方面。认为应该不惜一切代价避免它们是愚蠢的。在需要的地方使用它们,这就是它们的用途

  3. # 3 楼答案

    不正确或过度使用if有时可能表示代码有异味,但我不会说应该避免

    在你的情况下,它们确实有点不确定。我会把你的逻辑编成这样

    public void print(Job aJob) {
        if (!isAvailable()) {
            throw new IllegalStateException("Cannot print when printer not available.");
        }
        currentJob = aJob;
        aJob.setPrinter(this);
        aJob.setState(Job.PRINTING);
    }
    
    public void printingCompleted() {
        if (!isPrinting()) {
            throw new IllegalStateException("Attempt to complete printing when no printing in progress.");
        }
        currentJob.setPrinter(null);
        currentJob.setState(Job.COMPLETED);
        currentJob = null;
    }
    

    这有三个好处:

    1. 错误可以在别处处理/记录
    2. 您不必返回truefalse来表示成功/失败(一种常见的气味)
    3. 每种方法都有一个出口点(众所周知的气味)