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 6237467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:02:06+00:00 2026-05-24T11:02:06+00:00

private static final Group[] toGroups (String string) { int partialGroupSize = string.length() % GROUP_SIZE;

  • 0
 private static final Group[] toGroups (String string)
    {
        int     partialGroupSize = string.length() % GROUP_SIZE;
        boolean hasPartialGroup  = partialGroupSize != 0;
        int     nGroupOffset     = hasPartialGroup ? 1 : 0;
        int     nGroup           = string.length() / GROUP_SIZE + nGroupOffset;
        Group[] groups           = new Group[nGroup];
        for (int i = 0; i < nGroup; i++) {
            boolean isFirstGroup = i == 0;
            int beginIndex = isFirstGroup ? i :
                             GROUP_SIZE * (i - nGroupOffset) + partialGroupSize;
            int   endIndex = isFirstGroup && hasPartialGroup ?
                             beginIndex + partialGroupSize :
                             beginIndex + GROUP_SIZE;
            groups[i] = new Group(Integer.parseInt(string.substring(beginIndex,
                                                                      endIndex)));
        }
        return groups;
    }

My first question is one that I found different discussions about, but I still don’t know what I should do.
The method toGroups is only called once in the program, therefore .length() is only called twice on string of toGroups in the program. So with regards to performance and readability, should I replace string.length() with length where int length = string.length();?
Id est:

int     length           = string.length();
int     partialGroupSize = length % GROUP_SIZE;
boolean hasPartialGroup  = partialGroupSize != 0;
int     nGroupOffset     = hasPartialGroup ? 1 : 0;
int     nGroup           = length / GROUP_SIZE + nGroupOffset;

My second question is: within the for loop, given that when the predicate of the beginIndex conditional assignment, isFirstGroup, is true, i must be 0; should I replace the consequent of the beginIndex conditional with a literal 0?
Id est:

int beginIndex = isFirstGroup ? 0 :
                 GROUP_SIZE * (i - nGroupOffset) + partialGroupSize;

I reason that because the consequent of beginIndex is always 0, using the equivalent i iterator creates an ambiguity in the constancy/variableness of the consequent.

  • 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-05-24T11:02:06+00:00Added an answer on May 24, 2026 at 11:02 am

    So with regards to performance and readability …

    Readability is a more important attribute of a well written program than performance. Indeed, readability is second only to correctness (IMO).

    Performance is generally speaking a minor issue. And in the cases where it is important, large-scale algorithmic efficiency is more critical than “micro” issues such as whether you call a fast method such as String.length() once or twice.

    (Indeed, there is a good chance that the JIT compiler can figure out that it can safely “hoist” the String.length() call out of the loop. And even if it can’t, the next generation of JIT compiler may be able to do this optimization.)

    In general, it is best to leave micro-optimizations like this to the JIT compiler, and focus your effort on more important things. Only micro-optimize if you have clear evidence (e.g. from profiling) of a significant bottleneck in your code.

    Also, is there anything I should do that I didn’t do or is there anything I did that I shouldn’t do, stylistically or otherwise?

    Yes.

    Don’t line all your declaration / assignment statements up like that:

    • It is a waste of time.
    • It doesn’t make your code any more readable. (And if I’m reading your code, I don’t care about its artistic qualities …)
    • It violates all mainstream coding standards.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to pass the following String to a PreparedStatement : private static final
I have a method lets say: private static String drawCellValue( int maxCellLength, String cellValue,
Should I create private static final String = Some exception message or leave it
final static private Pattern includePattern = Pattern.compile(^\\s+([^\\s]*)); ... Matcher mtest = includePattern.matcher( this.txt); String
I have the following (Java) code: public class TestBlah { private static final String
I have a prepared statement as so: private static final String SQL_LIST_GROUP = SELECT
private static final String DESTINATION_DIR_PATH =/files; private File destinationDir; public void init(ServletConfig config) throws
Does a static final String inside a private static method instantiate a new object
private static String CONSUMER_KEY = mrnCC41nxtwkdFAmToEhtg; private static final String CONSUMER_SECRET = kmmVuahEspGvdl14aCD1GSBZpeHbxvkpAez7aKaaQ; EditText
private static final int REQUEST_CONNECT_DEVICE = 1; private static final int REQUEST_ENABLE_BT = 2;

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.