在复杂方法中进行局部def的良好实践

时间:2014-10-18 23:16:47

标签: scala readability

我准备写一个相当长的治疗,我希望超过130行。在Java中,我会编写一个公共方法和几个私有方法来将逻辑拆分为更易读的代码片段。

但是,这些私有方法对这个类中的任何其他方法都没用,那么我想将它们作为我的公共方法的本地方法,因为Scala启用了它。然而,在这种情况下,这种方法甚至比没有本地定义更长,因为我不需要多次调用这些本地函数,所以它似乎没用。

总结一下,我必须在

之间做出选择
def method {
    def localDef1 = { 
        // 20-30 lines of code
    }
    def localDef2 = { 
        // 20-30 lines of code
    }
    def localDef3 = { 
        // 20-30 lines of code
    }
    // 30-40 lines of code calling localDef[1-3] once each
}

def method {   
    // 30-40 lines of code calling privateDef[1-3] once each
}
private def privateDef1 = { 
    // 20-30 lines of code
}    
private def privateDef2 = { 
    // 20-30 lines of code
}
private def privateDef3 = { 
    // 20-30 lines of code
}

对于类似的内容,可读性方面的最佳做法是什么?

0 个答案:

没有答案