CS 101: Introduction to Computer Science
James Madison University, Fall 2019 Semester

Lab09: Static analysis, debugging


Background

Software engineers use wide variety of computing tools during each phase of the software life cycle. These tools are often bundled together in an integrated development environment (IDE). Eclipse is an open source IDE for building applications based on Java, C/C++, Python, and many other languages. In contrast to "lightweight" IDEs like IDLE and jGRASP, Eclipse is a "heavyweight" IDE that includes pretty much everything but the kitchen sink.

The goal of today's lab is NOT to make you an expert in using these tools, although you should plan on that during the next few years. For now, try to make connections to the ideas presented in Chapter 7 of the textbook. Be sure also to click on all the links in this lab handout and get a big picture of what role these tools play in software engineering.

Objectives

Download:   Lab09-Worksheet.txt

Part 1: Install PyDev

Getting up and running with a heavyweight IDE requires a lot of one-time setup. Please ask for help if you get lost! Once everything is configured, all your files are present each time you open the IDE. (Hopefully you will also learn why many software developers prefer to use a simple text editor and the command line.)

  1. If you don't already have Eclipse on your computer, download the installer here. The file will be named eclipse-inst-linux64.tar.gz or something similar, based on your operating system. Unzip this file and run the eclipse-inst program. When prompted, select "Eclipse IDE for Java Developers" (the first option). It may take several minutes to install. (NOTE: It should already be installed in the Virtual Machine.)

  2. Run Eclipse (it should now be installed under ~/eclipse/java-oxygen/eclipse/eclipse). It will prompt you to select a workspace location. Your "workspace" is a directory that contains all your projects and settings. The default location is ~/eclipse-workspace (recall that ~ means your home directory). Answer question 1 in your worksheet.

  3. On the menu bar, click "Help → Install New Software..." Copy and paste the link http://www.pydev.org/updates and press Enter. In the list below, check the box for "PyDev for Eclipse". Then click "Next", click "Next" again, accept the terms, and click "Finish". After the installation completes, Eclipse will restart.

Part 2: Project Setup

When Eclipse first runs, it shows you then Welcome screen. Feel free to explore its contents. Click the "Workbench" button in the upper-right corner or close that window before proceeding.

  1. Click the "New" button on the left. Expand the "PyDev" folder and select "PyDev Project" (the last item). Click the "Next" button and give your project the name Lab09. Click the link that says "Please configure an interpreter..." and then click the "Quick Auto-Config" button. Click "Finish" and then "Open Perspective".

  2. Figure out how to create and run a "Hello, world!" program. You will need to create a PyDev Module (e.g., named hello), write the code to print "Hello, world!", and then click the "Run As..." button on the toolbar (it's green with a white triangle). Answer questions 2 and 3 in your worksheet.

  3. Download Lab09.zip and extract its contents to your Lab09 directory. Make sure you don't have an extra Lab09 directory inside your Lab09 directory. You should have the same structure as shown below. (You may need to right-click Lab09 and click "Refresh".) Answer question 4 in your worksheet.

Part 3: Static Analysis

One feature that makes IDEs both useful and overwhelming is they analyze your code without even running it (hence the term "static" analysis). Warnings and errors will help you identify potential problems before they crash your program.

  1. In the dance package, open the finch.py module. Notice the "Outline" view on the right. Expand the Finch class and click on the method names to navigate the source code. Then open the square.py module. Answer questions 5 and 6 in your worksheet.

  2. Run the square.py module and make sure you understand what the output means. Then figure out how to stop the running program, since it will continue to loop forever.

  3. In the "other" directory, open the example1.py and example2.py modules. See how many errors and warnings you can fix. (Hover your mouse over the icons in the left margin for hints.) Answer question 7 in your worksheet.

Python has an official style guide named PEP 8, as well as a command line tool named pep8 that checks for style issues in your code. Eclipse has the ability to run this tool within the editor.

  1. Open the divisibles.py module. Don't worry too much about the details. Just skim the code to get an idea about what it does.

  2. From the menu bar, click "Window → Preferences". Type the word pep8 to search for it, and then click the pep8.py tab (under Code Analysis). Select the "Warning" radio button and then click "Apply and Close". You will now receive warnings for any PEP 8 violations.

  3. Close and reopen the divisibles.py module so that pep8 will run. Fix all the style errors. Answer question 8 in your worksheet.

Part 4: The Debugger

Most IDEs come with a feature that allows you to step through your code while it is running and see what is happening (i.e., "dynamic" analysis). Debuggers not only help you find bugs, they are also an excellent way to proofread your code before showing it to someone else.

  1. In the meld package, open the task.py module. In the left margin, double click on line number 19. A green dot should appear (see below). This dot represents a "breakpoint" in your code.

  2. Click on the "Debug As..." icon on the toolbar (it looks like a bug). Eclipse will prompt you to switch to the Debug perspective; click yes. You program is now running, but stopped at the breakpoint.

  3. Click the Step Over button (or press F6) to run the program one line at a time. Keep an eye on the "Variables" window on the upper right. Answer question 9 in your worksheet.

  4. Using a debugger is a good way to explore code that someone else wrote, and you have no idea what it does. Let's take a look at a final, simpler example. Open the square.py module (in dance) and set a breakpoint on line 4. Click the debug button and step through the code. Answer question 10 in your worksheet.

Submission Instructions