patterncsharpMajor
Checking opcode values
Viewed 0 times
opcodecheckingvalues
Problem
What troubles me are the empty
/**/ brackets. Is there a cleaner way of handling this logic?if (opcode == OP_0) { /* continue */}
else if (opcode == OP_1NEGATE) { /* continue */}
else if (opcode >= OP_1 && opcode <= OP_16) { /* continue */}
else { throw new Exception("decodeFromOpN called on non OP_N opcode"); }Solution
What you could use is a function that returns whether an opcode is valid.
Then in your code you posted, call the function, and throw an exception if it returns false.
Replace
And make a new class that extends from
private boolean isOpCode(Object opCode){
return opcode == OP_0 || opcode == OP_1NEGATE || (opcode >= OP_1 && opcode <= OP_16);
}Then in your code you posted, call the function, and throw an exception if it returns false.
if(!isOpCode(opCode)){
throw new OpCodeInvalidException("decodeFromOpN called on non OP_N opcode");
}Replace
Object with whatever the opcodes are.And make a new class that extends from
Exception. It's generally bad practice to throw Exception.Code Snippets
private boolean isOpCode(Object opCode){
return opcode == OP_0 || opcode == OP_1NEGATE || (opcode >= OP_1 && opcode <= OP_16);
}if(!isOpCode(opCode)){
throw new OpCodeInvalidException("decodeFromOpN called on non OP_N opcode");
}Context
StackExchange Code Review Q#43991, answer score: 41
Revisions (0)
No revisions yet.