Here's a little test for you. You think it's hard to judge whether a method compiles to fewer than 50 bytes? Well, train yourself with the first table below and then try to guess the answers in the second table. Hint: 10 bytes = 1 line. Roughly.
Method | Bytes |
public int getTotalWork() { return 1; } |
2 |
public int getProgressValue() { if (tuplesProcessed) { return 1; } return 0; } |
11 |
private void addNewOwnedTarget(Element ownedParent, Map |
21 |
private void clearGeneralState() { dependencyStrength.clear(); subLevelAlgorithm.clear(); currentElementScopes.clear(); } |
24 |
private void addElement(ConcreteTuple tuple, Element element) { if (isCancelled) { return; } if (element.getProperties().isInternal()) { processElement(tuple, element); } else { tuples.add(tuple); } } |
43 |
public boolean isLastElement(Element element) { if (elements.isEmpty()) { return false; } Element lastElement = elements.get(elements.size() - 1); return element.equals(lastElement); } |
44 |
public int getSubLevelDependencyCount(Element upperParent) { Model model = registry.get(Model.class); Level level = model.getLevel(); Map |
52 |
private Collection |
56 |
private Collection |
65 |
OK, test time. Which of the following methods are less than 50 bytes long? Answers below.
Method | Bytes |
public boolean isActive() { Options options = registry.get(Options.class); return options.isTrue(OptionTag.ABSOLUTE_ANALYSIS); } |
A. |
private void storeAllAccessibleCousins(Collection |
B. |
private Element storeElementValue(Element element) { Collection |
C. |
public void calculateHighlightedElements() { highlightedElements.clear(); highlightedElements .addAll(analysisLib.getAllSelectedAndImmediateRelations()); } |
D. |
public Element map(Element element) { if (!element.getProperties().isInternal()) { return element; } if (model.getLevel() == Level.FUNCTION) { return storeElementValue(element); } inferValueFromOwnedElements(element); return element; } |
E. |
private void storeAllPublicElements(Collection |
F. |
public Collection |
G. |
public void clearCache() { Ensemble ensemble = registry.get(Ensemble.class); ensemble.map(analyses, new ClearCacheFunction()); } |
H. |
public Collection |
I. |
private Collection |
J. |
public Element map(Element element) { if (!element.getProperties().isInternal()) { return element; } int value = getAmplificationValue(element); element.setAnalysisValue(this, value); summaryValue += value; return element; } |
K |
A = yes (25 bytes). B = hell no (130 bytes). C = yes (35 bytes). D = yes (29). E = yes (44 bytes). F = no (79 bytes). G = no (54 bytes). H = yes (34 bytes). I = yes (33 bytes). J = yes (46 bytes). K = yes (46 bytes).
Again, the point here is not to force Java programmers to be compilers, but just to get familiar with some baseline examples so that they can map these to their own style of programming and thereafter forget about it.
Broadly, limiting a method to four or five lines of code will usually bring it in under the 50-byte mark, which many projects have as their goal. The hard bytecode limit merely ensures that teams can avoid those petty code-review arguments over subjective size standards and that the results are automatically verifiable.