good+program-writing+techniques

= =
 * 3.2.5 b
 * ===explain the need for good program-writing techniques to facilitate the ongoing maintenance of programs===

__**Keywords to learn**__


 * **Modular programming ** || This is a software design technique that breaks components down into small segments called modules, each of which accomplishes one function and contains everything necessary to accomplish this. ||
 * **Dry-run ** || This is when you want to locate an error manually by searching for the cause and location, executing and recording each line as you turn. ||
 * **Black box testing ** || This is only concerned with the inputs and outputs of a program, and not how the program works. This is to test whether the inputs produce the outputs expected and to help fix any errors. ||

__**Advantages of good programming techniques**__


 * Having good programming techniques enables the programmer to focus on what the program is intended to do and makes it easier to check if the program does what it is meant to do. By doing this it provides an easy reference to errors and the programmer can be aware when writing the code. As a result of this, fewer errors are made and when they are made they are easier to fix.


 * When programmers are working together in a group, using good, consistent programming techniques allows all the members of the group to understand each other's code much better. As a result of this the whole process of working together goes a lot faster and smoother as there will be less to none arguments, and also it ensures that the modules they have written are compatible with modules written by the rest of the group members. If a programmer were to leave the group then other members can maintain the code instead of possibly starting again.


 * Good programming techniques will reduce the time needed for the programmers to familiarise themselves again with the code, which will increase effieciency. This can be put it into the situation when the programmer does a maintenance check because the code has been changed, bugs have been found or when new features are to be added.

**__Examination questions__**


 * Exam board and paper || Question  || Question number  || Page number  ||
 * <span style="display: block; font-family: 'arial','sans-serif'; font-size: 13px; text-align: center;">OCR June 2010 || <span style="font-family: 'Arial','sans-serif'; font-size: 13px;">Describe what is meant by black box testing. (2) || <span style="font-family: 'Arial','sans-serif'; font-size: 13px;">1 c) i) || <span style="font-family: 'Arial','sans-serif'; font-size: 13px;">2 ||
 * <span style="display: block; font-family: 'arial','sans-serif'; font-size: 13px; text-align: center;">OCR June 2010 || <span style="font-family: 'Arial','sans-serif'; font-size: 13px;">Describe two advantages of modular design. (4) || <span style="font-family: 'Arial','sans-serif'; font-size: 13px;">2 a) ii) || <span style="font-family: 'Arial','sans-serif'; font-size: 13px;">6 ||
 * <span style="display: block; font-family: 'arial','sans-serif'; font-size: 13px; text-align: center;">OCR January 2010 || <span style="font-family: 'Arial','sans-serif'; font-size: 13px;">Explain and justify how code can be rewritten to make it easier to maintain. (8) || <span style="font-family: 'Arial','sans-serif'; font-size: 13px;">2 e) || <span style="font-family: 'Arial','sans-serif'; font-size: 13px;">10 ||
 * <span style="display: block; font-family: 'arial','sans-serif'; font-size: 13px; text-align: center;">OCR June 2011 || <span style="font-family: 'Arial','sans-serif'; font-size: 13px;">Write a recursive function in a high level language to find the nth term. Use good programming techniques to ensure your code can be understood by another programmer. (7) || <span style="font-family: 'Arial','sans-serif'; font-size: 13px;">4 c) ii) || <span style="font-family: 'Arial','sans-serif'; font-size: 13px;">19 ||
 * <span style="display: block; font-family: 'arial','sans-serif'; font-size: 13px; text-align: center;">OCR June 2009 || <span style="font-family: 'Arial','sans-serif'; font-size: 13px;">Supervisors can call the lift using an override facility. This uses a subroutine call SupervisorCall. This subroutine has one parameter, Floor (the number of the floor where the supervisor is.) The subroutine sets the UP and DOWN values for the designated floor to TRUE, and all other values in the array to FALSE. Write the code for the subroutine SupervisorCall in a high level language. You should state the name of the language you have used and use good program writing techniques to ensure that your code can be understood by another programmer. (6) || <span style="font-family: 'Arial','sans-serif'; font-size: 13px;">3 d) || <span style="font-family: 'Arial','sans-serif'; font-size: 13px;">12 ||

