3 ms·
I've come to subscribe to a school of thought that says a constructor should never do anything other than assign its arguments to fields. Perhaps validate them,
by twic 12y ago
I've come to subscribe to a school of thought that says a constructor should never do anything other than assign its arguments to fields. Perhaps validate them, i suppose. But not compute with them - if you want to do computation for the creation of an object, write a factory method.
For me and my fellow disciples, it would therefore be really nice to have a constructor syntax that does this implicitly. So instead of writing:
class Customer {
new(customerNumber, name, address) {
_customerNumber = customerNumber
_name = name
_address = address
}
}
We could just write something like:
class Customer {
new(customerNumber, name, address)
}
Or, if you like Scala:
class Customer(customerNumber, name, address) {}
I'm a Java programmer by day, and some thoroughly depressing fraction of the lines of code in my codebases are of the form:
private final AmazingObjectOfSomeKind amazing;
private final BrilliantObjectOfSomeOtherKind brilliant;
private final CrappyObjectOfAnyKind crappy;
public Foo(AmazingObjectOfSomeKind amazing,
BrilliantObjectOfSomeOtherKind brilliant,
CrappyObjectOfAnyKind crappy) {
this.amazing = amazing;
this.brilliant = brilliant;
this.crappy = crappy;
}
Users of other languages quite rightly point and laugh at me for this. Now, Wren doesn't have those types cluttering up the place, nor does it have the field declarations, but it does still have unnecessary verbosity, and that verbosity provides crevices for bugs to hide in (add a parameter to a constructor, forget to add a corresponding assignment). It might be nice to add a dash of syntactic sugar to eliminate this problem.
Another facet of this school's approach is that objects are generally immutable. So instead of:
class Account {
withdraw(amount) { _balance = _balance - amount }
}
We would write:
class Account {
withdraw(amount) { new Account(_balance - amount) }
}
(Although in fact, a bank account might be one of the cases where we wouldn't do this, but never mind)
That gets old as soon as you have a few fields. Like:
class Account {
withdraw(amount) { new Account(_accountNumber, _sortCode, _balance - amount, _accountType) }
}
There's another bit of Scala sugar that is nice here - an automatic copy method which takes keyword arguments to override particular fields. Like:
class Account {
withdraw(amount) { copy(balance = _balance - amount) } // other fields implicitly copied
}
Might something like that might be useful enough to justify its weight?
- munificent 12y ago> it would therefore be really nice to have a constructor syntax that does this implicitly. Agreed. I'm on the Dart team at work, and in Dart you'd do: class Customer { int customerNumber; String name; String address; Customer(this.customerNumber, this.name, this.address); } You do still have to declare them. I've thought about adding something similar to Wren. Since fields are already syntactically distinguished, it could be as simple as: class Customer { new(_customerNumber, _name, _address) {} } I've hesitated because I want to see how much this is really needed in real-world code. I'm trying to keep things as simple as possible at least as first. > There's another bit of Scala sugar that is nice here - an automatic copy method which takes keyword arguments to override particular fields. I'm not opposed to the idea, but it probably doesn't carry its weight. Wren is incredibly minimal. I like immutability a lot, but for scripting language where program size is relatively small anyway, I don't think it's quite as high a priority.