Write code that writes code
Wouldn’t you rather write a program than type the same thing 100 times? Me too, let’s talk about how.
I fell in love
Let me tell you about a time I fell in love. With code that writes code. In 2016, I was working with a terrible library in Java. One such issue was that instead of using reflection, there was a large amount of boiler plate. My team had to patch it because it was incomplete and buggy. Every field we wanted to add required 12 lines of code. 12 lines that involved snake_case, camelCase, and GoCase.
Here’s an example for one field total in cents:
@XmlElement(name = "total_in_cents")
private Integer totalInCents;
public Integer getTotalInCents() {
return totalInCents;
}
public Integer setTotalInCents(final Object totalInCents) {
this.totalInCents = integerOrNull(totalInCents);
}
Notice this has “total_in_cents”, “totalInCents” and “TotalInCents”. And I had to add ~60 of these, for a total of ~800 lines. Doing this by hand would be grueling non-stop typing. Typing tha…