Source Code Editing

Source code editing is the work of creating and changing the text that defines a program. In engineering computing, you use it when writing calculation scripts, updating variable names or organizing repeated expressions. Effective editing combines useful editor features with careful control of what changes. Your goal is to produce the intended source text and save it to the correct file.

Source text and saved files

Source code is program text written in a programming language. A source code editor works with plain text and provides features such as line numbers, indentation controls, syntax highlighting and search. A word processor can introduce formatting or substituted quotation marks that do not belong in source files.


An editor usually holds an open document in an editing buffer: the working text currently available for editing. The buffer can differ from the saved file. Saving writes its current contents to a file; automatic saving depends on the editor's settings.


Check the destination folder and filename when saving. Two files with the same name in different folders are still different files. A filename extension such as .py identifies the intended file type, but changing an extension does not translate the contents into another programming language.


Use the encoding and line-ending convention required by your project. UTF-8 is a common text encoding. LF and CRLF are different representations of line endings; changing between them can make many lines appear modified even when their visible text is unchanged.

Whitespace can carry structure

Indentation makes nested statements easier to read. In Python, leading indentation also determines which statements belong to a block. Moving a statement farther right can therefore change the program's meaning.


A tab character and several space characters can look identical on screen. Tab display width is an editor setting; changing it does not necessarily replace any characters in the file. Distinguish displaying tabs differently from converting indentation to spaces.


Choose a consistent indentation convention, such as four spaces per level when that matches the project's rules. Review nested blocks after converting indentation. Avoid replacing every tab indiscriminately because tabs can also occur inside text data.



Syntax highlighting and bracket matching help you inspect source structure. They provide useful cues, but highlighting alone does not establish that an edit is correct.

Choose the scope of an edit

Search and replacement can operate on a selection, one file or several files. Establish that scope before applying a change. A search result is a text match, which may appear in a variable name, a comment or a quoted string.


Editing feature

Useful application

Check before applying

Single replacement

Change one occurrence

Confirm the selected occurrence

Replace all

Apply a repeated text change

Preview matches and confirm scope

Multiple cursors

Repeat an edit at selected locations

Inspect every cursor position

Automatic formatting

Apply consistent layout

Review indentation and other changes


Whole-word matching can reduce matches inside longer names, but it does not necessarily distinguish identifiers from quoted text. Check how your editor defines word boundaries and inspect the results.



Estimate effort and review the result

Bulk editing reduces repetitive work, but reviewing the affected text still takes time. Under the simplifying assumption that each occurrence takes the same time:


Tmanual=N(tedit+treview)T_{\text{manual}}=N(t_{\text{edit}}+t_{\text{review}})


Tbulk=tsetup+NtreviewT_{\text{bulk}}=t_{\text{setup}}+Nt_{\text{review}}


Here, NN is the number of intended occurrences, and each time is measured in seconds. The estimated saving is Tmanual−TbulkT_{\text{manual}}-T_{\text{bulk}}. These estimates describe editing effort; they do not measure correctness.


Inspect the changed text or a comparison with the previous version. Confirm that intended occurrences changed, protected text remained intact and block structure was preserved. Then save to the intended destination.


What to remember

  • Distinguish the editing buffer from the saved file.
  • Treat indentation and other source characters deliberately.
  • Preview replacement scope and inspect every affected context.
  • Review the result and confirm where you save it.

Example: Update a force variable without changing report filenames

You maintain a Python file named stress_calc.py. The editing request is to rename the variable load_n to force_n throughout that file while preserving existing report filenames. The file contains 24 exact identifier occurrences of load_n and three additional text matches inside quoted report filenames. There are no other matches.


The following excerpt contains two of those identifier occurrences and one filename match:


Python
load_n = 1800.0
area_mm2 = 45.0
stress_mpa = load_n / area_mm2
report_name = "load_n_report.txt"
print(stress_mpa)


Assume a plain-text search finds all 27 matches. Automatic saving is disabled.


a) Determine how many matches should change and explain how to select them.


The request applies to identifiers, while the ________ quoted filename matches are protected text. Subtract those protected matches from the complete search results:


Nchange=27−3=24 occurrences.N_{\text{change}}=27-3=24\ \text{occurrences}.


Set the search scope to stress_calc.py, preview all matches and select the 24 identifier occurrences. Exclude the three filename matches. In the excerpt, change the assignment name and the name used in the stress expression; preserve the quoted filename. An unrestricted replacement of all 27 matches would exceed the requested scope.


b) Write the revised excerpt and describe the checks needed before making it available in the intended saved file.


Apply the new name to both identifier occurrences in the excerpt:


Python
force_n = 1800.0
area_mm2 = 45.0
stress_mpa = force_n / area_mm2
report_name = "load_n_report.txt"
print(stress_mpa)


The assignment and its use now share the name force_n. The numerical constants, arithmetic expression, filename and line indentation remain unchanged.


Review the complete file's changes to confirm that all 24 intended occurrences were updated and all three protected filename matches remain. Then save the buffer to stress_calc.py in the intended project folder. Because automatic saving is disabled, reviewing the buffer alone does not update the saved file.


c) Estimate the time saved by bulk editing. Assume manual editing takes 6 seconds to change each intended occurrence, followed by 2 seconds to review it. Bulk replacement takes 18 seconds total to select and apply the changes, followed by the same 2 seconds of review per changed occurrence. Ignore other overhead. State both workflow times and the saving in seconds and minutes.


Use the 24 intended changes, rather than all ________ search matches. Both workflows include review, so first calculate their shared review time:


Treview=24(2 s)=48 s.T_{\text{review}}=24(2\ \text{s})=48\ \text{s}.


Manual editing includes an individual edit for each occurrence:


Tmanual=24(6 s)+48 s=192 s=3.2 min.T_{\text{manual}}=24(6\ \text{s})+48\ \text{s}=192\ \text{s}=3.2\ \text{min}.


Bulk editing includes one setup-and-application time:


Tbulk=18 s+48 s=66 s=1.1 min.T_{\text{bulk}}=18\ \text{s}+48\ \text{s}=66\ \text{s}=1.1\ \text{min}.


Subtract the complete workflow times:


ΔT=192 s−66 s=126 s\Delta T=192\ \text{s}-66\ \text{s}=126\ \text{s}


ΔT=126 s60 s/min=2.1 min.\Delta T=\frac{126\ \text{s}}{60\ \text{s/min}}=2.1\ \text{min}.


Bulk editing saves 126 seconds, or 2.1 minutes, under the stated timing assumptions. The saving comes from reducing repeated editing effort; the review time remains included in both workflows.

You modify strain_calc.py in a source code editor. Automatic saving is disabled, and the changes currently exist only in the editing buffer. A collaborator will read strain_calc.py from the project's folder on disk.


Which action makes the modified text available in that intended file?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

You are configuring a text editor to read a long engineering calculation script. Match each feature to its main purpose.

Match each term
A.

Displays a long source line across several screen rows without inserting newline characters

B.

Displays vertical reference marks that help you follow nesting across several lines

C.

Indicates the opening or closing delimiter paired with the one beside the cursor

D.

Applies different colours to categories such as comments, strings and keywords

Soft wrap

Indentation guides

Bracket matching

Syntax highlighting

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A Python file contains the following complete snippet:


Python
pressure = 125.0
label = "pressure"
print(pressure, label)


You want to rename the variable pressure to pressure_kpa while preserving the displayed label exactly. A whole-word text search finds three matches, including the match inside quotation marks.


Which editing action meets the requirement?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice