Building Large Web Applications That Are Easy to Maintain
-
Upload
marsbased -
Category
Engineering
-
view
218 -
download
2
Transcript of Building Large Web Applications That Are Easy to Maintain
![Page 1: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/1.jpg)
BUILDING LARGE WEB APPLICATIONS
That won’t be a pain to maintain
![Page 3: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/3.jpg)
FIRST MONTHS BUILDING THE NEXT BIG THING ®
![Page 4: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/4.jpg)
ONE YEAR LATER
![Page 5: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/5.jpg)
–Henning Koch - Makandra CEO and author of Growing Rails Applications
“Large applications are large.”
AN INCONVENIENT SECRET
![Page 6: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/6.jpg)
WHAT DO YOU THINK THAT MAKES AN APP MORE MAINTAINABLE?
![Page 7: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/7.jpg)
3 STEPS TO GLORY
• Follow conventions
• Write code for human beings
• Always test
![Page 8: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/8.jpg)
–Arthur Forman
“Not everyone thinks the way you think, knows the things you know, believes the things you believe, nor acts the way you would act. Remember this and you will go a long way in getting along with people.”
FOLLOW CONVENTIONS
![Page 9: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/9.jpg)
THE EASY ONE: CODING STYLEif current_user redirect_to user_tasks_path(current_user, format: 'json') end !if(current_user) redirect_to(user_tasks_path(current_user, :format => ‘json')) end !unless current_user.nil? redirect_to( user_tasks_path( current_user, format: 'json' ) ) end !redirect_to(user_tasks_path(current_user, :format => 'json') if current_user !current_user && redirect_to(user_tasks_path(current_user, :format => 'json') !current_user and redirect_to(user_tasks_path(current_user, :format => 'json')
![Page 10: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/10.jpg)
• It is very easy to start doing it.
• You’ll be faster reading code that follows a certain style.
• Saves time avoiding trivial discussions.
• When your team grows, you want to maintain coherence amongst your codebase.
WHY YOU SHOULD BE ADOPTING A CODING STYLE?
![Page 11: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/11.jpg)
• Almost every Ruby on Rails company use and recommend a Style Guide:
• Thoughtbot: https://github.com/thoughtbot/guides/tree/master/style
• Plataformatec: http://guidelines.plataformatec.com.br/ruby.html
• MarsBased: Coming soon!
• If you can’t afford to build yours. Use the community ones!!
• https://github.com/bbatsov/ruby-style-guide
• https://github.com/bbatsov/rails-style-guide
WHY SHOULD YOU BE ADOPTING A CODING STYLE?
![Page 12: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/12.jpg)
FRAMEWORK CONVENTIONS• Model View Controller architecture
• CRUD centric RESTFUL approach
• ActiveRecord pattern
• Convention over configuration
• Unobtrusive Javascript
• DRY (Don’t repeat yourself)
• Concerns and modules to extend functionality
• etc.
![Page 13: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/13.jpg)
resources :projects !!class ProjectsController < ApplicationController ! def index end ! def show end ! def new end ! def create end ! def edit end ! def update end ! def destroy end end
EXAMPLE: RAILS CRUD BASED RESTFUL APPROACH
![Page 14: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/14.jpg)
resources :projects !!class ProjectsController < ApplicationController ! def index end ! def show end ! def new end ! def create end ! def edit end ! def update end ! def destroy end end
Publishing feature
EXAMPLE: RAILS CRUD BASED RESTFUL APPROACH
![Page 15: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/15.jpg)
resources :projects !!class ProjectsController < ApplicationController ! def index end ! def show end ! def new end ! def create end ! def edit end ! def update end ! def destroy end end
resources :projects do member do patch :publish patch :unpublish end end !!class TasksController < ApplicationController def index end ! def show end ! def new end ! def create end ! def edit end ! def update end ! def destroy end ! def publish end ! def unpublish end end
Publishing feature
EXAMPLE: RAILS CRUD BASED RESTFUL APPROACH
![Page 16: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/16.jpg)
resources :projects do member do patch :publish patch :unpublish end end
resources :projects do member do patch :publish patch :unpublish post :duplicate patch :add_collaborator patch :remove_collaborator end ! collection do get :search end end
Lots of features
EXAMPLE: RAILS CRUD BASED RESTFUL APPROACH
![Page 17: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/17.jpg)
A TRULY RAILS RESTFUL APPROACH
resources :projects !namespace :projects do resources :publishings, only: [:create, :destroy] resources :duplications, only: [:new, :create] resources :collaborator_addings, only: [:create, :destroy] resources :searches, only: [:create] end
EXAMPLE: RAILS CRUD BASED RESTFUL APPROACH
![Page 18: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/18.jpg)
DEVELOPERS CONVENTIONS: DESIGN PATTERNS
Context Strategy
Concrete Strategy 1
Concrete Strategy 2
Concrete Strategy 3
![Page 19: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/19.jpg)
YOUR OWN CONVENTIONS
• Your team is going to naturally develop a coding style and a set of conventions.
• Embrace them, document them and use them in all your projects.
• It’s part of your company’s culture.
![Page 20: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/20.jpg)
YOUR OWN CONVENTIONS
Don’t change your conventions every 2
months
![Page 21: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/21.jpg)
–David Heinemeier Hansson (@dhh) - Ruby on Rails creator
“We are not computer scientists. We are software writers.”
WRITE CODE FOR HUMAN BEINGS
![Page 22: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/22.jpg)
Ruby programming language
WHY IS IT IMPORTANT?
BrainFuck programming language
![Page 23: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/23.jpg)
USE INTENTION REVEALING METHOD NAMES
aka: Let your code speak to you
![Page 24: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/24.jpg)
INTENTION REVEALING METHOD NAMESclass SearchUsersService def search_users(params) # TODO: refactor conditions = {} conditions["hireable"] = params[:hireable] unless params[:hireable].nil? conditions["availability"] = params[:availability] unless params[:availability].nil? conditions["profile.tags_array"] = params[:tags].split(",") if params[:tags] conditions["category_ids"] = params[:category_ids].split(",") if params[:category_ids] ! if params[:location] filter = { :multi_match => { :query => params[:location], :fields => ["profile.country","profile.state","profile.city"] } } end ! if params[:q].present? || !conditions.empty? query = User.search(params[:q], where: conditions, execute: false) query.body[:query][:bool][:must] << filter if params[:location].present? query.execute.results else User.active.ordered_for_directory(params[:order]) end end end
![Page 25: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/25.jpg)
class SearchUsersService ! def search_users(params) conditions = extract_conditions_from(params) filter = extract_filters_from(params) ! if query_term_for?(params) || conditions.any? query = build_users_search_query(params, conditions, filter) query.execute.results else User.active.ordered_for_directory(params[:order]) end end !end
INTENTION REVEALING METHOD NAMES
![Page 26: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/26.jpg)
KISS: KEEP IT SIMPLE STUPID
![Page 27: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/27.jpg)
HOW DO YOU ACHIEVE SIMPLICITY?
Create reusable components only when you detect duplication.
Apply a design pattern to a problem only when simplifies your code and improves code readability.
Prefer being explicit than being implicit.
Maintain the abstraction low.
![Page 28: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/28.jpg)
KISS: KEEP IT SIMPLE STUPIDIt also applies to the 3rd party software you are using
![Page 29: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/29.jpg)
HOW TO BE SURE YOU ARE WRITING UNDERSTANDABLE CODE?
Pair programming
Git workflow based on pull requests and code revisions
Reading, reading and reading open-source applications and libraries
![Page 30: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/30.jpg)
ALWAYS TEST
– Anonymous
“The principle objective of software testing is to give confidence in the software.”
![Page 31: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/31.jpg)
WHY TESTING?
Bring confidence and peace of mind.
Discover hidden bugs in your application.
Save lots of time avoiding manual tests.
Allow you to refactor code.
![Page 32: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/32.jpg)
# TODO: It needs refactoring. Replace conditionals with # polymorphism and delegate responsibilities. def author_information if author.kind_of?(Teacher) if author.male? "Mr. #{author.full_name} - Dpt #{author.department}" elsif author.female? "Ms. #{author.full_name} - Dpt #{author.department}" end elsif author.kind_of?(Student) "Student of #{author.degree}" end end
DON’T FORGET TO REFACTORA green test is not the final step
![Page 33: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/33.jpg)
WHY DO CODERS STOP TESTING?
Testing require a different set of skills.
Some tests are hard to write (e.g. integration).
Tests also need maintenance.
Customers don’t mind if you test or not.
![Page 35: Building Large Web Applications That Are Easy to Maintain](https://reader034.fdocuments.us/reader034/viewer/2022042619/587c3c441a28ab5a1d8b545b/html5/thumbnails/35.jpg)
BONUS TRACK
Model
Controller
View
Decorators Presenters
Service Objects
Policy Objects
Facade