Skip to content

Instantly share code, notes, and snippets.

@pmn4
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save pmn4/37281904dbeaef69bf1c to your computer and use it in GitHub Desktop.

Select an option

Save pmn4/37281904dbeaef69bf1c to your computer and use it in GitHub Desktop.
Modular Sinatra Refactor vs Code Complexity
class App < Sinatra::Base
get '/users' do
User.all.to_json
end
post '/users' do
User.create!(params)
204
end
get '/users/:id' do |id|
User.find(id).to_json
end
# etc
end
# the App looks really nice, but now this is getting a slightly higher Complexity score
module UserRoutes
def self.registered(app)
app.get '/users' do
User.all.to_json
end
app.post '/users' do
User.create!(params)
204
end
app.get '/users/:id' do |id|
User.find(id).to_json
end
# etc
end
end
class App < Sinatra::Base
register UserRoutes
end
# combining registered + namespace + routing is instantly complex
module UserRoutes
def self.registered(app)
app.namespace '/users' do
get do
User.all.to_json
end
post do
User.create!(params)
204
end
get '/:id' do |id|
User.find(id).to_json
end
# etc
end
end
end
class App < Sinatra::Base
register UserRoutes
end
@pmn4

pmn4 commented Feb 19, 2015

Copy link
Copy Markdown
Author

I would like to move from the original, a file which has become very long, to the desired implementation, but Code Climate and Rubocop consider the complexity of the third implementation to be much greater than the original.

What suggestions can be made to both modularize and maintain complexity?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment