Sitemap

Abstracting Methods

4 min readJul 22, 2018

--

I’ve never been entirely sold that most of the linter concerns matter. Whether someone employs single quotes or double quotes for strings hasn’t seemed to affect my reading ability. Neither has the use of tabs or spaces. Nor whether indents are two or four spaces. In fact I admit I never really saw what was wrong with 3. It’s got the nice width clarity that fades at 2 while notably saving room over 4.

Still, linters help solve a lot of wasted philosophical arguments on pros/cons so I like that. Or use Golang where the style is built into the language with good auto-formatting.

What tends to be unfortunate is that the focus on these microdetails detracts from larger scale issues. I liken it to the explanation I once got for why Poland takes jaywalking so seriously.

Every jaywalking ticket is a solved crime. This compensates statistically for more serious, harder to solve, crimes.

That explanation is probably at least a little tongue-in-cheek. Still I parallel the concern of quantity over quality.

Style maximizes the ease of working with code. To me this means leveraging techniques that will making the code faster to ramp up on and and less prone to mistaken development.

Linters are particularly bad at identifying poor method abstracting. They warn for too many lines, for heavy nesting, and for number of branches. These indicate overly complex methods, but are too coarse to catch typical problems. Besides they often cause worse solutions, like simply chopping a method in half to pass the linter.

Get Aaron Rosenberg’s stories in your inbox

Join Medium for free to get updates from this writer.

The poor abstraction problem stems from the single responsibility principle. The less a method does, the easier it is to understand. Consider the following answer to the typical interview question about returning the k most common words in a string.

public static List<String> mostCommonWords(String words, int k) {

// first get counts
Map<String, Integer> wordCounts = new HashMap<>();
for (String word : words.split(" ")) {
int currentCount = wordCounts.getOrDefault(word, 0);
wordCounts.put(word, currentCount + 1);
}

// sort in descending order
List<String> sortedWords = new ArrayList<>(wordCounts.keySet());
sortedWords.sort( (String a , String b) -> wordCounts.get(b).compareTo(wordCounts.get(a)));

// return first k
return sortedWords.subList(0, k);
}

I expect something like the above for a first pass. I honestly don’t care about more efficient solutions in practice either, but my issues with interviews is a concern for another time. The above solution is pretty typical to team code and no linter is going to complain about it. Consider the following refactoring of the solution.

public static List<String> mostCommonWords(String words, int k) {
Map<String, Integer> wordCounts = getWordCountMap(words);
List<String> sortedWords = orderWordsByPopularity(wordCounts);
return getFirstK(k, sortedWords);
}

private static List<String> orderWordsByPopularity(Map<String, Integer> wordCounts) {
List<String> sortedWords = new ArrayList<>(wordCounts.keySet());
sortedWords.sort( (String a , String b) -> wordCounts.get(b).compareTo(wordCounts.get(a)));
return sortedWords;
}

private static Map<String, Integer> getWordCountMap(String words) {
Map<String, Integer> wordCounts = new HashMap<>();
for (String word : words.split(" ")) {
int currentCount = wordCounts.getOrDefault(word, 0);
wordCounts.put(word, currentCount + 1);
}
return wordCounts;
}

private static List<String> getFirstK(int k, List<String> words) {
return words.subList(0, k);
}

What happens here is the first method now reads out exactly like you’d describe to a human. First get the word counts, then sort in a descending order, then return the first k. In practice, I doubt I’d separate `getFirstK`, but I will do it for one liners that are a little less descriptive of the overall operation being done.

A couple concerns typically pop up with the second solution. First, why create methods that are only called once. Second, aren’t more lines bad.

It’s certainly good to create methods to reuse code. It’s not the only reason though. If you gain readability by splitting out a process from one method into another then why wouldn’t you?

Understandably the comments in the first solution solved the same problem. They described the steps the same way as the second one. Problematically though, comments aren’t code.

Code changes without comments getting updated. It happens a lot simply because when you already know what code is doing you don’t read the comments. When you change it, it’s easy to miss that there were even comments explaining what was happening. It’s also easy for reviewers to miss since comments aren’t indicated as changed.

This has led me to be mislead and waste significant time and I’ve certainly done the same to others. Now I won’t even read comments unless I’ve read the code and am sufficiently confused. Then I’m desperate a comment will help me. If the abstraction documentation is instead built into method names they’ll naturally update with code changes.

As for the length problem, the second solution style will create longer code. Length of code is an indicator of too much happening in one place, but it’s not something to be minimized. If adding lines will increase readability and therefore maintainability, why wouldn’t you do it?

--

--