=3.2.5 d=
 * ===select and use meaningful identifier names and use standard conventions to show the data types and enhance readability;===


 * __ Keywords to learn __**
 * Variable || A variable is a name used to identify a particular memory location that is used to store data. It is a value that can be changed. ||
 * Constant || A constant is a name used to identify a fixed value. This is a value that cannot be changed once chosen as it is a fixed value. ||
 * Identifier || The name of a variable or constant can also be referred to as an identifier. Most languages insist that an identifier must begin with a letter. ||
 * Local variable || This is declared and used inside a module or subroutine and is only available in that module. It is created when the subroutine is called and destroyed when the subroutine exists. ||
 * Global variable || This is declared at the beginning of the code and is available throughout the code, including all of its subroutines. ||


 * __ Selecting identifiers __**
 * When a programmer is declaring variables and constants, they should use meaningful identifiers that communicate their purpose. Single letter identifiers should be avoided, exceptions being i, j and k, which are used as variables.


 * Spaces are not allowed so it is important that the programmer makes the division between words clear with an identifier name. This can be done by using underscores between words (time_of_arrival) and using uppercase letters at the start of each word (TimeOfArrival).


 * A prefix can be used at the start of the identifier names to remind the programmer of the data type of the variable. For example, strMessage can be used to represent the data type string for message, and also intAge to represent the data type integer for age. However only one prefix should be used per program as there are different conventions.


 * __ Examination questions __**
 * Exam board and paper || Question || Marks || Question number || Page ||
 * OCR June 2009 || Describe what is meant by a variable. || 2 || 4 a) i) || 14 ||
 * OCR Jan 2010 || Identify one global variable and one local variable in the extract shown. || 2 || 4 a) ii) || 16 ||
 * OCR Jun 2011 || Explain and justify the best practice when declaring variables and constants in code, to ensure error-free and easily understandable code. || 8 || 3 d) || 13 ||


 * __ Examination questions __**
 * Exam board and paper || Question || Marks || Number || Page ||
 * OCR June 2010 || Explain what is meant by an array. || 3 || 3 a) i) || 10 ||
 * OCR Jan 2010 || Describe what is meant by a parameter. || 3 || 2 b) i) || 8 ||
 * OCR June 2011 || Explain one difference between a procedure and a function. || 2 || 2 c) i) || 9 ||

=3.2.5 e=
 * ===use declared constants to improve maintainability;===


 * __ Keywords __**
 * True constant || A true constant is a declared identifier whose value cannot change. ||
 * Typed constant || Typed constants, unlike true constants, can hold values of array, record, procedural, and pointer types. Typed constants cannot occur in constant expressions. ||
 * Array constant || To declare an array constant, enclose the values of the array's elements, separated by commas. These values must be represented by constant expressions. ||
 * Record constant || To declare a record constant, specify the value of each field - as fieldName: value, with the field assignments separated by semicolons - in parentheses at the end of the declaration. The values must be represented by constant expressions. The fields must be listed in the order in which they appear in the record type declaration, and the tag field, if there is one, must have a value specified; if the record has a variant part, only the variant selected by the tag field can be assigned values. ||
 * Procedural constant || To declare a procedural constant, specify the name of a function or procedure that is compatible with the declared type of the constant. ||
 * Pointer constant || When you declare a pointer constant, you must initialize it to a value that can be resolved at least as a relative address at compile time. There are three ways to do this: with the @ operator, with nil, and (if the constant is of type PChar or PWideChar) with a string literal. ||


 * __ How declared constants improve maintainability of code __**
 * Initialising a variable ensures that its value has not be been retained from a previous use of the routine and that the value has not been accidently set in another part of the program – this helps avoid errors.


 * A declaration is a statement in a program that gives the compiler or the interpreter information about a variable or constant that is to be used within a program. A declaration ensures that sufficient memory is reserved in which to store the values and also states the variables’ data type.


 * The advantage of variable declaration is that it cuts out possible errors due to the misspelling of a variable name – if a variable is used that has not been declared, the programming translator will identify it as an error.

