Pointing to Subwords for Generating Function Names in Source Code (2020)

Abstract: We tackle the task of automatically generating a function name from source code. Existing generators face difficulties in generating low-frequency or out-of-vocabulary subwords. In this paper, we propose two strategies for copying low-frequency or out-of-vocabulary subwords in inputs. Our best performing model showed an improvement over the conventional method in terms of our modified F1 and accuracy on the Java-small and Java-large datasets.

## introduction programmers often share source code on sharing services such as github. 1 since they can freely define function names in the source code, the names are not necessarily reminiscent of the actual behavior of the functions. for example, the function in figure 1 returns the index of elem whose elem.key is the same as target key. however, the function name would be inappropriate as it implies that the function returns the value of the object. such a function name adversely affects readability and sometimes causes bugs, especially in collaborative environments. a proper function name such as indexoftarget in this case, instead of gettargetvalue, can help programmers understand the code efficiently and avoid possible bugs @xcite . automatically generating such function names has been studied as a generation task in natural language processing @xcite . recently, various neural network-based approaches have been proposed to solve this problem by generating a function name from given source code @xcite @xcite . in these approaches, a function name is treated as a sequence of subwords (get, target and value in figure 1 ). since these approaches heavily rely on a subword-based predefined dictionary to generate a function name, it is difficult to generate a function name containing low-frequency or unknown subwords. to solve this problem, we propose a method for outputting low-frequency or unknown words using a copy mechanism corresponding to a tree structure, and a method for replacing a specific word with a special token. we extend code2seq @xcite by using these methods. code2seq converts source code into an tree-structured representation, called abstract syntax tree (ast), before encoding. we observed that our best-performing model was the one that uses a combination of a hierarchical copy mechanism and a strategy to replace the most frequent word in an input snippet of source code with a delexicalized placeholder. in particular, the score of the best-performing model was increased in terms of our modified f1 and accuracy, calculated on the java-small and java-large foot_1 datasets by @xcite . ## code2seq we first describe code2seq @xcite , an existing model that we extend in this paper. code2seq first converts an input snippet of source code into an ast, a tree-structured data representation given by a parser in a compiler. after that, an encoder-decoder model is used to generate a function name from the ast. we describe asts and the architecture of the base model below. ## proposed method we extend code2seq by adding a mechanism that generates low-frequency or out-of-vocabulary subwords. (figure 2 ) we propose two methods: the first one replaces the most frequent subword with a delexicalized placeholder; and the second one is a hierarchical copy mechanism. ## conclusion this paper dealt with the function name generation task. we proposed two methods for including lowfrequency or out-of-vocabulary subwords: replacing the most frequent subword in an input snippet of source code and with a hierarchical copy mechanism. our models outperformed the existing methods in terms of our modified f1 and accuracy. our proposed copy mechanism is applicable to tree-structured inputs such as discourse structures, cooking recipes, and social network services. moreover, replacing the most frequent subword seems to be useful in tasks where the vocabulary is relatively small. there remain two major issues to address. the first is the need for better evaluation metrics. we believe that this task requires a metric that can accept synonyms such as meteor @xcite . however, some words that are considered synonymous in wordnet foot_7 are used differently in the context of source code. for example, increment is an operation that increases the value of a variable by 1 in source code. it cannot be replaced with a word such as increase, even if they are synonymous with each other. therefore, we need an evaluation metric that takes into account the subtle difference between synonyms. the second is to consider context in source code. our approach generates function names only from the information inside the function. however, the behavior of other functions and the information on the objects handled by the function are important factors in generating the function name, because the function is called somewhere in the code. therefore, automatic generation of function names can be made more practical by considering the context in the source code.