GithubHelp home page GithubHelp logo

complexity's Introduction

Complexity & Travis-CI Lab

In this lab, you'll be implementing simple algorithms for checking basic properties of code complexity.

We will also integrate Travis CI.

Two design patterns are of importance here:

  • A Builder pattern, which is used to build up state and then finally emit.
  • A Visitor pattern, which is used to abstract the process of visiting a data structure such as abstract syntax tree (AST). The only input you have to provide is what action to perform at each node.

Esprima

Writing programs to do static code analysis on Javascript is easier now than ever, thanks to esprima - the Javascript parser library. We will practice how to use esprima to do something fairly simple โ€“ measure basic code complexity metrics of JavaScript programs.

Video

This video provides more background about esprima: Watch 5:00-14:30,28:00-34:00.

Interactive Abstract Syntax Tree (AST)

Use the demo page to see what a code snippet looks like, in order to help you navigate the data structure. Better to open in a separate browser tab, as you will need to work with it for coding inspiration.

For example, the following snippet:

function functionName( node )
{
	if( node.id )
	{
		return node.id.name;
	}
	return "anon function @" + node.loc.start.line;
}

Will appear as following, and:

image

Can be interacted with here.

Lab activities:

The repository contains a stub that parses a javascript file and visits each function.

  1. Run the program and print all the tokens in an ast.

       npm install
       node analysis.js
    
  2. Do a simple calculation (2 Points Each)

    a) ParameterCount: The number of parameters for functions (FunctionBuilder) b) String Usage: How many string literals are used in file? (FileBuilder)

  3. Using multiple visitors (2 Points Each)

    a) SimpleCyclomaticComplexity: The number of if statements/loops + 1. b) MaxConditions: The max number of condition predicates (expressions seperated by ||, &&) in an if statement.

  4. Check build status on Travis-CI when committing each new function. Submit a screenshot of the final build. (2 Points)

  5. Advanced (2 Extra Points)

    a) MaxNestingDepth: The max depth of scopes (nested ifs, loops, etc) -- this one is hard, only expect a few to get to do finish this one (using parents/etc).

complexity's People

Contributors

aej11a avatar chrisparnin avatar dependabot[bot] avatar greenkeeper[bot] avatar joantubungbanua avatar katetan1234 avatar yy2111 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.