=3.2.5 f=
 * ===initialise variables appropriately, before using them;===


 * __ Keywords __**
 * Variable || A variable is a name used to identify a particular memory location that is used to store data. It is a value that can be changed. ||
 * Constant || A constant is a name used to identify a fixed value. This is a value that cannot be changed once chosen as it is a fixed value. ||
 * Algorithm || This will list every instruction the computer needs to execute to solve the problem, and show the order in which these instructions should be executed. ||
 * Boolean || This is data that can only have two values. This is usually true or false questions. ||


 * __ Initialising variables __**
 * Some programming languages will initialise variables when they are declared, which means that the variables are given a starting value. If the initial value is assumed in the code, then it makes the logic of the code more difficult to understand.


 * As a rule, the first time any variable is used, it should be on the left-hand side of an assignment statement.

State the values of the variables a, b, c, d at the end of this procedure cell. || 4 || 2 c) || 9 ||
 * __ Examination Questions __**
 * Exam board and paper || Question || Mark || Number || Page ||
 * OCR June 2009 || Explain what this algorithm does. || 2 || 1 c) ii) || 3 ||
 * OCR Jan 2010 || The procedure is called as follows: solveit (430, 250. 50)
 * OCR June 2011 || To take part, players must register their 11 digit mobile phone number (with no spaces or characters). Explain why the phone number should not be stored as an integer. || 2 || 3 b) || 13 ||

=3.2.5 g=
 * ===create appropriately modularised programs (following a top-down/modular design as produced in 3.2.1: Designing solutions to problems) making effective use of subroutines to improve maintainability;===


 * __ Keywords __**
 * Top-down modular design || The task that the program needs to perform is split into smaller subtasks. ||
 * Stepwise refinement || These subtasks are then repeatedly split into even smaller subtasks from top-down modular design. ||
 * Sub-routine || A set of instructions that perform a specific task of a larger program. ||


 * __<span style="color: red; font-family: Arial,sans-serif;">Advantages to modular programming __**
 * <span style="font-family: Arial,sans-serif;">The program is much easier to write and test because each module is small and can be written and tested independently.


 * <span style="font-family: Arial,sans-serif;">The use of top-down modular design clearly shows how the different parts of the program relate to each other. By doing this it reduces errors caused by different parts of a large program interfering with each other.


 * <span style="font-family: Arial,sans-serif;">When a team of programmers are working with each other, and working on separate modules, it is easier to develop the program. This is because the modules can be allocated to each programmer based on their skills in that area.


 * <span style="font-family: Arial,sans-serif;">The modules can be used again in other parts of the program.

<span style="font-family: Arial,sans-serif;">When a subroutine is defined it is given an identifier name. This is really useful when programming because it is easier to remember and identify the code.
 * __<span style="color: red; font-family: Arial,sans-serif;">Using subroutines to improve maintainability of code __**


 * __<span style="color: red; font-family: Arial,sans-serif;">Examination questions __**
 * Exam board and paper || Question || Marks || Question number || Page ||
 * OCR June 2009 || State the names of the parameters in the subroutine. || 2 || 3 c) ii) || 11 ||
 * OCR Jan 2010 || Describe what is meant by a parameter. || 3 || 2 b) i) || 8 ||
 * OCR Jun 2011 || Define the term procedure and explain how it is used. || 4 || 2 b) i) || 8 ||

