When I am thinking about changing / enhancing some code, I have usually two things (regarding the code for review) in mind:
- Whenever there is some method that gets called in every if-else / switch-case, think about using some variable to make it single call. (This includes same method on different objects of common type - not aplicable to this case, but for the record)
- Check all conditions (and variables), if they are really required. (This includes joined conditions, when one renders to be redundant because of another already met. Sometimes I see variables that are used to avoid
goto
, but it turns out it can be done without both by reorganizing it - again not aplicable to this code review, but for the check-list)
My final solution would look like this:
class CustomTextBox : TextBox {
public void AppendLine(string textToAppend) {
string current = this.Text;
if (current.Length > 0
&& !current.EndsWith(Environment.NewLine))
textToAppend = Environment.NewLine + textToAppend;
this.AppendText(textToAppend);
}
}
EDIT: I have deleted all redundant / wrong parts. The check for empty text is needed, and TextLength
is a property of WinForms TextBox, not the one in WPF.