Skip to contentSkip to conversation

use switch to avoid multiple ifs

PATCH New Yann Simon 1 participant 1 message Jun 15, 2016

lore
Share & tools
lore

Yann Simon

author
#0 lore raw

PATCH 1/1 use switch to avoid multiple ifs

instead of using multiple ifs that will evaluate an expression x times, use a switch with cases to evaluate the
expression only one time.

---
 .../src/org/spearce/jgit/lib/Repository.java       |   29
+++++++++++++------
 1 files changed, 20 insertions(+), 9 deletions(-)
(quoted diff) +0 -0

b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java index b6efac1..f42bae5 100644

org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java +0 -0
@@ -389,16 +389,23 @@ public Object mapObject(final ObjectId id, final

String refName) throws IOExcept
if (or == null)
return null;
final byte[] raw = or.getBytes();
- if (or.getType() == Constants.OBJ_TREE)
+ switch (or.getType()) {
+ case Constants.OBJ_TREE:
return makeTree(id, raw);
- if (or.getType() == Constants.OBJ_COMMIT)
+
+ case Constants.OBJ_COMMIT:
return makeCommit(id, raw);
- if (or.getType() == Constants.OBJ_TAG)
+
+ case Constants.OBJ_TAG:
return makeTag(id, refName, raw);
- if (or.getType() == Constants.OBJ_BLOB)
+
+ case Constants.OBJ_BLOB:
return raw;
- throw new IncorrectObjectTypeException(id,
+
+ default:
+ throw new IncorrectObjectTypeException(id,
"COMMIT nor TREE nor BLOB nor TAG");
+ }
}

/**
@@ -449,12 +456,16 @@ public Tree mapTree(final ObjectId id) throws IOException {
if (or == null)
return null;
final byte[] raw = or.getBytes();
- if (Constants.OBJ_TREE == or.getType()) {
+ switch (or.getType()) {
+ case Constants.OBJ_TREE:
return new Tree(this, id, raw);
- }
- if (Constants.OBJ_COMMIT == or.getType())
+
+ case Constants.OBJ_COMMIT:
return mapTree(ObjectId.fromString(raw, 5));
- throw new IncorrectObjectTypeException(id, Constants.TYPE_TREE);
+
+ default:
+ throw new IncorrectObjectTypeException(id, Constants.TYPE_TREE);
+ }
}

private Tree makeTree(final ObjectId id, final byte[] raw) throws
IOException {

Signature · 1 line
1.6.0.6