File size: 9,639 Bytes
746555e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | # Quick Reference - AED Cheat Sheet
This is a quick reference, please read the entire guide for more details.
**This is the bare minimum to improve the performance of untrained coding assistants**
If you're a Rails developer, or come from a similarly structured framework, you'll find a lot of this inspired by the Rails conventions.
# Naming Conventions
## General Principles & Guidelines
- Avoid unnecessary jargon or slang.
- Never create a whole new lexicon for your code base by applying a theme.
- Code names for code bases are entirely acceptable and expected.
- Names that read like plain English are preferred
## Object & Attribute Naming Conventions
- Data models should be singular, and only concerned with their own individual behavior.
- Good: `Customer`
- Bad: `Customers`
- Classes should be name spaced according to the feature that is being implemented.
- Good: `Billing::ActivateNewCustomerSubscription`
- Bad: `NewCustomerSubscription`
- Class names should be short statements or phrases that clearly express the process being performed.
- Good: `PerformCustomerAccountLocking`
- Bad: `LockCustomers`
- Class attributes of non-enumerable primitive types should be phrased as short statements for what the intended purpose of the attribute is.
- Example class `Customer`
- Well named attribute: `first_name` or `full_name`
- Poorly named attribute: `name`
- Class attributes of enumerable (Array or Array-like) objects or types should be phrased with `list_of_` or `collection_of_` or `array_of_`
- Example class `Customer` has many `Order`s
- Well named attribute: `list_of_previous_orders` or `collection_of_previous_orders` or `array_of_previous_orders`
- Poorly named attribute: `orders` or `previous_orders`
- **Suggestion**: in dynamically typed languages, adding the object type to the end of the name can be very helpful by using the `_as_` phrasing in your naming
- Example: `list_of_previous_orders_as_hashes`
- Class attributes of non-primitive types should be named using short statements or phrases that accurately and clearly express how that attribute is to be used
- Example class: `Customer` with a `Subscription`
- Ideal: `currently_active_subscription`
- Acceptable: `active_subscription`
- Bad: `subscription`
- Class attributes for boolean types should be named as if the expression is a question beginning with `if`
- Example class: `Customer`
- Good: `has_a_valid_payment_method`
- Bad: `payment_method_present`
## Method Naming Conventions
- Method names should be phrases or statements that explain the process thats taking place
- When possible, include wording to describe the expected return type
- Method parameters should be named when possible
- Ideally as if reading a plain statement
## File & Folder Naming Conventions
These primarily apply to domain/business logic. Use your appropriate framework folder structure where necessary. These conventions are primarily if you are using an agent helper that is trained to work with more than 1 file at a time or perform file edits/updates autonomously.
- The file name should be a lower snake case of the primary class from the file.
- Class name `ProcessCustomersWithExpiredSubscriptions`
- Filename: `process_customers_with_expired_subscriptions.cr`
- Classes that are name spaced should be in folders named for that namespace
- Class name `Billing::ProcessCustomersWithExpiredSubscriptions`
- Filename: `billing/process_customers_with_expired_subscriptions.cr`
- Data models rarely need to be name spaced.
- Reserve this kind of naming for single-table inheritance or other very specific situations where you need a data model to be name spaced.
Example
```crystal
# File found under `billing/process_customers_with_expired_payment_methods`
class Billing::ProcessCustomersWithExpiredPaymentMethods
property collection_of_customers_that_have_expired_payment_methods : Array(Customer) = [] of Customer
property collection_of_customers_that_were_retried_and_failed : Array(Customer) = [] of Customer
property all_of_the_customers_have_been_processed : Bool = false
def initialize(@collection_of_customers_that_have_expired_payment_methods)
end
def perform
retry_customers_who_failed_payment_processing_with_an_expired_card
mark_customer_accounts_as_delinquent_and_prevent_further_use
end
private def retry_customers_who_failed_payment_processing_with_an_expired_card
# Your business logic goes here
end
private def mark_customer_accounts_as_delinquent_and_prevent_further_use
# Your business logic goes here
end
end
```
## Process Manager Conventions
Process managers are the starting point of your businesses internal domain-specific language (DSL).
These objects are _typically_ just a plain class that is not part of a specific framework. Many frameworks have some utilities that bleed out of the framework and into your business logic for common usecase tasks.
A more formal definition that encompasses what the spirit of the process manager is:
`Process Manager: a starting point in a business process where a workflow of one or more steps begins and ends, with the final product being the end of the computational process for the business.`
Process managers conform to the following:
- The `initialize` method receives all of the necessary information possible to perform the process
- Any necessary data organization should happen during the objects initialization step
- Prefer to use named parameters when initializing objects
- The entry point method `perform` is defined, and performs all of the methods necessary for the business task to be completed in a single method call
- A well written `perform` method will read almost like psuedo code when outlining each step that's being performed.
- Use read-only public accessor methods if the object is going to be used for anything other than returning a single result
- Use "middle managers" if your business process requires a secondary layer of business logic.
## Process "Middle" Manager Conventions
Just like process managers, these are objects that are in your codebase and represent your business process. As a middle manager, they typically are responsible for small parts of a larger process that has complex logic.
- Middle managers _should be named spaced to the process manager_. These are not meant to be re-used across the code base, just as an organization tool in a large process.
- Middle managers _do not_ use any other managers.
# Framework Conventions
These conventions have (amber)[https://amberframework.org] and (Ruby on Rails)[https://rubyonrails.org] in mind, but any RESTful routing app will tend to follow these well.
- The standard `Create`, `Edit`, `Update` and `Destroy` will only effect a single resource object.
- The typical `CRUD` actions should maintain the bare minimum logic to do the following:
- a single resource:
- Recieve whitelisted parameters
- update and validate the target object
- Render a response (successful or otherwise)
- multiple resources:
- Render a response with 0 or more of the desired resource (more commonly known as an index route)
- Any non-RESTful routes should do the following:
- Recieve and validate any incoming parameters or request body
- Use a process manager to perform any logic required for the response
- Render a response (successful or otherwise)
A well written Rails controller would look like the following:
```ruby
# app/controllers/customers_controller.rb
class CustomersController < ApplicationController
def create
@customer = Customer.new(customer_params)
if @customer.save
render json: @customer, status: :created
else
render json: @customer.errors, status: :unprocessable_entity
end
end
def update_payment_and_subscription
customer = Customer.find(params[:id])
new_payment_method = params[:payment_method]
if customer && new_payment_method
process_manager = Billing::UpdateCustomerPaymentAndSubscription.new(
customer: customer,
new_payment_method: new_payment_method
)
if process_manager.perform
render json: { message: 'Customer payment method and subscription updated successfully' }, status: :ok
else
render json: { error: 'Failed to update payment method and subscription' }, status: :unprocessable_entity
end
else
render json: { error: 'Invalid parameters' }, status: :bad_request
end
end
private def customer_params
params.require(:customer).permit(:first_name, :last_name, :email)
end
end
```
Here's the accompanying process manager:
```ruby
module Billing
class UpdateCustomerPaymentAndSubscription
attr_reader :customer, :new_payment_method
def initialize(customer:, new_payment_method:)
@customer = customer
@new_payment_method = new_payment_method
end
def perform
update_payment_method && update_subscription_status
end
private def update_payment_method
# Implement the logic to update the customer's payment method
customer.update(payment_method: new_payment_method)
end
private def update_subscription_status
# Implement the logic to update the customer's subscription status based on the new payment method
if customer.payment_method_valid?
customer.update(subscription_status: 'active')
else
customer.update(subscription_status: 'inactive')
end
end
end
end
```
---
*The AED cheat sheet · published **verbatim** from the author's original · back to the [reading order](README.md#reading-order)*
|