|
|
|
|
|
by mythz
4770 days ago
|
|
A good example of bloatware encouraged by a non-expressive language. Instead of adding a language feature that makes constructing any instance easier, in 2013 they're adding support for a builder on a single Calendar class. final Calendar calendar =
new Calendar.Builder()
.set(YEAR, 2013)
.set(MONTH, APRIL)
.set(DATE, 6)
.build();
For a similar API in C# you could use named parameters: var calendar = new Calendar(year:2013, month:Month.April, date:6);
or just set public properties in an object initializer: var calendar = new Calendar {
Year = 2013,
Month = Month.April,
Date = 6,
};
Likewise in Dart with named parameters: var calendar = new Calendar(year:2013, month:APRIL, date:6);
or using method cascades: var calendar = new Calendar()
..year = 2013
..month = APRIL
..date = 6;
For C#, Dart you get this for free when constructing any object. Java requires creating the boilerplate of a builder for every class?? and with all that effort it's still more verbose. |
|
So, what month is the date set to after that code? Builder.build(), on the other hand, can throw when called on a partly initialized builder. A builder also can (I don't know whether Java's builder do this) accept variants such as:
So, builders have their advantages.