Giter Club home page Giter Club logo

complexity's Introduction

complexity

Calculate an approximation of code complexity per file in a language-agnostic way.

Motivation

If you're new to a codebase, it's helpful to understand at a glance what files may be particularly complex. With that guidance, developers can more quickly read through the code to understand hotspots.

At thoughtbot, we work in codebases of all shapes and languages, including Ruby, Elixir, Python, Scala, TypeScript/JavaScript, Go, Elm, Swift, and Java. This CLI tool aims to highlight complexity across any of these codebases by assigning simple heuristics to increases in indentation.

This concept has been discussed in this paper; complexity does not intend to mimic approaches in this paper directly, although the motivations discussed in the paper – especially avoiding calculating cyclomatic complexity (McCabe) given requirements of AST parsing and analysis due to time and language requirements – are of considerable overlap.

Installation

MacOS with Homebrew

brew tap thoughtbot/formulae
brew install complexity

Configuration

complexity has configuration options to ignore certain file extensions or substrings with paths.

To install this default configuration, run:

complexity install-configuration

This creates/overwrites a yaml configuration at $HOME/.config/complexity/complexity.yml.

By default, extensions including lock, toml, json, and md are ignored.

complexity will automatically honor .gitignore settings.

Usage

Basic

Let's grab the 20 most complex files:

complexity | sort -n --reverse | head -n 20

Within the Discourse codebase, for example, here's what the output might look like:

  487.96 ./spec/components/guardian_spec.rb
  465.19 ./spec/requests/users_controller_spec.rb
  363.09 ./spec/requests/topics_controller_spec.rb
  311.10 ./spec/models/topic_spec.rb
  273.57 ./lib/javascripts/messageformat.js
  266.61 ./spec/models/user_spec.rb
  248.28 ./app/assets/javascripts/discourse/app/controllers/topic.js
  238.81 ./app/assets/javascripts/discourse/tests/fixtures/discovery-fixtures.js
  219.06 ./script/import_scripts/socialcast/test/test_data.rb
  207.01 ./app/controllers/users_controller.rb
  205.86 ./app/assets/javascripts/discourse/tests/unit/lib/pretty-text-test.js
  202.18 ./app/assets/javascripts/discourse/tests/fixtures/topic.js
  200.17 ./lib/search.rb
  193.05 ./app/assets/javascripts/discourse/app/controllers/composer.js
  191.63 ./app/models/user.rb
  187.97 ./app/models/topic.rb
  186.10 ./spec/components/pretty_text_spec.rb
  179.25 ./spec/requests/session_controller_spec.rb
  174.89 ./spec/requests/groups_controller_spec.rb
  173.44 ./app/assets/javascripts/discourse/tests/integration/widgets/post-test.js

Advanced

complexity supports alternative formatting options, like JSON and CSV. Additionally, you can limit results by substring with the --only flag, or modify paths to ignore with --ignore.

You can view the full suite of options by running complexity help.

License

Copyright 2020 Josh Clayton and thoughtbot, inc. See the LICENSE.

complexity's People

Contributors

brian-penguin avatar joshuaclayton avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

complexity's Issues

Debian .deb (apt) install

Hi there - I am interested in using this with Elixir and Python code for my work. Since I primarily use Ubuntu, I'd like to try doing a a debian .deb build script.

I see that currently there is only the Mac Homebrew install - what exactly does the homebrew install do? I can likely replicate what it's doing for Debian/Ubuntu linux platforms, if someone can point me to the Homebrew install scripts, or just let me know what the Homebrew install does.

Thanks!

Installing from Homebrew on M1 mac

image

==> Downloading https://github.com/thoughtbot/complexity/archive/0.3.0.tar.gz
Already downloaded: /Users/briantenggren/Library/Caches/Homebrew/downloads/ccb5fd1da73aa8733102237a645c4bdebb8ac258d9fbd417a20f694f0a2a3505--complexity-0.3.0.tar.gz
==> Installing complexity from thoughtbot/formulae
�[34m==>�[0m �[1mcargo install�[0m
Last 15 lines from /Users/briantenggren/Library/Logs/Homebrew/complexity/01.cargo:
     |
1254 |     let div = n / bits;
     |                 ^ no implementation for `usize / u32`
     |
     = help: the trait `Div<u32>` is not implemented for `usize`

   Compiling dirs-next v2.0.0
Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `lexical-core` due to 27 previous errors
warning: build failed, waiting for other jobs to finish...
error: failed to compile `complexity v0.3.0 (/private/tmp/complexity-20220302-14250-1nwsd5q/complexity-0.3.0)`, intermediate artifacts can be found at `/private/tmp/complexity-20220302-14250-1nwsd5q/complexity-0.3.0/target`

Caused by:
  build failed

If reporting this issue please do so at (not Homebrew/brew or Homebrew/core):
  https://github.com/thoughtbot/homebrew-formulae/issues

These open issues may also help:
Release complexity 0.4.1 https://github.com/thoughtbot/homebrew-formulae/pull/71

When running

brew tap thoughtbot/formulae
brew install complexity

segmentation fault complexity

Mac Monterrye with M1 proc

!! segmentation fault complexity

➜  ~ complexity install-configuration
[1]    43098 segmentation fault  complexity install-configuration
➜  ~ sudo complexity install-configuration
Password:
[1]    43120 segmentation fault  sudo complexity install-configuration

Explanation of values

Hej, I've been experimenting with the tool.

I was wondering about the meaning of the computed complexity values. Obviously I'm too lazy to read the referenced paper, I totally understand if you direct me to that :D. Still a small TLDR would be nice:
How can I categorize the values (e.g. what's the threshold marking 'critical' complexity)?
Are the values independent of the file size? Are larger files assigned a larger complexity automatically?

Segmentation fault after a fresh installation

Installation using the command below was successful.

brew tap thoughtbot/formulae
brew install complexity

Tried executing

complexity install-configuration

which resulted in zsh: segmentation fault complexity install-configuration.

More info:

  • I'm running this on an M1 machine.
  • The installer seemed to install Rust 1.54.1 during installation.

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.