Last active
August 29, 2015 14:15
-
-
Save pmn4/37281904dbeaef69bf1c to your computer and use it in GitHub Desktop.
Modular Sinatra Refactor vs Code Complexity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?