Skip to content

Coding Conventions and Structuring

低頻~32 min7 terms4 past questions

Goals of this unit

  • Tell indentation, which shows blocks with leading spaces, apart from nesting depth, the number of nested levels.
  • Explain how naming conventions for names and module decomposition into parts make a program easier to modify.
  • Explain the caller and callee relationship between a main routine and a subroutine.
Terms at a glance (7)
TermIn one lineAspectMain benefit
字下げAdding spaces at the start of lines to show blocks of processingAppearanceBlock boundaries are visible at a glance
インデンテーションThe English name for the same leading-space practiceAppearanceSame as the entry above
ネストの深さHow many levels of nesting are stackedStructureFewer levels mean fewer misreadings
命名規則An agreement on how to name thingsNamesA name hints at its role
モジュール分割Building a program from parts split by functionStructureChanges stay local and parts can be reused
メインルーチンThe processing that runs first and directs the overall flowStructureShows the overall sequence of steps
サブルーチンA block of processing that is called when neededStructureThe same code need not be written repeatedly

Terms

字下げIndentation

Open in glossary 字下げ

Indentation (字下げ) is the practice of adding spaces at the start of lines so that blocks of processing and nesting levels are visible on sight.

Lines inside a conditional branch or a loop are shifted one level to the right, and lines at the same level start at the same position. In most languages changing the indentation does not change the result, but some languages such as Python use indentation to decide where a block begins and ends. Either way, block boundaries are clear at a glance and misreadings become less likely.

AnalogyIt works like an outline in which sub-items are written slightly to the right under a main item, so you see at once which sub-item belongs to which main item.

How to rememberThe Japanese name literally means "lowering the characters": the act of pushing the first characters of a line to the right became the name itself.

インデンテーションIndentation

Open in glossary インデンテーション

Indentation (インデンテーション) is simply the English name for 字下げ and refers to exactly the same thing.

The leading space itself, or its width, is called an indent, and one level is made of space characters or a tab. If the width of one level is not kept the same across the whole program, the boundaries between blocks become harder to read, not easier.

How to rememberThe English word indent describes the notch where text is set back inward from the edge of the page.

Exam points

  • Mix-upIndentation (インデンテーション) and 字下げ are the English and Japanese names for the same formatting practice, not two separate techniques.

ネストの深さNesting Depth

Open in glossary ネストの深さ

Nesting depth (ネストの深さ) is how many levels of nesting are stacked, where a conditional branch or loop is placed inside another conditional branch or loop.

  • Loop (level 1)
    • Branch (level 2)
      • Branch (level 3)

Another branch inside that branch makes three levels, and the further in a line sits, the harder it is to trace when it runs.

3 / 3

For example, a loop containing a branch that contains another branch is nested three levels deep. The deeper the nesting, the more levels you must trace back to see when a line runs, which invites misreadings and faulty fixes. Deep parts are kept shallow by measures such as moving them out into separate processing.

AnalogyThink of boxes inside boxes inside boxes: the innermost box takes the most opening to reach.

How to rememberNest means a bird's nest in English: just as eggs sit in a nest, processing sits inside processing.

Exam points

  • Mix-upNesting depth (ネストの深さ) is a structural property, the number of nested levels, while indentation (字下げ) is a way to show those levels visually, so widening every level uniformly leaves the depth unchanged.

Indentation vs. Nesting Depth

AspectIndentationNesting Depth
Key differenceWhat it representsVisual levels made by leading spacesNumber of stacked nesting levels
NatureA writing (formatting) practiceA property of how processing is organized (structure)
How to change itChange the width of the spacesReorganize, e.g. by moving processing out

命名規則Naming Conventions

Open in glossary 命名規則

Naming conventions (命名規則) are rules agreed in advance on how to name variables, functions and other elements.

Example ruleExample name
Mark word breaks with capitalsorderCount
Mark word breaks with underscoresorder_count
Write constants in all capitalsTAX_RATE

Typical rules are to use English words that show the role, to mark word breaks with capitals or underscores, and to write constants in all capitals. Naming conventions are usually set together with rules such as the indentation width, as part of the coding standards that keep the way programs are written consistent. When names follow one scheme, people other than the author can guess a name's role from the name alone.

AnalogyIt is like naming every file in a cabinet as "year-month_client_content", so anyone looking can guess what is inside.

Exam points

  • ExamThe main aim of unifying the way code is written, with naming conventions (命名規則) and the like, is to make later reading and fixing easier, that is, to raise maintainability (保守性).

1 past questions on this term 命名規則

モジュール分割Module Decomposition

Open in glossary モジュール分割

Module decomposition (モジュール分割) is a technique of splitting a large program into parts by function, called modules, and assembling it from them.

  • Payroll program
    • Read attendance records
    • Calculate pay amounts
    • Print pay slips

Each module is given a single role, and modules are combined after deciding what information they pass to one another. When a change is needed, its effect tends to stay inside that module, and parts already built can be reused in other programs. The word module is also used for the program units produced when source code is translated and linked.

AnalogyIt is like furniture built from boards, legs and drawers, where a broken drawer can be replaced on its own.

How to rememberModule means a standardized, interchangeable unit, like a spacecraft's habitation module, a part that can be swapped or added.

Exam points

  • ExamMachine code produced by linking translated programs, ready for the computer to run as is, is called a load module (ロードモジュール).

2 past questions on this term モジュール分割

メインルーチンMain Routine

Open in glossary メインルーチン

A main routine (メインルーチン) is the central processing that starts first when a program runs and directs the flow of the whole program.

  1. Main routine starts
  2. Call lower-level processing
  3. Processing ends and returns to the caller
  4. Continue with the next step

Once the processing returns, the main routine moves on to the next step.

4 / 4

It leaves individual detailed tasks to lower-level processing that it calls, then moves on to the next step once that processing returns. Reading the main routine therefore tells you what the whole program does and in what order.

AnalogyIt resembles a site supervisor who sets the order of the work, hands each stage to the right tradesperson, and moves to the next stage after hearing that one is done.

サブルーチンSubroutine

Open in glossary サブルーチン

A subroutine (サブルーチン) is a part that packs a specific block of processing into one unit so that other processing can call it.

メインルーチン

サブルーチン1

サブルーチン2

When called, it carries out its processing and then returns to where the caller left off. The same code need not be written in several places, and one fix applies everywhere it is called. Depending on the language it is also called a function or procedure, and a subroutine can itself call other subroutines.

AnalogyIt is like a recipe that says "for making stock, see the separate page", which you can turn to whenever that step comes up.

How to rememberSub is a prefix meaning below or subordinate, the same sub as in submarine, a ship that travels below the surface.

Exam points

  • Mix-upA main routine (メインルーチン) is where execution starts and sets the overall order, while a subroutine (サブルーチン) runs only when called and then returns to its caller.

Main Routine vs. Subroutine

AspectMain RoutineSubroutine
RoleDirects the overall flowHandles a specific block of processing
Key differenceWhat starts itThe program starting to runA call from other processing
After processingHeads toward the end of the programReturns to the caller and continues

Key points

  • Indentation, under its Japanese and English names, shows the extent of blocks with leading spaces.
  • Deeper nesting invites misreadings, so deep parts are moved out into separate processing to keep levels shallow.
  • Shared rules such as naming conventions make code readable by people other than the author and easier to fix later.
  • Module decomposition builds parts by role, narrowing the impact of changes and allowing parts to be reused.
  • Processing placed in a subroutine can be called any number of times, and a fix is needed in only one place.

Check questions

4 past questions from this unit

Start the check questions

All past questions in this topic