Using Natural Language Processing techniques for automated code refactoring

 

>Alan Barzilay
_
  • Introduction
  • Goals
  • Background
  • Related Work
  • DataSet
  • Proposed Model
  • Work Plan

{summary}

Naturalness Hypothesis[Allamanis et al. 2018]

Software is a form of human communication; software corpora have similar statistical properties to natural language corpora; and these properties can be exploited to build better software engineering tools. 

  • Understand if deep learning models are capable of predicting fine-grained refactorings
  • Create a model for automated function extraction

Goals

{background}

Background

Background

word2vec

Background

word2vec

Background

Background

Background

Background

{related work}

Related Work

Related Work

Related Work

{dataset

& model}

DataSet

Why function extraction?

DataSet

{
   "type":"Extract Method",
   "description":"Extract Method private extractMijCommand(rulePos int, contents String) : List<String> extracted from private extractedRuleMij(contents String) : List<String> in class com.reason.bs.Ninja",
   "leftSideLocations":[
      {
         "filePath":"src/com/reason/bs/Ninja.java",
         "startLine":106,
         "endLine":120,
         "startColumn":5,
         "endColumn":6,
         "codeElementType":"METHOD_DECLARATION",
         "description":"source method declaration before extraction",
         "codeElement":"private extractedRuleMij(contents String) : List<String>"
      },
      {
         "filePath":"src/com/reason/bs/Ninja.java",
         "startLine":109,
         "endLine":109,
         "startColumn":13,
         "endColumn":70,
         "codeElementType":"VARIABLE_DECLARATION_STATEMENT",
         "description":"extracted code from source method declaration",
         "codeElement":"None"
      },
      {
         "filePath":"src/com/reason/bs/Ninja.java",
         "startLine":111,
         "endLine":111,
         "startColumn":17,
         "endColumn":72,
         "codeElementType":"VARIABLE_DECLARATION_STATEMENT",
         "description":"extracted code from source method declaration",
         "codeElement":"None"
      },
      {
         "filePath":"src/com/reason/bs/Ninja.java",
         "startLine":112,
         "endLine":112,
         "startColumn":17,
         "endColumn":91,
         "codeElementType":"VARIABLE_DECLARATION_STATEMENT",
         "description":"extracted code from source method declaration",
         "codeElement":"None"
      },
      {
         "filePath":"src/com/reason/bs/Ninja.java",
         "startLine":113,
         "endLine":113,
         "startColumn":17,
         "endColumn":54,
         "codeElementType":"VARIABLE_DECLARATION_STATEMENT",
         "description":"extracted code from source method declaration",
         "codeElement":"None"
      },
      {
         "filePath":"src/com/reason/bs/Ninja.java",
         "startLine":114,
         "endLine":114,
         "startColumn":17,
         "endColumn":251,
         "codeElementType":"VARIABLE_DECLARATION_STATEMENT",
         "description":"extracted code from source method declaration",
         "codeElement":"None"
      },
      {
         "filePath":"src/com/reason/bs/Ninja.java",
         "startLine":115,
         "endLine":115,
         "startColumn":17,
         "endColumn":42,
         "codeElementType":"EXPRESSION_STATEMENT",
         "description":"extracted code from source method declaration",
         "codeElement":"None"
      },
      {
         "filePath":"src/com/reason/bs/Ninja.java",
         "startLine":116,
         "endLine":116,
         "startColumn":17,
         "endColumn":39,
         "codeElementType":"RETURN_STATEMENT",
         "description":"extracted code from source method declaration",
         "codeElement":"None"
      },
      {
         "filePath":"src/com/reason/bs/Ninja.java",
         "startLine":110,
         "endLine":117,
         "startColumn":33,
         "endColumn":14,
         "codeElementType":"BLOCK",
         "description":"extracted code from source method declaration",
         "codeElement":"None"
      },
      {
         "filePath":"src/com/reason/bs/Ninja.java",
         "startLine":110,
         "endLine":117,
         "startColumn":13,
         "endColumn":14,
         "codeElementType":"IF_STATEMENT",
         "description":"extracted code from source method declaration",
         "codeElement":"None"
      }
   ],
   "rightSideLocations":[
      {
         "filePath":"src/com/reason/bs/Ninja.java",
         "startLine":110,
         "endLine":121,
         "startColumn":5,
         "endColumn":6,
         "codeElementType":"METHOD_DECLARATION",
         "description":"extracted method declaration",
         "codeElement":"private extractMijCommand(rulePos int, contents String) : List<String>"
      },
      {
         "filePath":"src/com/reason/bs/Ninja.java",
         "startLine":111,
         "endLine":111,
         "startColumn":9,
         "endColumn":63,
         "codeElementType":"VARIABLE_DECLARATION_STATEMENT",
         "description":"extracted code to extracted method declaration",
         "codeElement":"None"
      },
      {
         "filePath":"src/com/reason/bs/Ninja.java",
         "startLine":113,
         "endLine":113,
         "startColumn":13,
         "endColumn":68,
         "codeElementType":"VARIABLE_DECLARATION_STATEMENT",
         "description":"extracted code to extracted method declaration",
         "codeElement":"None"
      },
      {
         "filePath":"src/com/reason/bs/Ninja.java",
         "startLine":114,
         "endLine":114,
         "startColumn":13,
         "endColumn":87,
         "codeElementType":"VARIABLE_DECLARATION_STATEMENT",
         "description":"extracted code to extracted method declaration",
         "codeElement":"None"
      },
      {
         "filePath":"src/com/reason/bs/Ninja.java",
         "startLine":115,
         "endLine":115,
         "startColumn":13,
         "endColumn":50,
         "codeElementType":"VARIABLE_DECLARATION_STATEMENT",
         "description":"extracted code to extracted method declaration",
         "codeElement":"None"
      },
      {
         "filePath":"src/com/reason/bs/Ninja.java",
         "startLine":116,
         "endLine":116,
         "startColumn":13,
         "endColumn":247,
         "codeElementType":"VARIABLE_DECLARATION_STATEMENT",
         "description":"extracted code to extracted method declaration",
         "codeElement":"None"
      },
      {
         "filePath":"src/com/reason/bs/Ninja.java",
         "startLine":117,
         "endLine":117,
         "startColumn":13,
         "endColumn":38,
         "codeElementType":"EXPRESSION_STATEMENT",
         "description":"extracted code to extracted method declaration",
         "codeElement":"None"
      },
      {
         "filePath":"src/com/reason/bs/Ninja.java",
         "startLine":118,
         "endLine":118,
         "startColumn":13,
         "endColumn":35,
         "codeElementType":"RETURN_STATEMENT",
         "description":"extracted code to extracted method declaration",
         "codeElement":"None"
      },
      {
         "filePath":"src/com/reason/bs/Ninja.java",
         "startLine":112,
         "endLine":119,
         "startColumn":29,
         "endColumn":10,
         "codeElementType":"BLOCK",
         "description":"extracted code to extracted method declaration",
         "codeElement":"None"
      },
      {
         "filePath":"src/com/reason/bs/Ninja.java",
         "startLine":112,
         "endLine":119,
         "startColumn":9,
         "endColumn":10,
         "codeElementType":"IF_STATEMENT",
         "description":"extracted code to extracted method declaration",
         "codeElement":"None"
      },
      {
         "filePath":"src/com/reason/bs/Ninja.java",
         "startLine":140,
         "endLine":146,
         "startColumn":5,
         "endColumn":6,
         "codeElementType":"METHOD_DECLARATION",
         "description":"source method declaration after extraction",
         "codeElement":"private extractRuleMijDev(contents String) : List<String>"
      },
      {
         "filePath":"src/com/reason/bs/Ninja.java",
         "startLine":143,
         "endLine":143,
         "startColumn":20,
         "endColumn":59,
         "codeElementType":"METHOD_INVOCATION",
         "description":"extracted method invocation",
         "codeElement":"extractMijCommand(ruleMijPos,contents)"
      },
      {
         "filePath":"src/com/reason/bs/Ninja.java",
         "startLine":120,
         "endLine":120,
         "startColumn":9,
         "endColumn":28,
         "codeElementType":"RETURN_STATEMENT",
         "description":"added statement in extracted method declaration",
         "codeElement":"None"
      }
   ]
}

Model

function printOwing(invoice) {
  printBanner();
  outstanding = calculateOutstanding();

  //print details
  console.log(`name: invoice.customer`);
  console.log(`amount: outstanding`);  
}

Model

function printOwing(invoice) {
  printBanner();
  outstanding = calculateOutstanding();

  //print details
  console.log(`name: invoice.customer`);
  console.log(`amount: outstanding`);  
}

Background

Model

function printOwing(invoice) {
  printBanner();
  outstanding = calculateOutstanding();

  //print details
  console.log(`name: invoice.customer`);
  console.log(`amount: outstanding`);  
}
function printOwing(invoice) {
  printBanner();
  outstanding = calculateOutstanding();
  printDetails(outstanding);
}

function printDetails(outstanding) {
  console.log(`name: invoice.customer`);
  console.log(`amount: outstanding`);
}

LSP

Work Plan

Thank you!

Made with Slides.com