Program logic and simplification
Consider the following two statements and decide which is more logical to say?
If her name is not Cindy then it is Mandy OR
Her name is Mandy, not Cindy.
The second statement is clearer and more precise. Now lets apply this to some Powerbuilder script examples:
if not taxable_income > 100 000 then
tax_rate=.2
else
tax_rate=.3
end if
If not is often confusing and clumsy especially statements that contain if not .... = true
The following expression is clearer and simpler.
if taxable_income > 100 000 then
tax_rate=.3
else
tax_rate=.2
end if
Always consider if a statement can be written more logically. Usually this will simplify it as well. Avoid uneccessary complicated and confusing statements.
Lets consider another example using SQL (Structured Query Language)
select customer.cusname from customers
where customer.cuscode=:arg_code;
In the above example the table prefix customers in the field name is not required. You can simplify the statement as follows:
select cusname from customers
where cuscode=:arg_code;
In a case like the above, the table customers is clear from the context. Where a selection is made from multiple tables, it will be beneficial and sometimes neccessary to include the table reference to avoid ambiguity.
Ok, one more example to finish this topic.
Do not code it like this:
if not This.ToolbarVisible = TRUE then
this.ToolbasVisible=true
else
this.ToolbarVisible=false
end if
This is better:
if This.ToolbarVisible then
this.ToolbarVisible=false
end if
Not only is the amount of code reduced, it is simpler and clearer.
Thus, the first concept is to simplify your code where possible. Achieve this by making it more logical.
Effective use of constructs
There are three major constructs:
- Sequence
- Iteration
- Selection
SEQUENCE Sequence is mereley code that is executed top-down, i.e. sequentially. You break out of the sequence when you use a construct, call a function, or when an event is triggerred. Control is usually transferred back to the next sequential statement.
Powerbuilder 10 supports the well known constructs:
SELECTION
Choose Case textexpression Use this construct when you are testing multiple conditions. Avoid complicated nested if--else statements
Example of choose case
choose case typeof(parent.control[idx])
case checkbox!
control_type="Check Box"
case commandbutton!
control_type="Command Button"
case datawindow!
control_type="Data Window"
case SingleLineEdit!
control_type="Single Line Edit"
case else
continue
end choose
IF condition THEN action1 {ELSE action2} Use the if... then (else) statement for simple selections such as:
if typeof(parent.control[idx]=datawindow! then
//some code
else
//some code
end if
ITERATION
Do ...Loop Use a do loop when a block of code needs to be executed conditionally, i.e. until a condition is true, or while a condition is true.
For ... Next Use the for...next construct when a block of code simply needs to be executed a given number of times.
Eliminate redundancy
This reduces the amount and code and improves efficiency.
The use of the Powerbuilder pronouns allows general references to an object, thereby eliminating complications when an objects name changes. As in the example below:
close(w_cusmas)//close window w_cusmas
However, this is better
close(parent) //closes the parent window
Then again, do not use pronouns when they are not needed and therefor add no value, as in the case below
this.hide() //adds no value
when
hide()
is all tht is needed.
In Powerbuilder code, if a condition is true, then you do not need to use the expression =true, you can simply use the condition statement only.
if sle_1.visible
is the same as
if sle_1.visible=true
So it is preferable to use the first method.
In database design we have the concept of normalization of data. Therefor you will not store a customers name in every transaction record. The name should be in the master table. All that is needed is the customer code to relate back to the master table. To include the customer name on a transaction table will be redundant and silly.
Naming Conventions
Use the recommended Powerbuilder naming conventions eg a window is prefixed w_ and and a command button cb_ Use meaningful names, wihout making them rediculously long. For example fname, mname and lname is better than first_name, middle_name and last_name. By keeping names short yet meaningful, the amount of coding is reduced while clarity is retained. Then last but not least, use naming conventions consistently. Do not call a column cuscode in one table and cus_code in another, when in fact they refer to the same thing.
Effective use of functions
When certain piece of code is often used, it is a good candidate for a function. For example a customer lookup function. Instead of having two or three sets of code that do a customer look-up, create a function that does this and call the function when required.
Resource :
http://www.rds.co.za/pbefficiency.htm