Literate style with a nod to Hamcrest matchers - good or bad?

"simonvarley99" <[email protected]> Sat, 13 Feb 2010 04:17:11 -0000
Newsgroups gmane.comp.programming.refactoring
Message-ID <[email protected]>
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!



------------------------------------

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/