Previously, we discussed how MVC frameworks use meta-programming to simplify development. In this article, we will address another pattern commonly used by these frameworks.
In software development, configuration is a crucial step for customizing the behavior and appearance of an application. However, explicit configuration can be cumbersome and error-prone, especially in complex projects. The “Conventions over Configuration” (CoC) approach aims to simplify this process by establishing predefined conventions that developers can follow, thus avoiding the need for explicit configurations. This approach has gained popularity over the years due to the benefits it offers in terms of productivity, simplicity, and code consistency.
The Basics
The core principles of CoC are based on the premise that most projects share common patterns and conventions. By adopting predefined conventions, developers can avoid repetitive configurations and focus on the unique and more complex parts of the code. This reduces the time spent on configuration tasks and improves code readability and maintainability.
The CoC approach brings several benefits to software development. Firstly, by following predefined conventions, developers can easily integrate into existing projects, eliminating the need to learn all specific configurations. Additionally, CoC promotes code consistency, making it easier to be understood by different team members. Productivity is also enhanced as developers can focus on the unique aspects of the code rather than spending time on repetitive configurations.
Principles of CoC:
- Convention over Configuration: CoC promotes the use of predefined conventions instead of requiring explicit configuration. By following these conventions, developers can minimize the amount of configuration needed, leading to a simpler and more streamlined development process.
- Consistency: CoC ensures consistency across projects and within development teams. By adhering to established conventions, developers can create a common understanding and standardize practices, making it easier for team members to collaborate and maintain the codebase.
- Productivity: CoC aims to boost productivity by reducing the time and effort spent on configuration. Developers can focus more on writing application code and implementing business logic, rather than dealing with intricate configuration details.
- Simplicity: CoC simplifies software development by providing default behaviors and assumptions. Developers can rely on these defaults, reducing the complexity of decision-making and avoiding unnecessary configuration choices.
- Readability: CoC promotes code readability by enforcing consistent naming conventions, project structures, and coding styles. This improves code comprehension, making it easier for developers to understand and maintain the codebase.
- Rapid Prototyping: CoC is particularly beneficial for rapid prototyping and quick iterations. By following conventions, developers can rapidly set up a functional application without spending excessive time on configuration, allowing them to focus on experimenting and iterating their ideas.
Overall, the principles of CoC aim to enhance development efficiency, code maintainability, and collaboration within teams by leveraging standardized conventions and minimizing explicit configuration requirements.
Practical Examples
Let’s now take a look at some examples using Ruby on Rails, a web development framework that follows the “Convention over Configuration” principle.
Directory Structure
Rails has a standardized directory structure where different types of files have their predefined locations. For instance, models are placed in the “app/models” directory, controllers in “app/controllers,” and views in “app/views.” This structure facilitates the file’s location and organization, following clear conventions.
- app
- controllers
- products_controller.rb
- ...
- models
- product.rb
- ...
- views
- products
- index.html.erb
- show.html.erb
- new.html.erb
- edit.html.erb
- ...
- layouts
- application.html.erb
- ...
- config
- routes.rb
- ...
- db
- migrate
- ...
- public
- ...
- ...
Class and Table Naming
Rails follows a naming convention for model classes and database tables. For example, if you have a model class named “Product,” Rails will automatically assume that the corresponding table in the database is called “products.” This convention allows Rails to automatically associate classes with their respective databases, avoiding the need for explicit configuration.
# app/models/product.rb
class Product < ApplicationRecord
validates :name, presence: true
validates :price, numericality: { greater_than: 0 }
end
class CreateProducts < ActiveRecord::Migration[6.0]
def change
create_table :products do |t|
t.string :name
t.decimal :price
t.timestamps
end
end
end
In this example, the migration “CreateProducts” creates a table called “products” with columns “name” (string) and “price” (decimal). Rails follows conventions to create the table and columns with the appropriate data types.
Routes
Rails has a configuration file called “routes.rb” where application routes are defined. Rails uses conventions to automatically map routes to controller actions. For example, when defining “resources :products” in the routes file, Rails will automatically create RESTful routes to handle actions related to products, such as index, show, create, update, and delete.
# config/routes.rb
Rails.application.routes.draw do
get '/products', to: 'products#index'
post '/products', to: 'products#create'
get '/products/new', to: 'products#new'
get '/products/:id', to: 'products#show'
get '/products/:id/edit', to: 'products#edit'
patch '/products/:id', to: 'products#update'
delete '/products/:id', to: 'products#destroy'
end
The “Conventions over Configuration” approach offers an effective and simplified alternative for handling configurations in software development. By following predefined conventions, developers can save time, improve productivity, and maintain code consistency. Although challenges and considerations should be taken into account, the proper adoption of conventions can bring significant benefits to software development teams. The growing popularity of CoC is a reflection of its effectiveness in streamlining the development process.
Before concluding, I would like to recommend reading the article “Avoiding the Configuration Spiderweb” by Christopher Laine, published in May 2019. It appears to provide valuable insights into the potential pitfalls and challenges that can arise from misusing the Convention over Configuration approach. Understanding the limitations and considerations associated with any development practice is crucial for making informed decisions and mitigating potential headaches.