تخطَّ إلى المحتوى

Public Mutable Field (ARCH6186)

هذا المحتوى غير متوفر بلغتك بعد.

Key ruby.quality.public-mutable-attr · Severity warning · Category quality

Directly exposed mutable fields (attr_accessor) bypass validation and invariants; the object’s internal state can be corrupted from anywhere.

class Account
attr_accessor :balance
def initialize
@balance = 0
end
end
class Account
attr_reader :balance
def initialize
@balance = 0
end
def deposit(amount)
raise ArgumentError if amount.negative?
@balance += amount
end
end

Drop the writer and route state changes through explicit methods that enforce validation.