14 | Chapter 1: Node.js Basics 14 | Chapter 1: Node.js Basics

14 | Chapter 1: Node.js Basics 14 | Chapter 1: Node.js Basics

In Eclipse PDT, the code can be edited in a separate window, in the comparison window itself, or in both. If it is edited in a separate window and saved, any comparison windows that show the same file will be reloaded and recompared. Making some tweaks in a separate window and saving the file so that the effect on the comparison can be deter‐ mined is a common technique.

Naturally, it really helps to keep the code in the same format in both files to use the same names for everything (such as functions and variables), and even to refactor the code in one or both files such that the visual diff feature will find as many matches as possible.

To keep the PHP and Node.js code synchronized and simultaneously improve and add features to both codebases, you will often rely on the visual diff to make sure that the PHP and Node.js code are correct. In time, a developer will develop a finely tuned sense of what is not enough white and what is too much white.

When there isn’t enough white, the visual diff feature usually is getting off track and trying to match PHP code in the .php file to Node.js code in the .njs file, which is not meant to be matched. There will be a lot of gray in the comparison, indicating differ‐ ences, and not each matches. Experimentation will often correct this issue.

When there is too much white, it often means that there is some PHP code in the .njs file that has not been converted completely to Node.js code. Even though the .njs file can be parsed and run, too much white indicates that more conversion is needed. Often, eyeballing the Node.js code will indicate specific conversions that have not been done yet. One simple conversion that may be missed is that dollar signs ($) need to be added to PHP variables; dollar signs are not used on Node.js variables. Adding dollar signs to the PHP code will reduce the amount of white, bringing the comparison closer to having the right amount of white.

Visual inspection, especially using the visual diff feature, is much faster than interactively testing the PHP and the Node.js code. Visual inspection can act as a “smoke test” to determine if the conversion is approximately correct. Automated test cases, which are beyond the scope of this book, may also be used to quickly test the effectiveness of the conversion so far.

Throughout the book, there will be opportunities to convert a particular code element of a large amount of PHP code into the corresponding code element for Node.js code. For example, a PHP associative array is created by calling the PHP array() function, whereas in Node.js, it is often created by using the object literal notation, which uses curly brackets ( { and } ). When the contents of an entire .php file are copied wholesale into a .njs file at the start of the conversion of the code, the .njs file will then obviously

Eclipse PDT | 15 Eclipse PDT | 15

Figure 1-4. Eclipse PDT Find/Replace dialog box The operation of this dialog box is straightforward.

Rather than including a screenshot of the Find/Replace dialog box every time that it is needed, this book uses a text shorthand. For the Find/Replace dialog box options in the figure, the text will have the following blurb inserted:

Operation: "Find/Replace" in Eclipse PDT Find: array( Replace: { Options: Case sensitive Action: Replace All

The Find/Replace dialog box can be used in two different ways. One way is to do what I call a “blind” global find-and-replace action, like the example

find-and-replace blurb in Figure 1-4 . I call it “blind” because it finds and replaces every occurrence in the file all at once, with no warning and no manual inspection. If all the Find/Replace dialog box values are tested and determined to be foolproof, a “blind” global find-and-replace action is fast and accurate. Unfortunately, if the result causes an error, there are only two options: undo the action or perform a new action that corrects the previous action.

The second option for find-and-replace action repair work is worth pointing out. Some‐ times, it is better to do a simple-to-understand find-and-replace action that will correctly