Yes. That's definitively a good approach. I think there are many other
opportunities to improve StringBuilder and other subclasses of
CharSequence, but this isEmpty() method is a very important one. A lot of
developers might be constantly doing this kind of refactoring and it may
reduce the effort to do so. It also keeps the elegance up all the time,
instead of downgrading it a little bit by a length() invocation (replacing
an unary check by a binary operator check).

It might be a case where a small change in the core libs design doesn't
cause any impact (backwards compatible) and may call attention of lots of
developers to move complex concatenation scenarios to
StringBuilder/StringBuffer without any loss.

On Mon, Feb 11, 2013 at 1:20 PM, Peter Levart <peter.lev...@gmail.com>wrote:

> This could be a default method in CharSequence....
>
> Regards, Peter
>
>
> On 02/11/2013 12:54 PM, Hildeberto Mendonça wrote:
>
>> Hello,
>>
>> we have a scenario where a project with approximately 500K lines of code
>> is
>> going through a large refactoring. One of the changes was to replace
>> string
>> concatenations in loops by StringBuilder. Within the logic we found the
>> following condition:
>>
>> for(...) {
>>     if(str.isEmpty()) {
>>        // do something
>>     }
>> }
>>
>> After using StringBuilder,we had to change it to:
>>
>> for(...) {
>>     if(str.length() == 0) {
>>        // do something
>>     }
>> }
>>
>> ... which is a less readable code than the previous code. Having shown
>> that, we would like to know if there was a previous discution on the
>> inclusion or not of a method isEmpty in the StringBuilder. if yes, why it
>> is not there, if no, can we actually have it on Java8 or in a near future?
>>
>> We can actually find other similar String-like methods within
>> StringBuilder, such as charAt, replace and others.
>>
>> We would like to know if it is worthwhile to be discussed and taken
>> forward.
>>
>> Best regards,
>>
>>
>


-- 
Hildeberto Mendonça, Ph.D
Blog: http://www.hildeberto.com
Twitter: https://twitter.com/htmfilho <http://www.cejug.org>

Reply via email to