Re: Literate style with a nod to Hamcrest matchers - good or bad?
Steven Gordon <[email protected]> Sat, 13 Feb 2010 09:48:59 -0700
| Newsgroups | gmane.comp.programming.refactoring |
|---|---|
| Message-ID | <[email protected]> |
Is MyClass the only class in the code base that wants to know if one integer is divisible by another? Even if it is, is divisibility really a natural resposibility of MyClass (hard to tell from the name of the class and the lack of context). If you find the built-in integer operations not readable enough, maybe you need a MyInteger class. I have certainly been tempted to encapsulate java classes I found unnecessarily confusing (for example, Date and its related classes). SteveG On Fri, Feb 12, 2010 at 9:17 PM, simonvarley99 <[email protected]>wrote: > > > I was looking for a more literate way to reflect that the code was trying > to figure out if one number is divisible by another number. So a few beers > later and ... well, I ended up down this strange Hamcrest matcher path and > went from one line: > > public MyClass() { > ... > if ((number % divisor) == 0) { > ... > } > > to all of these, plus had to add another constructor. Surely there's an > easier way that I would have found if I stuck to just water? > > public MyClass() { > private int number = 0; > .... > if (thisNumber(number).isDivisibleBy(divisor)) { > ... > } > > private MyClass thisNumber(int number) { > return new MyClass(number); > } > public MyClass(int number) { > this.number = number; > } > private boolean isDivisibleBy(int divisor) { > return (this.number % divisor) == 0; > } > > all the tests still passed of course! > > > > [Non-text portions of this message have been removed] ------------------------------------ Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/refactoring/ <*> Your email settings: Individual Email | Traditional <*> To change settings online go to: http://groups.yahoo.com/group/refactoring/join (Yahoo! ID required) <*> To change settings via email: [email protected] [email protected] <*> To unsubscribe from this group, send an email to: [email protected] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/