jBC Programming Programming Constructs and Programming Standards TEMENOS EDUCATION CENTRE
NOTICE These training materials are the copyrighted work of Temenos Headquarters SA and other companies in the TEMENOS group of companies (The Copyright Owner). The training materials contain protected logos, graphics and images. Use of the training materials is restricted solely for use by licensed end users, partners and employees. Any un-licensed reproduction by any means, redistribution, editing, transformation, publishing, distribution, or public demonstration of the training materials whether for commercial or personal gain is expressly prohibited by law, and may result in severe civil and criminal penalties. Violators will be prosecuted to the maximum extent possible. Such training materials shall not be represented, extracted into or included in part, or in whole, as part of any other training documentation without the express permission of the Copyright Owner, which must given in writing by an authorised agent of the Copyright Owner to be valid. Where such permission is given a clear and prominent notice must be displayed on any and all documentation accrediting the Copyright Owner with having copyright over the materials. End-user licenses will in no event contain permissions extending the use of these training materials to third parties for commercial training purposes. Without limiting the foregoing, copying or reproduction of the training materials in part or in whole to any other sever or location for further reproduction or redistribution is expressly prohibited, unless such reproduction is expressly licensed by the Copyright Owner. Also ensure that all the materials are marked as follows at least on the front page: Copyright © 2010 Temenos Headquarters SA (and change each year like 2009-2011 as time passes)
Objectives
The objectives of this session are to understand the following
Conditional Execution – IF..ELSE & CASE Looping - LOOP…REPEAT, FOR…NEXT Comment in jBC Paragraph your code – Modular programming
Programming Standards
Slide 2
The way jBC works
What goes inside the block ? Once you are inside the block of a Program or Subroutine, you can write all the normal things that you write in most programming languages….to make the computer do what you want.
Slide 3
Conditional constructs
IF…THEN…ELSE construct
IF <expression> THEN <statements> END
IF <expression> THEN <statements> END ELSE <statements> END
Slide 4
CASE construct If the number of tests to be done is more than 3, IF..THEN…ELSE may appear cluttered. CASE construct is a cleaner code to perform multiple conditional checks. BEGIN CASE CASE
= <statements> <statements>
CASE = <statements> <statements> CASE =
<statements> ….. ….. CASE 1
<statements> END CASE
Slide 5
CASE construct
EXAMPLE Predict the output of the following code snippet BEGIN CASE CASE GAME.RESULT = 1
CRT "You won!" CASE 1 CRT "You came nowhere" END CASE
Slide 6
Looping constructs
Loops as you are aware, facilitate repetitive execution of code.
jBC has two looping constructs,
FOR…NEXT and
LOOP…REPEAT
Slide 7
Looping constructs
FOR…NEXT
FOR var = expression1 TO expression2 [STEP expression3] <statements> <statements> …… NEXT [var]
Slide 8
Looping constructs
FOR…NEXT
Predict the output of the following code snippet INIT = 1 FOR CNT = INIT TO 10 STEP 2 CRT “HELLO” : INIT NEXT CNT
Slide 9
Looping constructs
LOOP…REPEAT
The LOOP construct allows the programmer to specify loops with multiple exit conditions. LOOP <statements 1> WHILE/UNTIL<expression> <statements 2> …………..
REPEAT
Slide 10
BREAK and CONTINUE
BREAK is used for an early termination of the innermost loop.
The CONTINUE statement takes control to the beginning of the loop
Example COUNTER = 10 MAX = 20 LOOP CRT “this will always execute” WHILE COUNTER < MAX POINTS = COUNTER++ IF POINTS EQ 18 THEN BREAK IF POINTS EQ 15 THEN CONTINUE CRT “POINTS WON = “ : POINTS REPEAT
Slide 11
Comments
*Comment PROGRAM *Author & Date of creation *Program description *Amendment date and amendment details ----------SQ = “’”
;* A Single quote
----------
END
Slide 12
PARAGRAPHS
jBC is a structured programming language and follows a modular programming approach
In Modular programming, a large program is broken into manageable units called paragraphs, in order to create code that can be easily re-used.
Modular programming improves maintainability of code
A modular program consists of a main module and one or more auxiliary modules
Slide 13
PARAGRAPHS
PARAGRAPHS EXAMPLE ….. ….. GOSUB PARTY.PLAN ….. ….. PARTY.PLAN: BEGIN CASE CASE CHOICE EQ 1 GOSUB CHOOSE.MUSIC CASE CHOICE EQ 2 GOSUB CHOOSE.FOOD CASE 1 EXIT(1) END CASE
….. ….. CHOOSE.MUSIC: ….. ….. RETURN
CHOOSE.FOOD: … … RETURN
RETURN …
….
Slide 14
Programming Standards
•
Research has revealed that unprofessional code takes much more time reading than writing
•
Code IS “Documentation” and is the main communication channel
•
Programming is a profession. It is essential to acquire the art of writing professional code and become a professional workforce.
Good coding conventions result in precise, readable and unambiguous source code.
Slide 15
Some Programming Standards
The way jBC works
Slide 16
Some Programming Standards
Code is more meaningful if written as CRT “ Enter Name to search” INPUT NAME LOCATE NAME IN FRIENDS.LIST SETTING POSITION THEN CRT “Name found at Position“ : POSITION END ELSE
INS NAME BEFORE FRIENDS.LIST CRT “Name inserted at ” : POSITION END CRT FRIENDS.LIST
Slide 17
Some Programming Standards Can you figure out the purpose of this subroutine from its name SUBROUTINE FUN.TRN ........ RETURN
Try this subroutine
Use pronounceable, non-cryptic names
SUBROUTINE FUNDS.TRANSFER
Variables and Routines should not use the same names. This may lead to a hard to detect bug
........ ........
Subroutine names and variable names should be in upper case and should be as long as necessary(limited to 35 char) to describe their purpose. Do not use keywords as identifiers for variables, programs, etc. e.g. INPUT, CRT. Give your comment on this variable
RETURN
FUNDS.WRITE.CORRECT.CURRENCY.INFO = “ “ Use TRANSFER.CURRENCY = “ “
There is a limit to how long a name can be before it actually starts having detrimental effect on readability
Slide 18
Some Programming Standards
You have 5 seconds to understand this code
Try the following instead,
Do not write multiple statements in a single line.
Slide 19
Some Programming Standards
Figure out the choice of variable names by this programmer INITIALISATION: RATING.DETAIL = RATING RATING = 0
SQ = “ ‘ " DQ = ‘ “ ' PARA.LINES = 0 NEST.LEVEL = 0
NEST.LINES = 0 FIRST.CODE.LINE = 0 RTAG.OPEN = '' RTAG.CLOSE = ''
Slide 20
Some Programming Standards
Since the programmer cared to do this, it enhances a readers understanding, INITIALISATION: * Initialise variables to evaluate code quality RATING.DETAIL = RATING
;* Passed from calling routine
RATING = 0
;* To begin with
SQ = “ ‘ "
;* A single quote
DQ = ‘ “ '
;* A double quote
PARA.LINES = 0
;* Reset each time there's a label
NEST.LEVEL = 0
;* Count of number of nested IFs etc
NEST.LINES = 0
;* Number of lines in the nest
FIRST.CODE.LINE = 0
;* Set when we see the first line of code - comments count after that
RTAG.OPEN = ''
;* Tag to store rating in code
RTAG.CLOSE = ''
;* And its closing tag
Slide 21
Some Programming Standards Do not re-state code with comments
The following are bad comments (they give information that is obvious from the code): LOOP WHILE (TIME < SCHEDULED.TIME) IF(NO.OF.TASKS > 5) THEN
;* Loop until SCHEDULED.TIME ;* If no. of tasks is > 5 calculate overload
OVERLOAD = OVERLOAD + 5 …….
Here’s a better version of the code (comments offer new information) LOOP WHILE (TIME < SCHEDULED.TIME) IF(NO.OF.TASKS > 5) THEN OVERLOAD = OVERLOAD + 5
;* Calculates the work-overload factor of an employee ;* only tasks in the ongoing project are estimated. ; * Ensuing planned projects’ overload is not considered
…….
Slide 22
Some Programming Standards
The following is a subroutine for payroll process. Can you suggest a quality improvement in this code snippet
SUBROUTINE PAYROLL.PROCESS * FIX_ME: 9/2/11
SALARY = 7000
* Why is this here? * The fix should be explained with a comment, or removed. ;* 7000 is assigned to SALARY
* Redundant comment
……. …….
END
Slide 23
Some Programming Standards
Comments should be brief Instead of
* The following code will calculate the shortest path between the source and destination by applying the Djikstra’s Algorithm on the existing values. Write :
* Calculates the shortest path using Djikstra’s algorithm
While amending an existing code, DO NOT comment out existing code DELETE it.
Slide 24
Some Programming Standards
Code that looks like this …….. IF IF IF
IF IF IF IF IF
IF IF should automatically raise a red flag
Slide 25
Some Programming Standards Can you follow the flow of this code. Note the line numbers….the code is spread across multiple pages 0024 0025 0026 0038 0039 0188 0189 0190 0191 0250 0251 0253 0254 0255 0256 0257 0350 0351 0352 0353 0354 0400 0401
LBL1: ................ RETURN GOTO LBL5 ………… LBL2: ……… GOTO LBL1 RETURN LBL3: …………. GOTO LBL2 ……….. RETURN ……….. ……….. LBL25: ………… GOTO LBL3 ……….. RETURN LBL5: ………..
GOTO encourages poor programming style. Never use GOTO statements for branching
All code should be structured in a modular
fashion and broken into small units/paragraphs (less than 100 lines) and each unit should have a single entry and exit point.
Indent your code for better readability. Use + N in the jED editor for automatic indentation.
Slide 26
Some Programming Standards
Use “BREAK” & “CONTINUE” sparingly in loops
•
Loops are more readable if the conditions for termination (or continuation) are either at the beginning or at the end of the loop.
•
Using “break” and “continue” is like using a “goto” inside the loop.
•
There are a few cases though that the use of “break” may help you avoid introducing extra flags, therefore may lead to cleaner code.
•
Use your judgment on a case-by-case basis.
Slide 27
Exercises
Modify the program written in the previous session. Use a switch statement to display a menu to accept, and display the personal
details. Include a default behaviour. Incorporate Modular programming and Programming Standards learnt in this session.
Slide 28
Summary
CASE construct is a cleaner code to perform multiple conditional checks. More than three IF’s should be replaced by CASE
jBC has two looping constructs, FOR…NEXT and LOOP…REPEAT
BREAK is used for an early termination of the innermost loop.
The CONTINUE statement takes control back to the beginning of the loop
Adherence to programming standards, improves maintainability of code
Slide 29
Thank You TEMENOS EDUCATION CENTRE
NOTICE These training materials are the copyrighted work of Temenos Headquarters SA and other companies in the TEMENOS group of companies (The Copyright Owner). The training materials contain protected logos, graphics and images. Use of the training materials is restricted solely for use by licensed end users, partners and employees. Any un-licensed reproduction by any means, redistribution, editing, transformation, publishing, distribution, or public demonstration of the training materials whether for commercial or personal gain is expressly prohibited by law, and may result in severe civil and criminal penalties. Violators will be prosecuted to the maximum extent possible. Such training materials shall not be represented, extracted into or included in part, or in whole, as part of any other training documentation without the express permission of the Copyright Owner, which must given in writing by an authorised agent of the Copyright Owner to be valid. Where such permission is given a clear and prominent notice must be displayed on any and all documentation accrediting the Copyright Owner with having copyright over the materials. End-user licenses will in no event contain permissions extending the use of these training materials to third parties for commercial training purposes. Without limiting the foregoing, copying or reproduction of the training materials in part or in whole to any other sever or location for further reproduction or redistribution is expressly prohibited, unless such reproduction is expressly licensed by the Copyright Owner. Also ensure that all the materials are marked as follows at least on the front page: Copyright © 2010 Temenos Headquarters SA (and change each year like 2009-2011 as time passes)