Remove Setting Method
Before you begin: Constructors, properties, and object identity.
Remove Setting Method removes an assignment operation for a value that should be established once and remain fixed. Construction becomes the place to supply the value, and later code can no longer casually replace it through the public API. 1
Original version: a ticket can change its identifier
public sealed class Ticket
{
public string Id { get; set; } = "";
}A caller creates the ticket, assigns an identifier, and can assign another one later. Dictionaries, logs, or other records may already refer to the original identifier.
class Ticket:
def __init__(self):
self.id = ""The public attribute permits both initial assignment and arbitrary reassignment. The interface does not distinguish those operations.
Revised version: identity is supplied at construction
public sealed class Ticket
{
public string Id { get; }
public Ticket(string id) { Id = id; }
}var ticket = new Ticket("T-7");
Console.WriteLine(ticket.Id);The get-only property can be initialized by the constructor but not assigned by ordinary callers afterward. The identifier string is itself immutable.
class Ticket:
def __init__(self, ticket_id):
self._id = ticket_id
@property
def id(self):
return self._idticket = Ticket("T-7")
print(ticket.id)The public property has no setter. Direct assignment to _id remains possible by convention; this is not a security boundary.
Remove only changes the domain should not permit
A ticket’s identity may be fixed while its status legitimately changes. Removing every setter without providing meaningful update operations can make a useful object unusable.
Migrate construction sites before removing the setter. Some serializers and persistence frameworks require particular construction mechanisms; support those explicitly rather than keeping an unrestricted setter for all application code.
The example does not add nonempty-string validation. Its change is the timing and availability of assignment, which can break callers that previously mutated identity. Verify that those mutations were not part of the intended contract before calling the migration complete.