我应该将代码分解为尽可能多的功能吗?

Wes*_*ley 1 oop performance design-patterns class function

假设我有一个用伪代码登录用户的函数

def login():
    # find user in db
    # check if user exists
    # check password
    # login user
Run Code Online (Sandbox Code Playgroud)

每个动作应该是它自己的功能吗?或者所有代码都应该留在那里?

编辑:我问这个是因为我通常只把我的所有代码都放在一个函数中,但是我的一个朋友将他需要做的所有事情放在多个函数中,然后填写他写下的函数的空白

恩.他会写这个:

def login():
    findUser()
    checkUser()
    checkPass()
    userLoggedin()
Run Code Online (Sandbox Code Playgroud)

然后他会创建这些函数并填充它们.

Jea*_*ean 6

我的建议是:

将其分解为有意义的,可重复使用的功能.这在代码库增长时最有用.

# find user in db         => re-usable
# check if user exists    => No. If you find it, it exists. So you don't need that.
                             If you don't find it, it does not. So both should be the same.
# check password          => re-usable
# login user              => re-usable
Run Code Online (Sandbox Code Playgroud)

注意:WWDC 2012会话视频标题为:

基础知识+习惯:将您的软件项目构建到最后

你可以看.目标受众主要是MAC开发人员,但他们的许多建议也适用于其他面向对象的语言.您需要一个免费的MAC开发者帐户才能访问它.