Hamburger Icon
  • Labs icon Lab
  • Core Tech
Labs

Guided: Static Code Analysis

Ever wish you could have a second set of eyes on your code? What if you could have an entire team of highly experienced developers who’ve seen a wide variety of problems, and better yet, know how to solve them? That is the promise of Static Analysis, which you’ll explore in this lab.

Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 29m
Published
Clock icon Mar 28, 2024

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Perform Static Analysis on the `PatientHealth.py` File

    In this Guided Code Lab, you will be using Flake to analyze your code.

    Flake is a static analysis tool for Python that can examine code for problems and make recommendations.

    Examine the code in the PatientHealth.py file. While this code is solid, it contains some elements that could be improved. Enforcing a coding style for the team would certainly be beneficial. This should find eight issues:

    PatientHealth.py:12:13: F841 local variable 'score' is assigned to but never used

    PatientHealth.py:12:47: W291 trailing whitespace

    PatientHealth.py:15:9: F841 local variable 'long_variable_name' is assigned to but never used

    PatientHealth.py:15:80: E501 line too long (208 > 79 characters)

    PatientHealth.py:18:9: F401 'medical_tests.blood_test' imported but unused

    PatientHealth.py:21:9: F841 local variable 'patient_name' is assigned to but never used

    PatientHealth.py:22:15: F821 undefined name 'patient_condition'

    PatientHealth.py:28:80: E501 line too long (96 > 79 characters) You should receive no response, indicating that the Python interpreter has parsed the PatientHealth.py code correctly. This means that while the interpreter finds syntax errors, you have to rely on a static analysis tools to find problems which don't directly break the parsing of the code.

  2. Challenge

    Configure Flake to Work the Way You Want

    This analysis is great, but the nature of the medical terminology that the code uses makes for long lines, which the team is accustomed to. This means that you don't care about rule E501 - "line too long". In this step, you will exclude that rule from the analysis. This will create a .flake8 file. Flake will use this file as a source of configuration, and the content you have created will instruct it to ignore the E501 rule. You should see the following content:

    [flake8]
    ignore = E501
    

    This will make Flake ignore rule E501.

    In reviewing the list of Flake rules for analysis, you see another rule you wish to ignore. Rule W292 is "No newline at end of file". The team's IDE writes files in this manner, and it doesn't affect the project. Therefore, you don't want it cluttering up your analysis. You should now see only six issues:

    PatientHealth.py:12:13: F841 local variable 'score' is assigned to but never used

    PatientHealth.py:12:47: W291 trailing whitespace

    PatientHealth.py:15:9: F841 local variable 'long_variable_name' is assigned to but never used

    PatientHealth.py:18:9: F401 'medical_tests.blood_test' imported but unused

    PatientHealth.py:21:9: F841 local variable 'patient_name' is assigned to but never used

    PatientHealth.py:22:15: F821 undefined name 'patient_condition'

    The E501 and W292 issues are now suppressed from analysis.

  3. Challenge

    Exclude Files from Analysis with Flake

    After gaining a basic grasp of static analysis with Patient Health, you want to broaden your understanding further. You also want to avoid the need to specify the filename for every file to be analyzed. This will analyze the entire current folder. You should see two new issues at the top relating to dbaccess.py. dbaccess.py is a third-party script you use to manage database access, and you don't want to analyze it - that's the job of the provider. This analyzes the surgery folder, where there are three Python files. Two of them are used by a build process for preparing surgical reports and are in generated by another tool. Therefore, there is no point in analyzing them. This will exclude all files that match the surgery_build*.py pattern. If you re-execute the flake8 surgery command, you will see that only the Outcome.py file is being analyzed and the build files are now excluded.

  4. Challenge

    Fix Problems that Flake Has Found

    In this last step, you're will begin the process of remediating the problems that Flake has found. The first issue reads:

    ./PatientHealth.py:12:13: F841 local variable 'score' is assigned to but never used

    You'll fix this issue. Now if you execute the flake8 PatientHealth.py command again, you will see that the first two issues now no longer appear. Your changes have fixed the problems, and now static analysis will help maintain those fixes or detect them if they reappear. If you re-execute the flake8 PatientHealth.py command, you will see only three problems remain. By repeating this pattern of analysis and repair, you can easily resolve the remaining few issues.

    Congratulations on completing this Guided Code Lab on Static Code Analysis!

Chris B. Behrens is a writer, speaker and software developer, specializing in DevOps. He has been a developer and architect for more than twenty years focusing on small to medium size companies and the development changes they face.

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.