# 关注点分离：何时“太多”分离？

``````def get_last_appearance_of_keyword(file, keyword):
with open(file, 'r') as file:
line_number = 0
for line in file:
if keyword in line:
line_number = line
return line_number
``````

``````def get_last_appearance_of_keyword(file, keyword):
with open(file, 'r') as text_from_file:
line_number = find_last_appearance_of_keyword(text_from_file, keyword)
return line_number

def find_last_appearance_of_keyword(text, keyword):
line_number = 0
for line in text:
if keyword in line:
line_number = line
return line_number
``````

``````def get_last_appearance_of_keyword(file, keyword):
text_from_file = get_text_from_file(file)
line_number = find_keyword_in_text(text_from_file, keyword)
return line_number

def get_text_from_file(file):
with open(file, 'r') as text:
return text

def find_last_appearance_of_keyword(text, keyword):
line_number = 0
for line in text:
if check_if_keyword_in_string(line, keyword):
line_number = line
return line_number

def check_if_keyword_in_string(text, keyword):
if keyword in string:
return true
return false
``````

6

## 6 答案

``````def get_last_appearance_of_keyword(text, keyword):
line_number = 0
for line in text:
if keyword in line:
line_number = line
return line_number
``````

6

Your last example is pretty good, but you could maybe simplify the for loop with a `text.GetLines(i=>i.containsKeyword)` or something.

*实用版：工作时停止。当它破裂时分开更多。

4

@cariehl你应该添加一个答案来争论这个案子。我想你会发现，实际上你需要在这些函数中使用更多的逻辑

“你不能有太多的分离。”我不认为这是真的。 OP的第三个例子就是将常见的python构造重写为单独的函数。我是否真的需要一个全新的功能才能执行'if x in y'？

```您的 find_last_appearance_of_keyword 可能更通用，并查找序列中项目的最后一个外观。为此，您可以使用现有实现，或进行可重用实现。此外，它可以采用不同的过滤器，以便您可以搜索正则表达式，或者不区分大小写的匹配项等。```

``````def get_last_appearance_of_keyword(filename, keyword):
with open(filename) as f:  # File-opening concern.
numbered_lines = enumerate(f, start=1)  # Line-numbering concern.
last_line = None  # Also a concern! Some e.g. prefer -1.
for line_number, line in numbered_lines:  # The searching concern.
if keyword in line: # The matching concern, applied.
last_line = line_number
# Here the file closes; an I/O concern again.
return last_line
``````

2

``````def lines_from_file(file):
with open(file, 'r') as text:
line_number = 1
lines = []
for line in text:
lines.append((line_number, line.strip()))
line_number += 1
return lines

def filter(l, func):
new_l = []
for x in l:
if func(x):
new_l.append(x)
return new_l

def contains(needle):
return lambda haystack: needle in haystack

def last(l):
length = len(l)
if length > 0:
return l[length - 1]
else:
return None
``````

``````lines = lines_from_file('./test_file')
filtered = filter(lines, lambda x : contains('some value')(x[1]))
line = last(filtered)
if line is not None:
print(line[0])
``````

1

1

@JoshuaJones单行函数没有固有的错误，但如果它们没有抽象出任何有用的东西，它们可能是一个障碍。用于返回两点之间的笛卡尔距离的单线函数非常有用，但是如果在文本中有``` return keyword的单行，那么只需在构建的顶部添加一个不必要的层。在语言结构中。```

@JoshuaJones在这种情况下，你抽象出一些有用的东西。在原始示例的上下文中，没有充分的理由存在这样的函数。 `中的`是一个常见的Python关键字，它实现了目标，并且它本身就具有表现力。围绕它编写包装函数只是为了使包装函数模糊代码，使其不那么直观。

@cariehl为什么`在文本`中返回关键字是不必要的层？如果您发现自己一直在lambda中使用该代码作为高阶函数中的参数，为什么不将它包装在函数中呢？

1. 满足所有要求（即它正确地做了它应该做的事）
2. 易读且易于理解
3. 易于重构
4. 遵循良好的编码惯例/原则

``````def get_last_match(file, predicate):
with open(file, 'r') as file:
line_number = 0
for line in file:
if predicate matches line:
line_number = line
return line_number
``````

0