Diagnosing Java code: REPL provides interactive evaluation

Source: Internet
Author: User
Tags expression

Typically, when you run a Java program, you must use the string[] input parameter to pass the parameter in and run the program from the main () method. But if the program is still being debugged, it could be a heavy task. This month, Eric Allen discusses the benefits of an interactive evaluation of expressions and statements in a program, and provides several Java repl that help you with this task (the "Read-eval-print-loop" tool, the read-evaluate-print-loop tool). He also discusses how interactive evaluations can help build the GUI and explore new APIs.

Most programs contain methods that are distributed across a large number of classes. There is no doubt that it is difficult to test all of these methods from the main entry point of the program, if not impossible.

This is why unit tests are useful. Many programmers and software designers, including myself, emphasize that unit testing is useful when writing robust software. But you might want to compromise if you want to be able to access the various elements in your program in a more interactive way.

When you do this, writing, compiling, and running a new unit test for each result quickly becomes a heavy task. I've found that when you can't predict what the program will do when you give a particular input (for example, this might happen in an AI program), this is especially true.

So, what should we do?

Don't worry about the small changes

For an analogy, consider the difference between programming in a language that is normally compiled (for example, Java or C + +) and programming in a language that is interpreted in more cases (Python or Scheme).

In a compiled language, each write/test/debug loop must contain the additional step of compiling this, which may be a tedious experience, especially for minor changes. This may lead us to conclude that interpretative languages are more fluid and therefore easier to modify. (This flexibility comes at a cost: interpreted languages typically have less static checking of code, such as type checking.) )

Just as sometimes we might want to make a change to the program, but we don't have to go through the hassle of recompiling, we might want to check some elements in the program, but don't have to routinely add a new unit test to the suite. When you do, it may be helpful to have a tool that is traditionally called read-evaluate-print-loop (i.e. REPL).

REPL is a text-based tool that takes an expression as input, evaluates it in the context of a particular program, and then displays the results. Next, it waits for another expression as input, and then repeats the actions. Such tools originate from Lisp-like languages, but they can also be used in newer languages, such as Python.

The benefits of REPL in Java programming

Such tools are not only useful in these languages. Java programmers can also benefit from using REPL, not just debugging, but in other ways as well.

Building GUI

When assembling a GUI, there are many components that need to be laid out and connected. When you construct the GUI, you will surely encounter these things:

Components will interact in an invisible way.

It is quite time-consuming to write all of its code before running the GUI.

Once you see the actual visual effects of the GUI, you will inevitably want to change some aspects of the GUI.

A common "solution" to this problem is the use of graphical GUI-building tools, such as those contained in JBuilder, Forte, and other Ides. Personally, I don't like the idea--it's hard to know what Java code the tool will generate for you, and you can't risk losing the compatibility with the GUI build tool when you're making changes to the generated code (in fact, some Ides are forcing you to modify any part of the machine-generated code).

In addition, many of these GUI builders use a dedicated GUI library when generating Java code, thereby limiting GUI compatibility.

I find it much easier to build a GUI using REPL. I can completely interactively define each GUI component and then show it in turn. I can fix anything I don't like right away. Then I can interact with it and paste the components into the program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.