我在Rails项目上运行reek时收到警告:
[36]:ArborReloaded :: UserStoryService#destroy_stories有大约8个语句(TooManyStatements)
以下是方法:
find . -type f -name *.jar | while read jarfile; do echo $jarfile; jar tf $jarfile; done
如何最小化此方法?
答案 0 :(得分:2)
首先,我发现类和方法大小对于查找可能需要重构的代码很有用,但有时您确实需要一个长类或方法。并且始终有一种方法可以缩短您的代码以克服这些限制,但这可能会降低您的可读性。因此,在使用静态分析工具时,我禁用了这种类型的检查。
此外,我不清楚为什么在删除故事时会出现错误,或者只是包含ID的错误消息以及错误发生的错误信息。
那就是说,我会写这样的方法,以减少显式的本地状态,并更好地区分问题:
def destroy_stories(project_id, story_ids)
project = Project.find(project_id) # I don't see a need for an instance variable
errors = story_ids.
select { |story_id| !project.user_stories.find(story_id).destroy }.
map { |story_id| "Error destroying user_story: #{story_id}" }
respond errors
end
# Lots of services probably need to do this, so it can go in a superclass.
# Even better, move it to @common_response's class.
def respond(errors)
# It would be best to move this behavior to @common_response.
@common_response.success = errors.any?
# Hopefully this works even when errors == []. If not, fix your framework.
@common_response.errors = errors
@common_response
end
您可以看到在您的框架中小心谨慎可以为您的组件节省大量噪音。