关于分析一段代码的modularity和cohesion
大家好, 我是一个在美国的留学生 今天在面试的时候面试官给了我以下这段代码让我分析下面这段代码的modularity跟有没有缺少cohesion, 我由于只有用rails写过两个网站所以不是特别了解rails回答的时候根本胡扯, 所以我想问下有没有大神来讲解下这段代码. 这是这段代码的背景:"This segment of syntax implements aspects of user interface in an existing software. It binds the internal representation(s) of information that enable(s) the processing of Scenarios and the way the user interacts with those Scenarios."
class ScenariosController < ApplicationController
include ScenarioCopier
before_action :set_scenario, only: [:show, :copy, :edit, :tree, :update, :destroy]
respond_to :java
# GET /scenarios
def index
authorize! :index, Scenario
render json: Scenario.where(:locked => false, :university => current_user.university)
end
# GET /scenarios/1
def show
authorize! :read, @scenario
render json: @
end
# GET /scenarios/1/edit
def edit
end
def tree
authorize! :read, @scenario
render json: @scenario, choices: @scenario.complete_choices, serializer: ScenarioTreeSerializer
end
# POST /
def create
authorize! :create, Scenario
scenario = Scenario.new(scenario_params)
scenario.university = current_user.university
scenario.archived = false
scenario.locked = false
Scenario.transaction do
begin
save_scenario_with_root_node(scenario)
rescue ActiveRecord::RecordInvalid => invalid
render json: invalid.record.errors, status: :unprocessable_entity and return
end
render json: scenario, status: :created
end
end
# POST /scenarios/1/copy
def copy
authorize! :copy, @scenario
unless scenario_params[:name]
render json: {error: 'Must provide a new scenario name.'}, status: :unprocessable_entity and return
end
scenario_copy = copy_scenario(@scenario, {:copy_name => scenario_params[:name], :description => scenario_params[:description]}); return if performed?
render json: scenario_copy, status: :created
end
# PATCH/PUT /scenarios/1
def update
authorize! :update, @scenario
if @scenario.locked
render json: {error: 'Cannot update a locked scenario.'}, status: :bad_request and return
end
if @scenario.update(scenario_params)
head :no_content
else
render json: @scenario.errors, status: :unprocessable_entity
end
end
private
def set_scenario
@scenario = Scenario.find(params[:id])
end
def scenario_params
params.require(:scenario).permit(:name, :description, :archived, :root_node_id)
end
def save_scenario_with_root_node(scenario)
scenario.save!
root_node = initialize_root_node(scenario)
scenario.update!(root_node_id: root_node.id)
end
def initialize_root_node(scenario)
node = Node.create!(:scenario_id => scenario.id)
node.update!(is_start: false, label: 1)
node
end
end
class ScenariosController < ApplicationController
include ScenarioCopier
before_action :set_scenario, only: [:show, :copy, :edit, :tree, :update, :destroy]
respond_to :java
# GET /scenarios
def index
authorize! :index, Scenario
render json: Scenario.where(:locked => false, :university => current_user.university)
end
# GET /scenarios/1
def show
authorize! :read, @scenario
render json: @
end
# GET /scenarios/1/edit
def edit
end
def tree
authorize! :read, @scenario
render json: @scenario, choices: @scenario.complete_choices, serializer: ScenarioTreeSerializer
end
# POST /
def create
authorize! :create, Scenario
scenario = Scenario.new(scenario_params)
scenario.university = current_user.university
scenario.archived = false
scenario.locked = false
Scenario.transaction do
begin
save_scenario_with_root_node(scenario)
rescue ActiveRecord::RecordInvalid => invalid
render json: invalid.record.errors, status: :unprocessable_entity and return
end
render json: scenario, status: :created
end
end
# POST /scenarios/1/copy
def copy
authorize! :copy, @scenario
unless scenario_params[:name]
render json: {error: 'Must provide a new scenario name.'}, status: :unprocessable_entity and return
end
scenario_copy = copy_scenario(@scenario, {:copy_name => scenario_params[:name], :description => scenario_params[:description]}); return if performed?
render json: scenario_copy, status: :created
end
# PATCH/PUT /scenarios/1
def update
authorize! :update, @scenario
if @scenario.locked
render json: {error: 'Cannot update a locked scenario.'}, status: :bad_request and return
end
if @scenario.update(scenario_params)
head :no_content
else
render json: @scenario.errors, status: :unprocessable_entity
end
end
private
def set_scenario
@scenario = Scenario.find(params[:id])
end
def scenario_params
params.require(:scenario).permit(:name, :description, :archived, :root_node_id)
end
def save_scenario_with_root_node(scenario)
scenario.save!
root_node = initialize_root_node(scenario)
scenario.update!(root_node_id: root_node.id)
end
def initialize_root_node(scenario)
node = Node.create!(:scenario_id => scenario.id)
node.update!(is_start: false, label: 1)
node
end
end
...全文
相关推荐
软件工程专业英语.pdf
模块性(Modularity)是指软件设计的模块性和独立性。单模块软件(Monolithic Software)是指软件系统中的单个模块。
【信息科学与工程学】【数据中心】 第八篇 智算中心训练业务流量分析
层次性明显:从服务器到机架到POD到全局,流量模式逐层聚合时空相关性:训练迭代产生周期性模式,拓扑结构决定空间模式可预测性:基于训练作业的参数可预测流量模式可优化性:通过智能调度和路由可优化流量分布设计关键原则:匹配原则:流量模式与拓扑结构匹配均衡原则:流量面积在空间中均衡分布局部性原则:最大化流量局部性,减少跨域流量可预测原则:利用训练作业的可预测性优化网络技术发展趋势:AI驱动优化:使用机器学习预测和优化流量模式可编程网络:通过P4等实现流量感知的智能网络协同设计:训练框架与网络协同设计量子启发。
第2章软件质量评价.pptx
模块化有助于降低复杂性,提高代码的可读性和可维护性。模块的划分应遵循一定的原则,例如,模块的内聚度(closeness of cohesion)和耦合度(coupling)是衡量模块质量的重要指标。
软件工程PDF课件 08_OODesign面向对象设计.pdf
面向对象设计是软件工程中的核心环节,它基于分析模型,利用面向对象技术进行系统软件设计。这一过程主要包括系统设计和详细设计(或对象设计)两个层次。
SASD_ver2.ppt
**系统分解(Decomposition)**:将复杂的系统拆分为更小的部分,便于理解和管理。2. **模块化(Modularity)**:通过分解来重新设计、组装和复用系统组件。3.


