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

  • Home
  • SEARCH
  • 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 8779299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:45:08+00:00 2026-06-13T19:45:08+00:00

Im trying to figure out what is the best practice when initializing certain variables…

  • 0

Im trying to figure out what is the best practice when initializing certain variables…
My code looks like this at the moment:

int nHexCount = 0;
int prevState = sc.state;

bool bOnlySpaces = true;
bool bIsValidLabel = true;
bool bIsHotstring = false;
bool bIsValidName = true;
bool bIsValidExpStart = false;                         

bool fInExpression = false;
bool fInStringBlock = (sc.state == SCE_AHKL_STRINGOPTS || sc.state == SCE_AHKL_STRINGBLOCK);

for (; sc.More(); sc.Forward()) {

    if (sc.atLineStart) {
        if (!fInStringBlock && sc.state != SCE_AHKL_COMMENTBLOCK)
            sc.SetState(SCE_AHKL_DEFAULT);

        // Reset Status
        prevState = sc.state;

        bOnlySpaces = true;
        bIsValidLabel = true;
        bIsHotstring = false;
        bIsValidName = true;
        bIsValidExpStart = false;

        fInExpression = false;
    }

...

So as you can see most of these variables are reset each time my program finds a new line in the edit component i am working on…

The question would be:

Is it better programming practice declaring and initializing all those variables inside the for loop or should i leave it like it is at the moment?

  • 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-13T19:45:09+00:00Added an answer on June 13, 2026 at 7:45 pm

    You should always reduce the scope of the variables as much as possible. This will improve the maintainability of your code, and reduce the chance of bugs.

    // bad
    int i, j, k;
    k = 0;
    for (i = 0; i < X, ++i)
    {
      j = foo(i);
      k += j;
    } 
    
    bar(k);
    

    … vs …

    // better
    int k=0; // needs scope outside loop
    for (int i = 0; i < X, ++i)
    {
      int j = foo(i);
      k += j;
    } 
    
    bar(k);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to figure out the best way to do this: Should I do something
I'm trying to figure out the best way to do this, but I'm getting
I'm trying to figure out the best practice to setup hierarchy data that i
I am trying to figure out the best practice of communication between a TabActivity
I'm trying to figure out the best practice for implementing a complex algorithm on
I am trying to figure out best practice for N-Tier application design. When designing
I'm trying to figure out the best practice for dealing with the following situation:
I'm trying to figure out what the best practice is for using jQuery in
Just trying to figure out what's the best practice: when using method that takes
Folks, I'm trying to figure out a pattern or best practice to translate data

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.