=3.2.5 h=
 * ===annotate the code with comments so that the logic of the solution can be followed;===


 * __<span style="color: red; font-family: Arial,sans-serif;">Keywords __**
 * <span style="color: red; font-family: Arial,sans-serif;">Commenting || <span style="font-family: Arial,sans-serif;">Comments which are inserted into a programme that can only be read by the programmer and not the computer. ||


 * __<span style="color: red; font-family: Arial,sans-serif;">Comments in code __**
 * <span style="font-family: Arial,sans-serif;">There should be a program header comment at the beginning of the code. This comment will include the name of the program, what the program does, the names of the programmers, the date of the last update and notes on what is currently being worked on or any known and resolved bugs.


 * <span style="font-family: Arial,sans-serif;">Each subroutine should have a comment at the beginning explaining what the subroutine does and how it will be called, including any parameters that are necessary to call the subroutine.


 * <span style="font-family: Arial,sans-serif;">Where variables and constants are declared, a short comment should be given explaining their purpose.


 * <span style="font-family: Arial,sans-serif;">The code itself should have inline comments explaining the algorithm. These comments should come before the code they refer to. There should be enough comments to explain the entire algorithm.


 * __<span style="color: red; font-family: Arial,sans-serif;">Examination questions __**
 * Exam board and paper || Question || Marks || Question number || Page ||
 * OCR June 2009 || Write an algorithm which takes the number of tickets wanted as an input, and outputs the best seats available. || 8 || 4 c) || 16 ||
 * OCR Jan 2010 || By concatenating strings to the value d, format the output statement in line 06 so that it is user friendly. || 2 || 2 d) || 9 ||
 * OCR Jun 2011 || Describe what is meant by a recursive algorithm. || 2 || 4 c) i) || 19 ||

=3.2.5 i=
 * ===use indentation and formatting to show clearly the control structures within the code.===


 * __<span style="color: red; font-family: 'Arial','sans-serif';">Keywords __**
 * <span style="color: red; font-family: 'Arial','sans-serif';">Indentation || <span style="font-family: 'Arial','sans-serif';">Indentation is used to format program source code to improve readability. ||
 * <span style="color: red; font-family: 'Arial','sans-serif';">Formatting || <span style="font-family: 'Arial','sans-serif';">This is when code is grouped into logical blocks that perform parts of the main task by entering blank lines between them. ||

<span style="font-family: 'Arial','sans-serif';">Every time a code structure is used which has a beginning and an ending statement on separate lines, the code within the structure should be given a new level of indentation. There should be a strip of white space down the page from the begin statement to the end statement. This makes it clear where each structure starts and finishes, and which structures are nested in others. Some languages, such as Python, actually use the indentation as part of the rules of the language. However, in most languages, it is down to the programmer to get this right.
 * __<span style="color: red; font-family: 'Arial','sans-serif';">Using indentation __**


 * <span style="color: red; font-family: 'Arial','sans-serif';">Exam board and paper || <span style="color: red; font-family: 'Arial','sans-serif';">Question || <span style="color: red; font-family: 'Arial','sans-serif';">Marks || <span style="color: red; font-family: 'Arial','sans-serif';">Question number || <span style="color: red; font-family: 'Arial','sans-serif';">Page ||
 * <span style="font-family: 'Arial','sans-serif';">OCR Jan 2009 || <span style="font-family: 'Arial','sans-serif';">State what is meant by recursion. || <span style="font-family: 'Arial','sans-serif';">1 || <span style="font-family: 'Arial','sans-serif';">5 c) i) || <span style="font-family: 'Arial','sans-serif';">13 ||
 * <span style="font-family: 'Arial','sans-serif';">OCR June 2010 || <span style="font-family: 'Arial','sans-serif';">Using the same format, write the algorithm for updating the points if there is a winner. || <span style="font-family: 'Arial','sans-serif';">4 || <span style="font-family: 'Arial','sans-serif';">1 b) || <span style="font-family: 'Arial','sans-serif';">3 ||
 * <span style="font-family: 'Arial','sans-serif';">OCR Jan 2011 || <span style="font-family: 'Arial','sans-serif';">Explain how nesting has been used in the algorithm above. You should refer to line numbers in your answer. || <span style="font-family: 'Arial','sans-serif';">3 || <span style="font-family: 'Arial','sans-serif';">3 b) || <span style="font-family: 'Arial','sans-serif';">12 ||