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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:07:55+00:00 2026-05-13T19:07:55+00:00

My textbook goes: a) Obj p = curScope.locals, last = null; while (p !=

  • 0

My “textbook” goes:

a)

Obj p = curScope.locals, last = null;
while (p != null) {
    if (p.name.equals(name)) error(name + " declared twice");
    last = p; p = p.next;
}
if (last == null) curScope.locals = obj; else last.next = obj;

Should I refactor along these lines?

b)

if (curScope.locals == null) {
  curScope.locals = obj;
} else {
  Obj p = curScope.locals;
  while (true) {
    if (p.name.equals(name))
      error(name + "declared twice");
    if (p.next == null) 
      break;
    p = p.next; // Good catch, @michaelmior
  }
  p.next = obj;
}

Edit: removed long variable names as I thought they would add clarity of intent but they did not. Thanks @danben. It’s obvious that less is more.

Edit: (as I cannot comment back) yes, I should have used the same variable names in the first place. Sorry about that.

Edit:

My thoughts on b) as an “improvement” were:

  • one less disposable variable name: p instead of p, last
  • p goes out of scope after traversal, identifier freed
  • on curScope.locals == null to begin with, more logical execution path
  • no need for pointer chasing like last = p; p = p.next
  • closing assignment not under an if/else

but I am unsure since in b):

  • the most frequently executed path is actually under an else branch

Is there a better c) ?

PS: my first question, forgive my initial confusion with the tool, I am still learning.

  • 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-13T19:07:55+00:00Added an answer on May 13, 2026 at 7:07 pm

    Now that you changed the variable names in (b), my original answer no longer applies, so I will say this:

    Modern compilers are very good at optimization and can render a lot of the programmer’s micro-optimizations obsolete. What they can’t do, however, is make your code easier for others to understand. So a good rule of thumb to follow is to first and foremost write your code such that it is easy to read.

    If, when you are done, you find that your program runs too slowly or takes up too much memory, then find the cause of that particular problem and start optimizing.

    Edit: in response to your comment, my answer is that I would not worry about refactoring. The only change I would make would be to reformat (a) such that it is more readable and follows coding conventions. Something like:

    Obj p = curScope.locals, last = null;
    while (p != null) {
        if (p.name.equals(name)) {
            error(name + " declared twice");
            last = p;
            p = p.next;
        }
    }
    if (last == null) {
        curScope.locals = obj; 
    } else {
        last.next = obj;
    }
    

    The extra few keystrokes will save you a lot of headache if you or anyone else decides to add a statement to one of those if or else clauses without remembering to add the braces.

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

Sidebar

Ask A Question

Stats

  • Questions 372k
  • Answers 372k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It should be inside the :html options, not on the… May 14, 2026 at 7:16 pm
  • Editorial Team
    Editorial Team added an answer In C function def cannot be nested and that is… May 14, 2026 at 7:16 pm
  • Editorial Team
    Editorial Team added an answer It's not possible to combine queries within one OleDbCommand. If… May 14, 2026 at 7:16 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.