Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8314175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:44:02+00:00 2026-06-08T20:44:02+00:00

I just stumbled over a weird piece of code while refactoring. It looks like

  • 0

I just stumbled over a weird piece of code while refactoring. It looks like a candidate for factoring out the common part of both readString()-methods, only it seems to be impossible (it’s a freaking brain teaser for me):

private final StringBuilder readStringBuilder = new StringBuilder(128);

@Override
public String readString() throws IOException {
    final int l = readInt();
    if (l <= 0) {
        switch (l) {
            case -1: return null;
            case 0: return "";
            default: throw new IOException("invalid string length encoding: " + l);
        }
    }
    readStringBuilder.setLength(0);
    for (int i=0; i<l; ++i) {
        readStringBuilder.append(readChar());
    }
    return readStringBuilder.toString();
}

@Override
public String readString(final StringCache cache) throws IOException {
    final int l = readInt();
    if (l <= 0) {
        switch (l) {
            case -1: return null;
            case 0: return "";
            default: throw new IOException("invalid string length encoding: " + l);
        }
    }
    readStringBuilder.setLength(0);
    for (int i=0; i<l; ++i) {
        readStringBuilder.append(readChar());
    }
    return cache.get(readStringBuilder, readStringBuilder);
}

You see that both methods do almost the same, the method body is completely identical, except for the return statement. But since there are early termination exits, I can’t find a method signature that can take the main body – natuarally the returns type would be StringBuilder, only in the early termination case it would be String…

Any ideas how to factor the body into a separate method? (and take note that toString() on an empty StringBuilder does create a new String instead of returning a constant string literal)

EDIT: Definition of the StringCache is:

public interface StringCache {
    public String get(final CharSequence charSeq, final CharSequence notFoundResult);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:44:03+00:00Added an answer on June 8, 2026 at 8:44 pm

    Looks like having readStringBuilder as an instance field is some form of (premature?) optimization. Assuming this is the case, how about something like this:

    private StringBuilder readStringHelper() throws IOException {
      StringBuilder readStringBuilder = new StringBuilder(128);
        final int l = readInt();
        if (l <= 0) {
            switch (l) {
                case -1: return null;
                case 0: break;
                default: throw new IOException("invalid string length encoding: " + l);
            }
        }
        for (int i=0; i<l; ++i) {
            readStringBuilder.append(readChar());
        }
        return readStringBuilder;
    }
    
    @Override
    public String readString() throws IOException {
        StringBuilder readStringBuilder = readStringHelper();
        return readStringBuilder==null ? null : readStringBuilder.toString();
    }
    
    @Override
    public String readString(final StringCache cache) throws IOException {
        StringBuilder readStringBuilder = readStringHelper();
        return readStringBuilder== null ? null : cache.get(readStringBuilder, readStringBuilder);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just stumbled over the following line of code cout << &Blahh << endl;
I just stumbled over a very interesting problem. Giving the following code: using System;
I just converted a code snippet from VB.NET to C# and stumbled over this
I just stumbled over this in some C# code...: public Foo Foo { get;
just stumbled onto a miss in my code, a function was called like this
Just stumbled upon this weird bug with php's DateTime object... Check this out: <?php
I just stumbled over a small problem when extending javascript objects using jQuery. When
I'm just starting to learn JavaScript, and stumbled over Underscore.js and jQuery . Underscore
WPF DataBindings used to make me happy. One thing I've stumbled over just now
I just browsed through Mark Probst's diploma thesis and stumpled over the following code:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.