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

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

A second way to use the Find/Replace dialog box is to do a step-by-step global find- and-replace action. First, the Find/Replace dialog box is used to find the first instance. The developer then examines the instance and decides whether to modify the code manually (which he can do by clicking on the code and without dismissing the dialog box), or to execute the replace (by pressing the Replace/Find button), or to skip to the next instance without changing the current instance (by pressing the Find button again). Here’s the blurb for a step-by-step global find-and-replace action:

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

The Find/Replace dialog box in the Eclipse PDT can also use regular expressions. Reg‐ ular expressions are a pattern matching technique: instead of finding an exact phrase,

a regular expression describes a pattern to search for. Each time that the pattern is found, the exact phrase that matches the pattern can be applied to the Replace field. For ex‐ ample, if the array\((.*)\) regular expression matches array(id=>'name'), the (.*) in the regular expression will save the id=>'name' text. This saved text is called a capture field, or less commonly, a capture group. In the Eclipse PDT Find/Replace dialog box,

a capture field is captured by surrounding it with undelimited parentheses. To apply a capture field to the Replace field, the capture fields are enumerated according to the order that they were captured in the Find field. A dollar sign ($) indicates that a capture field is being specified, followed by the capture field number. For example, $1 in the Replace field indicates the first capture field, which, in the example earlier in this para‐ graph, would contain the id=>'name' text. Very often, there is only one capture field, so it is very common to only see $1 and rarely to see $2, $3, or beyond.

Here’s a blurb for a blind global find-and-replace action using regular expressions:

Operation: "Find/Replace" in Eclipse PDT Find: array\((.*)\) Replace: {$1} Options: Case sensitive, Regular expressions Action: Replace All

Eclipse PDT | 17

In converting PHP to Node.js, regular expressions are only tangential to the process, so this book will not be giving a primer on how to understand and write your own regular expressions. The regular expressions will be provided as part of blurbs for find-and- replace actions that can be copied to the appropriate fields of the Find/Replace dialog box in the Eclipse PDT, usually verbatim, without requiring you to understand or modify them. If you need additional help with regular expressions or need to understand the rules and how they work, you are encouraged to consult the Eclipse PDT and to use Google or a similar search engine to find websites, blogs, and forums that will answer your questions.

Find-and-replace actions with regular expressions are often more comprehensive and effective than literal find-and-replace actions (i.e., actions where only one specific string is matched). A regular expression allows more variation in what it can match, and with capture fields, it can transport that variation to the Replace field. Often, a literal find- and-replace will be able to match only the beginning of a code element or the end of a code element at one time because the code element can vary in the middle. With a regular expression, the middle can be matched to a pattern that allows the entire code element to be matched in a single find-and-replace action. When the conversion of a code ele‐ ment can be done in a single find-and-replace action, instead of multiple ones, the chances for errors are reduced.

Until now, this chapter has described a range of activities and knowledge about how to set up a development environment for doing a PHP to Node.js conversion. The first thing to do was to download and install Node.js itself and become familiar with the two executables that it comes with. After that, we dug into Node.js stack traces to learn how to read them and how to use them to find what the real, underlying problem is such that the coding issue can be addressed and repaired. Then, we set up the Eclipse PDT as a foundation for a development environment, including a modification for it to under‐ stand .njs files, geared toward PHP to Node.js conversion. And finally, we learned how to use the visual diff feature and find-and-replace actions that will be very important when doing the conversion.

A capable development environment is essential to efficiency and is the way that big efforts get done. Too often, amateur developers will leap into coding with an inefficient or even an annoying development environment. At first, the development will go quickly in any environment because a small amount of code is simple to improve upon. But as the codebase grows larger, the complexity of the code will also grow and the pace of development will slow down. An inefficient or annoying development environment will do nothing to help the developer with the complexity, but a capable development envi‐ ronment will simplify the knowledge needed and help the developer such that the pace can be sustained and, ultimately, the project finished.

With a PHP to Node.js conversion, it is assumed that a large PHP codebase already exists. At the end of the conversion, it is expected that the codebase will more than double in