55502f40dc8b7c769880b10874abc9d0

I got this:

a) // see below

to this:

b) // see below

do you have a better c) ?

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;

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;
  }
  p.next = obj;
}

Refactorings

No refactoring yet !

D41d8cd98f00b204e9800998ecf8427e

foo

March 1, 2010, March 01, 2010 00:38, permalink

No rating. Login to rate!
Obj lookup(String name) {
  for (Obj p = curScope.locals; p != null; p = p.next)
    if (p.name.equals(name))
      return p;

    return null;
}

Obj insert(String name) {
  Obj found = lookup(name);

  if (found)
    error(name + " declared twice");

  // else
  if (curScope.locals == null) {
    curScope.locals = found;
    found.next = null;
  } else {
    found.next = curScore.locals.next;
    curScore.locals.next = found;
  }
}

Your refactoring





Format Copy from initial code

or Cancel