Skip to content

Instantly share code, notes, and snippets.

@unclebob
Created October 3, 2010 20:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save unclebob/608933 to your computer and use it in GitHub Desktop.
Save unclebob/608933 to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link

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

@joebowbeer
Copy link

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