Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class Wrapper {
public static String wrap(String s, int col) {
return new Wrapper(col).wrap(s);
}
private int col;
private Wrapper(int col) {
this.col = col;
}
private String wrap(String s) {
if (s.length() <= col)
return s;
int space = (s.substring(0, col).lastIndexOf(' '));
if (space != -1)
return breakLine(s, space, 1);
else if (s.charAt(col) == ' ')
return breakLine(s, col, 1);
else
return breakLine(s, col, 0);
}
private String breakLine(String s, int pos, int gap) {
return s.substring(0, pos) + "\n" + wrap(s.substring(pos + gap), col);
}
}
@sortega

This comment has been minimized.

Copy link

@sortega sortega commented Oct 9, 2011

I find confusing the 3-arguments function breakLine. Furthermore, the gap argument looks like a flag for me and, as you pointed out in Clean Code, we must avoid flag arguments.

Would you do this kata differently nowadays?

@csesteban

This comment has been minimized.

Copy link

@csesteban csesteban commented Oct 9, 2011

I've cloned this gist and changed some things:
git://gist.github.com/1274003.git

@joebowbeer

This comment has been minimized.

Copy link

@joebowbeer joebowbeer commented Dec 25, 2012

This is a more efficient last-index computation for line 15:

int space = s.lastIndexOf(' ', col);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment