Skip to main content
 首页 » 编程设计

python之缩进内联评论是好习惯吗

2024年10月01日7mengfanrong

我发现自己写了一些棘手的算法代码,我试着尽可能地评论它,因为我真的不知道谁会维护这部分代码。
按照这个想法,我写了很多 block 和内联评论,也尽量不过度评论它。但是,当我回到一周前写的代码时,我发现它很难阅读,因为大量的注释,尤其是行内注释。 我虽然将它们缩进(到 ~120char)可以提高可读性,但根据样式标准显然会使行太长。

这是原始代码的示例:

fooDataTableOccurrence = nestedData.find("table=\"public\".") 
if 0 > fooDataTableOccurrence:  # selects only tables without tag value "public-" 
    otherDataTableOccurrence = nestedData.find("table=") 
    dbNamePos = nestedData.find("dbname=") + 7   # 7 is the length of "dbname=" 
    if -1 < otherDataTableOccurrence:  # selects only tables with tag value "table=" 
        # database resource case 
        resourceName = self.findDB(nestedData, dbNamePos, otherDataTableOccurrence, destinationPartitionName) 
        if resourceName:  #if the resource is in a wrong path 
            if resourceName in ["foo", "bar", "thing", "stuff"]: 
                return True, False, False  # respectively isProjectAlreadyExported, isThereUnexpectedData and wrongPathResources 
            wrongPathResources.append("Database table: " + resourceName) 

下面是缩进内联评论的样子:

fooDataTableOccurrence = nestedData.find("table=\"public\".") 
if 0 > seacomDataTableOccurrence:                                                                       # selects only tables without tag value "public-" 
    otherDataTableOccurrence = nestedData.find("table=") 
    dbNamePos = nestedData.find("dbname=") + 7                                                          # 7 is the length of "dbname=" 
    if -1 < otherDataTableOccurrence:                                                                   #selects only tables with tag value "table="  
        # database resource case 
        resourceName = self.findDB(nestedData, dbNamePos, otherDataTableOccurrence, destinationPartitionName) 
        if resourceName:                                                                                # if the resource is in a wrong path 
            if resourceName in ["foo", "bar", "thing", "stuff"]: 
                return True, False, False                                                               # respectively isProjectAlreadyExported, isThereUnexpectedData and wrongPathResources 
            wrongPathResources.append("Database table: " + resourceName) 

代码是用 Python 编写的(我公司的遗留代码并没有完全遵循 PEP8 标准,所以我们不得不坚持使用它),但是我的观点不是关于代码本身的简洁性,而是关于注释。我正在寻找代码的可读性和易于理解之间的权衡,有时我发现很难同时实现这两者

哪个例子更好?如果没有,会是什么?

请您参考如下方法:

也许这是一个 XY_Problem?
评论能不能全删?

这是重构发布的代码的(快速且肮脏的)尝试:

dataTableOccurrence_has_tag_public = nestedData.find("table=\"public\".") > 0 
 
if dataTableOccurrence_has_tag_public: 
    datataTableOccurrence_has_tag_table = nestedData.find("table=") > 0 
 
    prefix = "dbname=" 
    dbNamePos = nestedData.find(prefix) + len(prefix) 
 
    if datataTableOccurrence_has_tag_table: 
        # database resource case 
        resourceName = self.findDB(nestedData,  
                                   dbNamePos,  
                                   otherDataTableOccurrence,  
                                   destinationPartitionName) 
 
        resource_name_in_wrong_path = len(resourceName) > 0 
 
        if resourceNameInWrongPath: 
 
            if resourceName in ["foo", "bar", "thing", "stuff"]: 
                project_already_exported = True 
                unexpected_data = False 
                return project_already_exported,  
                       unexpected_data,  
                       resource_name_in_wrong_path 
 
            wrongPathResources.append("Database table: " + resourceName) 

进一步的工作可能涉及从代码块中提取函数。