Understanding the Drawbacks of ActiveRecord
🎧 Spotlights
I have been writing more in GoLang lately. Although I miss the expressiveness of Ruby, Go programs are more straightforward due to their rigid syntax and lack of modern language features.
Thoughts about ActiveRecord and complexity.
Snippets of Code is an engineering journal documenting learnings for experiments and projects.
Understanding the Drawbacks of ActiveRecord
Ruby on Rails offers programmers great flexibility and many valuable tools. Yet, we must use them wisely and consider how we structure our code.
While ActiveRecord associations can make the code appear to have no dependencies. It can lead to a state called "Big Ball of Mud," where everything can be accessed by anything without clear boundaries. As our application grows, we must consider dividing it into smaller, more manageable parts with clear boundaries.
Ruby on Rails applications make creating an HTTP layer on top of a database accessible. By combining data reads and writes in a single component, the ActiveRecord model.
The primary problem with this interface type is that it leads to duplication. ActiveRecord is a Ruby DSL for SQL, which is better for understanding the application's data access than business-related behavior.
The issue with ActiveRecord is that Object-Relational Mappers (ORMs) are always flawed abstractions. ORMs translate between a database table and objects in a system. ActiveRecord couples its implementation to the structure of tables and columns, which needs to be revised.
The underlying problem here is that ActiveRecord can act as a leaky abstraction.
If you are enjoying the learning journal, please ❤️
💻 👓
package cards
// FavoriteCards returns a slice with the cards 2, 6 and 9 in that order.
func FavoriteCards() []int {
return []int{2, 6, 9}
}
// GetItem retrieves an item from a slice at given position.
// If the index is out of range, we want it to return -1.
func GetItem(slice []int, index int) int {
if index < len(slice) && index >= 0 {
return slice[index]
}
return -1
}
// SetItem writes an item to a slice at given position overwriting an existing value.
// If the index is out of range the value needs to be appended.
func SetItem(slice []int, index, value int) []int {
if index < len(slice) && index >= 0 {
slice[index] = value
return slice
}
return append(slice, value)
}
// PrependItems adds an arbitrary number of values at the front of a slice.
func PrependItems(slice []int, values ...int) []int {
return append(values, slice...)
}
// RemoveItem removes an item from a slice by modifying the existing slice.
func RemoveItem(slice []int, index int) []int {
if index < 0 || index >= len(slice) {
return slice
}
return append(slice[:index], slice[index+1:]...)
}
1. FavoriteCards():
This function returns a slice containing the integers 2, 6, and 9 in that specific order.
A slice is a dynamically sized, flexible view into the elements of an array in Go.
2. GetItem(slice []int, index int) int:
This function retrieves an item from a slice at a given position specified by the index.
It checks if the index is within the bounds of the slice. If it is, it returns the value at that index. If not, it returns -1.
The
len(slice)
function returns the length of the slice.
3. SetItem(slice []int, index, value int) []int:
This function writes an item to a slice at a given position, overwriting an existing value if present.
It first checks if the index is within the bounds of the slice. If it is, it assigns the value to the specified index in the slice. If not, it appends the value to the end of the slice.
The
append()
function appends elements to the end of a slice.
4. PrependItems(slice []int, values ...int) []int:
This function adds an arbitrary number of values at the front of a slice.
It achieves this by appending the given values to the slice using the spread operator (...) followed by the slice itself.
This operation effectively adds the values at the beginning of the slice.
5. `RemoveItem(slice []int, index int) []int`:
This function removes an item by modifying the existing slice.
🧪 Next Actions
More Go practice
More profound research on Ruby on Rails architectural changes for big applications.
If you think someone will benefit from this content, please share.