| Conditions | 32 |
| Total Lines | 79 |
| Code Lines | 64 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like it.cnr.istc.pst.platinum.ai.lang.ddl.v3.parser.DDLNumericParameterConstraint.parse() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | package it.cnr.istc.pst.platinum.ai.lang.ddl.v3.parser; |
||
| 30 | @Override |
||
| 31 | void parse() {
|
||
| 32 | String constraint_type = getText(); |
||
| 33 | if (constraint_type.equals("=")) {
|
||
| 34 | constraintType = DDLNumericParameterConstraintType.EQ; |
||
| 35 | } |
||
| 36 | if (constraint_type.equals(">")) {
|
||
| 37 | constraintType = DDLNumericParameterConstraintType.GT; |
||
| 38 | } |
||
| 39 | if (constraint_type.equals("<")) {
|
||
| 40 | constraintType = DDLNumericParameterConstraintType.LT; |
||
| 41 | } |
||
| 42 | if (constraint_type.equals(">=")) {
|
||
| 43 | constraintType = DDLNumericParameterConstraintType.GE; |
||
| 44 | } |
||
| 45 | if (constraint_type.equals("<=")) {
|
||
| 46 | constraintType = DDLNumericParameterConstraintType.LE; |
||
| 47 | } |
||
| 48 | if (constraint_type.equals("!=")) {
|
||
| 49 | constraintType = DDLNumericParameterConstraintType.NEQ; |
||
| 50 | } |
||
| 51 | if (getChild(0).getType() == ddl3Lexer.VarID) {
|
||
| 52 | leftTerm = getChild(0).getText(); |
||
| 53 | leftCoefficient = 1l; |
||
| 54 | } else if (getChild(0).getText().equals("*")) {
|
||
| 55 | long coefficient = getChild(0).getChild(0).getText().equals("INF") ? Long.MAX_VALUE - 1 : Long.parseLong(getChild(0).getChild(0).getText());
|
||
| 56 | if (getChild(0).getChild(0).getChildCount() > 0) {
|
||
| 57 | coefficient = getChild(0).getChild(0).getChild(0).getText().equals("-") ? -coefficient : coefficient;
|
||
| 58 | } |
||
| 59 | leftTerm = getChild(1).getChild(1).getText(); |
||
| 60 | leftCoefficient = coefficient; |
||
| 61 | } |
||
| 62 | if (getChild(1).getType() == ddl3Lexer.VarID) {
|
||
| 63 | rightVariables.add(getChild(1).getText()); |
||
| 64 | // rightCoefficients.add(1l); |
||
| 65 | rightCoefficients.add(1); |
||
| 66 | } else if (getChild(1).getText().equals("*")) {
|
||
| 67 | int coefficient = getChild(1).getChild(0).getText().equals("INF") ? Integer.MAX_VALUE - 1 : Integer.parseInt(getChild(1).getChild(0).getText());
|
||
| 68 | if (getChild(1).getChild(0).getChildCount() > 0) {
|
||
| 69 | coefficient = getChild(1).getChild(0).getChild(0).getText().equals("-") ? -coefficient : coefficient;
|
||
| 70 | } |
||
| 71 | rightVariables.add(getChild(1).getChild(1).getText()); |
||
| 72 | rightCoefficients.add(coefficient); |
||
| 73 | } else {
|
||
| 74 | int coefficient = getChild(1).getText().equals("INF") ? Integer.MAX_VALUE - 1 : Integer.parseInt(getChild(1).getText());
|
||
| 75 | if (getChild(1).getChildCount() > 0) {
|
||
| 76 | coefficient = getChild(1).getChild(0).getText().equals("-") ? -coefficient : coefficient;
|
||
| 77 | } |
||
| 78 | rightVariables.add(null); |
||
| 79 | rightCoefficients.add(coefficient); |
||
| 80 | } |
||
| 81 | for (int i = 2; i < getChildCount(); i++) {
|
||
| 82 | if (getChild(i).getText().equals("+")) {
|
||
| 83 | if (getChild(i).getChild(0).getType() == ddl3Lexer.VarID) {
|
||
| 84 | rightVariables.add(getChild(i).getChild(0).getText()); |
||
| 85 | rightCoefficients.add(1); |
||
| 86 | } else if (getChild(i).getChild(0).getText().equals("*")) {
|
||
| 87 | rightVariables.add(getChild(i).getChild(0).getChild(1).getText()); |
||
| 88 | rightCoefficients.add(Integer.parseInt(getChild(i).getChild(0).getChild(0).getText())); |
||
| 89 | } else {
|
||
| 90 | int coefficient = getChild(i).getChild(0).getText().equals("INF") ? Integer.MAX_VALUE - 1 : Integer.parseInt(getChild(i).getChild(0).getText());
|
||
| 91 | if (getChild(i).getChildCount() > 0) {
|
||
| 92 | coefficient = getChild(i).getChild(0).getText().equals("-") ? -coefficient : coefficient;
|
||
| 93 | } |
||
| 94 | absoluteValue += coefficient; |
||
| 95 | } |
||
| 96 | } else {
|
||
| 97 | if (getChild(i).getChild(0).getType() == ddl3Lexer.VarID) {
|
||
| 98 | rightVariables.add(getChild(i).getChild(0).getText()); |
||
| 99 | rightCoefficients.add(-1); |
||
| 100 | } else if (getChild(i).getChild(0).getText().equals("*")) {
|
||
| 101 | rightVariables.add(getChild(i).getChild(0).getChild(1).getText()); |
||
| 102 | rightCoefficients.add(-Integer.parseInt(getChild(i).getChild(0).getChild(0).getText())); |
||
| 103 | } else {
|
||
| 104 | long coefficient = getChild(i).getChild(0).getText().equals("INF") ? Integer.MAX_VALUE - 1 : Integer.parseInt(getChild(i).getChild(0).getText());
|
||
| 105 | if (getChild(i).getChildCount() > 0) {
|
||
| 106 | coefficient = getChild(i).getChild(0).getText().equals("-") ? -coefficient : coefficient;
|
||
| 107 | } |
||
| 108 | absoluteValue -= coefficient; |
||
| 109 | } |
||
| 162 |