Always choose the simplest, most readable way to express your code logic. Unnecessary complexity makes code harder to understand and maintain. When writing conditionals, method calls, or data transformations, ask yourself: "Is there a clearer way to express this?"
Always choose the simplest, most readable way to express your code logic. Unnecessary complexity makes code harder to understand and maintain.
When writing conditionals, method calls, or data transformations, ask yourself: “Is there a clearer way to express this?”
Examples of simplifying expressions:
if reflection.belongs_to? && model.ignored_columns.include?(reflection.foreign_key.to_s)
if model.ignored_columns.include?(reflection.foreign_key.to_s)
2. Format array display consistently:
```ruby
# Instead of this verbose approach
filter_names = @filters.length == 1 ? @filters.first.inspect : "[#{@filters.map(&:inspect).join(", ")}]"
# Use the built-in inspect method
filter_names = @filters.length == 1 ? @filters.first.inspect : @filters.inspect
unless ActiveStorage.supported_image_processing_methods.any? { |method| method_name == method }
unless ActiveStorage.supported_image_processing_methods.include?(method_name)
4. Optimize iterations when appropriate:
```ruby
# Instead of filtering then iterating
enums_to_create = columns.select { |c| c.type == :enum && c.options[:values] }
enums_to_create.each do |c|
# process enum
end
# Check inside the loop to avoid an extra iteration
columns.each do |c|
next unless c.type == :enum && c.options[:values]
# process enum
end
def add_binds(binds, proc_for_binds = nil, &_)
def add_binds(binds, proc_for_binds = nil, &) ```
Remember that code is read far more often than it’s written. Optimizing for readability pays dividends throughout the lifecycle of your application.
Enter the URL of a public GitHub repository