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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:30:39+00:00 2026-06-01T13:30:39+00:00

While programming, I often come across the following scenario (described in code). I define

  • 0

While programming, I often come across the following scenario (described in code). I define variables in the block that they are going to be used.

    int maximumLengthOfToken = 0;

    while(fileScanner.hasNextLine())
    {
       String line; // definition
       line = fileScanner.nextLine();

       Scanner lineScanner; // definition
       lineScanner = new Scanner(line);

       String token = lineScanner.next(); // definition and assignment
       if(stringToMatch.endsWith(token))
        {
                 if(token.length() >= maximumLengthOfToken )
                  {
                     maximumLengthOfToken = token.length();  
                     builder.append(line);
                     builder.append("\n\n\n");
                  }
        }
    }
    fileScanner.close();

However, sometimes, for the sake of “neatness” and also because I come from a C background, I like to define all variables at the top, regardless of where they are used. So I do the following –

    int maximumLengthOfToken = 0;
    String line; // definition
    Scanner lineScanner; // definition
    String token; // definition

    while(fileScanner.hasNextLine())
    {
       line = fileScanner.nextLine();
       lineScanner = new Scanner(line);

       token = lineScanner.next(); // definition

               if(stringToMatch.endsWith(token))
                {
                 if(token.length() >= maximumLengthOfToken )
                  {
                     maximumLengthOfToken = token.length();  
                     builder.append(line);
                     builder.append("\n\n\n");
                  }
        }
    }
    fileScanner.close();

“Ah! It looks so neat”, I think.

However, does this have any effect on the performance of the code? Would the first style lead to faster executing code or the second style? Also, what is the convention in such cases? I believe it is the first style.

  • 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-01T13:30:41+00:00Added an answer on June 1, 2026 at 1:30 pm

    No. There will be no performance difference.

    If you doubt this, ask yourself, “Will it take longer for the computer to look up my variable if it’s 2000 bytes away, than it will if it’s 200 bytes or 20 bytes away?” The answer should be no.

    The only case where it might make a difference is if your variable drops out of the processor cache (see locality) before it gets used, in which case moving the variable closer to where it is being used might improve speed. But it would take a lot of code between the variable and its use for that to happen.

    It is also possible that the compiler will compile your method down to the same bytecode anyway. In short, I think you don’t have to worry about it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While programming with python I am often confused with the following ambiguity: should it
Hey, while programming stuff, I have found that it is often very helpful to
While seeing a programming interview site, I came across code which swap adjacent elements
I'm a VMware user and far too often I use keyboard shortcuts while programming.
I've just started using ASP, so while programming the code behind in C# I
While programming under eclipse+PyDev and using Flask framework, I noticed that the auto-organizing imports
Often in programming, it is a very common requirement that some piece of functionality
While programming software stored in a Subversion repo, I often modify some files, then
Lately, I've decided to start using Perl::Critic more often on my code. After programming
While programming, the longest part is a build on my ASP.NET Website project is

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.