Sourcery is the pair programmer who will help you improve your code. It reviews and improves your code automatically so you can spend more time focused on writing new code and less time cleaning things up. SourcerySourcery is a VS Code extension to help you move faster and ship code with confidence. Sourcery gives you code reviews anytime you want, so you can get feedback before you need to open a PR. Plus Sourcery's coding assistant helps you speed up repetitive tasks while you work. Here are some of the features Sourcery offers to help improve your code:
To start using Sourcery on your code, check out our Getting Started guide. Check out our documentation for more information on how to use Sourcery. Sourcery is free to use for open source projects. To use Sourcery on non open sourced projects you'll need a Sourcery Pro or Sourcery Team subscription. To get a 14 day free trial to Sourcery Pro, sign up for an account on the Sourcery site. FeaturesCode reviews on demandGet feedback on your code anytime you want. Sourcery can review your code directly in your IDE, looking at the branch you're currently on or the uncommitted changes you have. GitHub & GitLab Pull Request ReviewSourcery can help you speed up code reviews and clean up every new commit by automatically reviewing each of your GitHub pull requests. To get started, add Sourcery to your GitHub repo or GitLab project. Sourcery will then start reviewing every new pull request automatically! Real-time refactoring suggestionsWhile you work, Sourcery will review all of the Python, JavaScript, and TypeScript files you have open and look for opportunities to clean up and improve your code. Once Sourcery finds a potential improvement it will underline that section of your code. Hover your mouse over the underlined section of code to see the changes Sourcery suggests and to see a diff of the proposed change. To make the change, just bring up the quick fix menu and choose to accept the change. Sourcery will then apply the change to your code. We're constantly adding new improvements and rules that Sourcery can make. The current list of rules Sourcery checks for is available here. The commands to bring up the quick fix menu depend on your OS & region, but generally they are:
Sourcery reviews all of the files you have open. You can get an overview of all the suggestions Sourcery has in the Problem window. Set up your own rules - or use public rulesetsSourcery is widely extendable so that you can have it check for whatever types of best practices you care about within your code. Sourcery reads rules from a .sourcery.yaml configuration file that you can set up in your project directory - then it will look to apply those rules across the full project. To get started we’ve set up a quick tutorial and you can dive deeper in our full documentation You can also opt into publicly available sets of rules to help improve your code. Check out the Google Python Style Guide and see how you can add it to Sourcery. Continuous code quality feedbackSourcery gives each of your functions a quality score ranging from 0% (bad) - 100% (good) and also gives you sub-scores on Method Length, Complexity, and Working Memory so that you can figure out how to structure your code as cleanly as possible. Method Length is a metric is a measure of how long each method is on average. It is based on the number of nodes in the method's Abstract Syntax Tree. Complexity is a measure of how difficult your code is to read and understand. It is based on these principles:
Working Memory is a measure of the number of variables that need to be kept in your working memory as you read through the code. Sourcery will warn you if your overall quality score for a function falls below 25%. Multi-File AnalysisYou can use Sourcery to review a single file, a folder, or your whole project at once. Right-click on any item in the Explorer window and select "Sourcery → Scan for Refactorings". Sourcery will show you a notification when the scan is done & will show all of the suggestions it found in the Problems window. Duplicate Code DetectionSourcery can help you find duplicate code and near-duplicate code across your whole project. Right-click on any item in the Explorer window and select "Sourcery → Detect Clones". Sourcery will show you a notification when it's done scanning for clones & will show you all of the instances of duplication it found in the Problems window. By default, Sourcery will flag items where at least 3 lines are duplicates or near-duplicates that occur at least twice in the scanned files. Configuring SourceryNot everyone's coding style is the same, so we know you won't always want to accept every refactoring Sourcery suggests. Sourcery reads configuration settings from two sources:
You can use the project-specific config file to set project-specific settings, and the user-specific config file to set settings that apply to all projects. The project-specific configuration takes precedence over the user configuration. The config file uses YAML syntax. If you are new to YAML check out "Learn YAML in 5 minutes". Here is the default set of values set for the file. If a config file isn't set or if a setting isn't specified then Sourcery will use these values:
You can easily configure Sourcery to: Choosing which rules to be shownYou can choose to have Sourcery never show you a specific type of rule by adding it to a list under the Individual ids are displayed in the plugin suggestions and GitHub Bot comments for easy lookup or you can find a full list in the refactorings section of the docs. For example:
Would mean that the Assign If Expression & De Morgan's Identity refactorings are never shown. To make this even easier, we’ve set up the Sourcery Hub. Click on the Sourcery button in the status bar to bring up the Hub, click on settings, and you can toggle on and off individual types of suggestions Skip suggestionsYou can also choose to have Sourcery not review specific files, paths, or individual functions. To ignore a path or a file, add the paths or files as items in a list underneath the For example:
If you want to just ignore a specific function you can add a
If you want to ignore specific refactorings in a function you can add in
Example CodeWe've put together a handful of examples if you want to play around with Sourcery and see a bit more what it can do:
Here is an example where Sourcery would chain together multiple refactorings:
Copy these examples into a Python file to see how Sourcery would handle them. About usWe're a small team out of London trying to make it easier for everyone to write brilliant code. Follow us on Twitter or visit our blog to keep up with updates. LicensingThis repository includes source code as well as packaged binaries. The MIT license only applies to the source code, not the binaries. |