Click here to close now.




















Welcome!

Microsoft Cloud Authors: Eric Aarrestad, Greg O'Connor, Liz McMillan, Aleksei Gavrilenko, Elizabeth White

Related Topics: PowerBuilder, Microsoft Cloud

PowerBuilder: Article

Elegant Programming | Part 4

Private and public affairs

Law of Demeter (or Principle of Least Knowledge)
A given object should assume as little as possible about the structure or properties of anything else (including its subcomponents).

In simpler words: if you want to get data from an object, referenced by a variable, then use a public function declared in the referenced object itself, but not variables and functions of its nested object(s).

The law is dictated by real life. Imagine you are paying in a store, and the cashier asks you to give your credit card. You take your wallet from your pocket, take the card from the wallet and hand it to the cashier. Nothing strange or unusual. Let's describe this situation in a programming language:

lnv_credit_card = lnv_buyer.uf_get_credit_card()

But imagine a slightly different situation: instead of asking for your card, the cashier silently takes your wallet from your pocket and takes the card from the wallet:

lnv_credit_card = lnv_buyer.uf_get_jacket().uf_get_upper_left_pocket() .uf_get_wallet().uf_get_mastercard()

In both cases there is the same result - the cashier will have your credit card. But there is a huge difference if we are talking about possible dangers. In the first case, the buyer is responsible for his credit card - he knows in which pocket the wallet is and which credit card should be used (if there are more than one). If next time the buyer decides to put the wallet in other pocket, or use different credit card - it's his/her private affair; in programming terms: if the credit card's storing method in the "buyer" class has changed, it's enough to change only GetCreditCard() function's code, and all the calls to that function all over the application will continue to work. The calling objects don't have to know anything about jackets, pockets and other details - they have enough of their own problems and should not be responsible for the private affairs of others (do you want a cashier, an unknown person, to be responsible for your credit cards?).

This issue doesn't exist if data is properly encapsulated, but PowerBuilder has problems in this area: while instance variables can be declared as private or protected, data in DataWindows is always public, so it's enough to have a pointer to a window to access its DWs (w_XXX.tab_XXX.tabpage_XXX.dw_XXX.object.field_XXX[1]).

Functions
If a function of a class will be accessed only from inside the class, declare that function as private (if also from descendants then as protected).

If you do so, then developers using the class will immediately see its "outer world" interface; may be, they only want to learn how to use the class as a "black box" - in that case the implementation details (like private functions) are not needed. If a function, which is practically used privately, is mistakenly declared as public, then the class consumers can think they can (or even should) use it to work with the class. And the opposite, if a developer is working on the class (changing or increasing its functionality), then he/she cannot be 100% sure that a public function is not called from somewhere outside, and is forced to apply the global search if that function's logic or signature must be changed.

Instance Variables
Avoid public instance variables.

Create them only "private" (or "protected" if you are planning to have the class be extended and the descendants have a need to access the variable directly). The purpose of instance variables is to keep internal information of the object, so the outer world (other classes) should access this information only using public functions, i.e., in a controllable way.

Change default access level of class members
Don't leave instance variables and functions public as they are created by default - change the access level to reflect their practical use.

Remember, that PowerBuilder's instance variables and functions are public by default. A method's access can be changed to "private" or "protected" using the dropdown in the declaration section, and instance vars must be "privatized" by adding the "private" or protected" keyword before each var, or the same keyword with a semicolon before a group of vars. Pay attention - if no scope identifier is mentioned explicitly then the vars are public (in contrast to C#, where "no scope identifier" means "private"). IMHO, it was a bad solution of PB authors (everything should be private by default and be changed only if the need arises), but that's what we have... So, when you are creating a new instance variable or a function, and your brain is busy thinking about the functionality being developed, don't forget to change the access level to what it should be.

Why is this advice important? We can see many PB applications with all functions public, and nothing bad is happening... I would say, it's not too important in the development stage, but when the maintenance time comes... Let's say, I have to add and even change the functionality of an existing object. If the stuff I am touching is private, then I know that my changes will not cause straightforward damage in other (sometimes unexpected) parts of the application. For example, I can change the number of parameters or even the name of a private function without having a chance to do any harm to other objects. But if the function is declared as public, I need to perform the Global Application Search (which is extremely slow in PowerBuilder, especially in serious apps) to make sure the function is not called outside the object (and if it is called then, may be, to change the technical solution).

Intriguing World of Logic
Positive wording in Boolean names

Give Boolean variables and functions positive names. Avoid both - direct negation and words that carry a hint of negativity.

Think positively :-) That will simplify the reading and understanding of Boolean expressions, especially if they are nested. As an example, you can see the expressions "if not lb_not_printed" and "if lb_printed": they are logically equivalent, but the positive version is easier to understand. As you can see, the difference appears even when the expressions are standalone, but imagine if they were a part of a complex Boolean expression. You can contradict: and what if the calling code wants to ask if the document has not been printed yet? Whether "if not lb_printed" is better "if not lb_not_printed"? Okay, there is always a chance that our Boolean var or function will be used with NOT, but using the positive wording will cause maximum ONE negation in that situation, while using negative wording can result in TWO of them - "negation of negation." Table 1 contains a few examples of "negative" and "positive" words:

NEGATIVE WORDS
(TO AVOID):

POSITIVE WORDS
(TO USE INSTEAD):

disabled

enabled

disallowed(prohibited)

allowed

hidden

displayed

excluded

included

failed

ok (succeeded)

Table 1

But please remember, this advice is only about Boolean names. If your non-Boolean function performs a negative action, its name must reflect exactly that action, for example:

uf_disable_save_button, uf_prohibit_print, uf_exclude_expired_records.

Positive logic in Boolean expressions
Comparing values, try to use "=" instead of "<>".

*** BAD code: ***

if ls_order_status <> n_order_status.CLOSED) then
[actions for orders which are not closed]
else
[actions for closed orders]
end if

*** GOOD code: ***

if ls_order_status = n_order_status.CLOSED) then
[actions for closed orders]
else
[actions for orders which are not closed]
end if

This advice is especially important if the value comparison is nested into a Boolean expression:

*** BAD code: ***

if ls_order_status <> n_order_status.CLOSED or &
ls_state <> n_state.MONTANA or &
ls_day_type <> n_day_type.WEEKEND then
[actions if: order is not closed OR state differs from Montana OR day is not a weekend]
else
[actions for other cases]
end if

*** GOOD code: ***

if ls_order_status = n_order_status.CLOSED and &
ls_state = n_state.MONTANA and &
ls_day_type = n_day_type.WEEKEND then
[actions if: order is closed AND state is Montana AND day is a weekend]
else
[actions for other cases]
end if

Use CHOOSE CASE to compare one value with many
Comparing one value with many other values, use the "choose case" construction instead of multiple "or" operators, so the compared value will be mentioned in the code only once.

In this situation, we are not really choosing between cases (so we can call it "a fake choose case"), but why not make our code more readable if the programming language doesn't allow the IN clause like T-SQL and PL/SQL do?

*** BAD code: ***

if ls_city = "Toronto" or ls_city = "Boston" or ls_city = "Chicago" then
ls_message = "Nice city!"
end if

*** GOOD code: ***

choose case ls_city
case "Toronto", "Boston", "Chicago"
ls_message = "Nice city!"
end choose

Use CHOOSE CASE TRUE for independent Boolean expressions
Use one compact "choose case true" block to "pack" a series of evaluated Boolean expressions even if they are NOT related to each other. So, instead of:

if not IsValid(ads) then
ls_err = "Passed datastore is not valid"
end if

if ls_err = "" and IsNull(al_row) then
ls_err = "Passed row must not be null"
end if

if ls_err = "" and al_row < 1 then
ls_err = "Passed row must be greater than 0"
end if

write

choose case true
case not IsValid(ads)
ls_err = "Passed datastore is not valid"
case IsNull(al_row)
ls_err = "Passed row must not be null"
case al_row < 1
ls_err = "Passed row must be greater than 0"
end choose

Short conditions in IFs
Don't write complicated expressions inside of if statements (just after the if); instead, store the expression's result in a Boolean variable with a quality, descriptive name and use that variable in the if statement.

That will make the logic being implemented extremely easy to understand.

*** BAD code: ***

if (ls_calc_method = n_calc_method.ADDITIVE and lb_additive_calculation_passed) or &
(ls_calc_method = n_calc_method.RATIO and lb_ratio_calculation_passed) then...

*** GOOD code: ***

boolean lb_structural_change_occurred

lb_structural_change_occurred = &
(ls_calc_method = n_calc_method.ADDITIVE and lb_additive_calculation_passed) or &
(ls_calc_method = n_calc_method.RATIO and lb_ratio_calculation_passed)

if lb_structural_change_occurred then...

As you see, the bad code tells us only when the condition is true, but the good code tells us both when and why!!! It is so important that I easily go against the rule to decrease scripts size - I don't try to keep my scripts short, sometimes I prefer to add lines if that makes the code more understandable or looking less like a heap of rubbish. And, of course, don't forget that the expression in my example is pretty small because it's enough to convey the idea; in real life I have seen statements with half-a-screen Boolean expressions inside an "if". If the logic is complicated, don't populate the Boolean variable in one assignment statement (as described earlier) but come to the truth step-by-step using a number of simple Boolean expressions instead of one monstrous construction. Please see the next example of populating a variable step-by-step instead of inserting everything in the if statement:

boolean lb_the_show_must_go_on

lb_the_show_must_go_on = false
choose case true
case [cond1], [cond2]
lb_the_show_must_go_on = true
case [cond3] and [cond4]
lb_the_show_must_go_on = this.uf_xxx()
case [cond3]
lb_the_show_must_go_on = this.uf_yyy()
end choose

if lb_the_show_must_go_on then...

Don't duplicate conditions in Boolean expressions
Don't add conditions to a Boolean expression that don't change the final result, produced by the expression.

Foolish advice, you say? Why add them? But this advice was learned during my code inspection practice. Look at the following nice examples:

  • if (not IsNull(ld_expire_date)) and ld_expire_date < ld_today then

is logically the same as:

if ld_expire_date < ld_today then

because when ld_expire_date contains null then the second part of the expression will produce null, and nulls are interpreted by code branching operators as false.

  • if dw_xxx.RowCount() > 0 and dw_xxx.GetSelectedRow(0) > 0 then

must be re-written as;

if dw_xxx.GetSelectedRow(0) > 0 then

because when dw_xxx contains no rows, dw_xxx.GetSelectedRow(0) will never return a positive number.

  • if (not IsNull(ids_xxx)) and IsValid(ids_xxx) then

is the same as:

if IsValid(ids_xxx) then

because if IsValid returns true (i.e., memory is allocated for the pointed object) then, of course, the variable is pointing something (i.e., is populated), so testing it with IsNull will produce true.

if ll_row_count > 0 then
for ll_row = 1 to ll_row_count
[...]
next
end if

will work exactly in the same way as simply:

for ll_row = 1 to ll_row_count
[...]
next

Boolean functions
If a function returns Boolean then its name should convey the meaning of the returned value to make the business logic in the calling scripts easily understandable. Calling that function as an "if" statement's condition must produce a correct real-English sentence.

Here are some examples of well-named Boolean functions:

uf_row_is_validated, uf_orders_are_ok, uf_file_name_already_exists.

A strange tradition dictates to put the words "is"/"are"/"does"/"do" in the beginning (like uf_is_row_validated, uf_are_orders_ok etc.), but I personally don't like that approach because it produces incorrect English: we say "if row is validated then do something," not "if is row validated...". Of course, it's not an issue of high importance - you can even omit "is"/"are" at all, and the names will still be fine:

uf_row_validated, uf_orders_ok.

Never use verbs in imperatives to name Boolean functions. For example, the following names are absolutely unacceptable for functions returning Boolean:

uf_validate_row
uf_check_orders
uf_check_file_existence

We don't say "if validate row then save," "if check orders then print."

Sometimes it makes sense to return Boolean from the function as an OUTPUT argument instead of the return value. Once I found this line of existing code:

if uf_display_comments_window() then...

Any idea what the meaning of the Boolean value the function returns? I opened the function code and saw that it returned true if the user clicked the OK button in the comments window, opened in the function, and false if he/she clicked CANCEL. As you can see, the function uf_display_comments_window() cannot be renamed to uf_user_cicked_ok_in_comments_window() because its main purpose is to display the comments window (not to report the fact of button clicking), so the best solution in that situation is to use a REF argument - that will force (!!!) developers to create a variable, pass it to the function and use beautifully in the "if" construction:

boolean lb_user_cicked_ok
uf_display_comments_window(ref lb_user_cicked_ok)
if lb_user_cicked_ok then...

Populating Boolean variables
If it's possible, populate Boolean variables with results of Boolean expressions instead of directly assigning true and false.

*** BAD code: ***

if ll_rows_retrieved > 1 then
lb_multi_rows_mode = true
else
lb_multi_rows_mode = false
end if

*** GOOD code: ***

lb_multi_rows_mode = (ll_rows_retrieved > 1)

Don't return null from Boolean functions
Boolean functions must always return true or false (but not null). Think defensively if the function fails to return the asked value.

You cannot be sure if the calling script (which must make a code branching decision) will check the returned result for null. In fact, usually there is no such check, especially when the function is directly put inside an if construction. There is a high probability that a null, returned by a Boolean function, will be treated as false - it's acceptable in most cases, but not always. What do we have to return if the function cannot answer the asked question? In this situation, think in a pessimistic way. If the function checks an eligibility, return a value, saying "the entity is not eligible for what you asked". For example, if the function uf_customer_can_get_discount() failed to connect to the database, it should return false, but the function uf_passenger_must_be_double_checked_by_security should return true in that situation - simply use common sense trying to prevent the bad. But the best way to treat technical failures is to throw an exception.

De Morgan's Laws
Simplify Boolean expressions using De Morgan's Laws.

To ensure that logical statements are expressed simply, use the De Morgan's Laws:

(NOT P) OR (NOT Q) can be expressed shorter as NOT (P AND Q)

(NOT P) AND (NOT Q) can be expressed shorter as NOT (P OR Q)

In that situation, use the NOT operator only once, as shown below.

Example 1 (AND becomes OR):

lb_is_gas = (not lb_is_liquid) and (not lb_is_solid) and (not lb_is_plasma)

should be simplified as:

lb_is_gas = not (lb_is_liquid or lb_is_solid or lb_is_plasma)

Example 2 (OR becomes AND):

(To go to a restaurant, 2 conditions must be satisfied: "we are hungry" AND "restaurants are open now").

lb_we_will_stay_home = (not lb_we_are_hungry) or (not lb_restaurants_are_open_now)

should be simplified as:

lb_we_will_stay_home = not (lb_we_are_hungry and lb_restaurants_are_open_now)

More Stories By Michael Zuskin

Michael Zuskin is a certified software professional with sophisticated programming skills and experience in Enterprise Software Development.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


@ThingsExpo Stories
For IoT to grow as quickly as analyst firms’ project, a lot is going to fall on developers to quickly bring applications to market. But the lack of a standard development platform threatens to slow growth and make application development more time consuming and costly, much like we’ve seen in the mobile space. In his session at @ThingsExpo, Mike Weiner, Product Manager of the Omega DevCloud with KORE Telematics Inc., discussed the evolving requirements for developers as IoT matures and conducted a live demonstration of how quickly application development can happen when the need to comply wit...
The Internet of Everything (IoE) brings together people, process, data and things to make networked connections more relevant and valuable than ever before – transforming information into knowledge and knowledge into wisdom. IoE creates new capabilities, richer experiences, and unprecedented opportunities to improve business and government operations, decision making and mission support capabilities.
Explosive growth in connected devices. Enormous amounts of data for collection and analysis. Critical use of data for split-second decision making and actionable information. All three are factors in making the Internet of Things a reality. Yet, any one factor would have an IT organization pondering its infrastructure strategy. How should your organization enhance its IT framework to enable an Internet of Things implementation? In his session at @ThingsExpo, James Kirkland, Red Hat's Chief Architect for the Internet of Things and Intelligent Systems, described how to revolutionize your archit...
SYS-CON Events announced today that HPM Networks will exhibit at the 17th International Cloud Expo®, which will take place on November 3–5, 2015, at the Santa Clara Convention Center in Santa Clara, CA. For 20 years, HPM Networks has been integrating technology solutions that solve complex business challenges. HPM Networks has designed solutions for both SMB and enterprise customers throughout the San Francisco Bay Area.
MuleSoft has announced the findings of its 2015 Connectivity Benchmark Report on the adoption and business impact of APIs. The findings suggest traditional businesses are quickly evolving into "composable enterprises" built out of hundreds of connected software services, applications and devices. Most are embracing the Internet of Things (IoT) and microservices technologies like Docker. A majority are integrating wearables, like smart watches, and more than half plan to generate revenue with APIs within the next year.
Growth hacking is common for startups to make unheard-of progress in building their business. Career Hacks can help Geek Girls and those who support them (yes, that's you too, Dad!) to excel in this typically male-dominated world. Get ready to learn the facts: Is there a bias against women in the tech / developer communities? Why are women 50% of the workforce, but hold only 24% of the STEM or IT positions? Some beginnings of what to do about it! In her Opening Keynote at 16th Cloud Expo, Sandy Carter, IBM General Manager Cloud Ecosystem and Developers, and a Social Business Evangelist, d...
In his keynote at 16th Cloud Expo, Rodney Rogers, CEO of Virtustream, discussed the evolution of the company from inception to its recent acquisition by EMC – including personal insights, lessons learned (and some WTF moments) along the way. Learn how Virtustream’s unique approach of combining the economics and elasticity of the consumer cloud model with proper performance, application automation and security into a platform became a breakout success with enterprise customers and a natural fit for the EMC Federation.
The Internet of Things is not only adding billions of sensors and billions of terabytes to the Internet. It is also forcing a fundamental change in the way we envision Information Technology. For the first time, more data is being created by devices at the edge of the Internet rather than from centralized systems. What does this mean for today's IT professional? In this Power Panel at @ThingsExpo, moderated by Conference Chair Roger Strukhoff, panelists addressed this very serious issue of profound change in the industry.
Discussions about cloud computing are evolving into discussions about enterprise IT in general. As enterprises increasingly migrate toward their own unique clouds, new issues such as the use of containers and microservices emerge to keep things interesting. In this Power Panel at 16th Cloud Expo, moderated by Conference Chair Roger Strukhoff, panelists addressed the state of cloud computing today, and what enterprise IT professionals need to know about how the latest topics and trends affect their organization.
It is one thing to build single industrial IoT applications, but what will it take to build the Smart Cities and truly society-changing applications of the future? The technology won’t be the problem, it will be the number of parties that need to work together and be aligned in their motivation to succeed. In his session at @ThingsExpo, Jason Mondanaro, Director, Product Management at Metanga, discussed how you can plan to cooperate, partner, and form lasting all-star teams to change the world and it starts with business models and monetization strategies.
Converging digital disruptions is creating a major sea change - Cisco calls this the Internet of Everything (IoE). IoE is the network connection of People, Process, Data and Things, fueled by Cloud, Mobile, Social, Analytics and Security, and it represents a $19Trillion value-at-stake over the next 10 years. In her keynote at @ThingsExpo, Manjula Talreja, VP of Cisco Consulting Services, discussed IoE and the enormous opportunities it provides to public and private firms alike. She will share what businesses must do to thrive in the IoE economy, citing examples from several industry sectors.
There will be 150 billion connected devices by 2020. New digital businesses have already disrupted value chains across every industry. APIs are at the center of the digital business. You need to understand what assets you have that can be exposed digitally, what their digital value chain is, and how to create an effective business model around that value chain to compete in this economy. No enterprise can be complacent and not engage in the digital economy. Learn how to be the disruptor and not the disruptee.
Akana has released Envision, an enhanced API analytics platform that helps enterprises mine critical insights across their digital eco-systems, understand their customers and partners and offer value-added personalized services. “In today’s digital economy, data-driven insights are proving to be a key differentiator for businesses. Understanding the data that is being tunneled through their APIs and how it can be used to optimize their business and operations is of paramount importance,” said Alistair Farquharson, CTO of Akana.
Business as usual for IT is evolving into a "Make or Buy" decision on a service-by-service conversation with input from the LOBs. How does your organization move forward with cloud? In his general session at 16th Cloud Expo, Paul Maravei, Regional Sales Manager, Hybrid Cloud and Managed Services at Cisco, discusses how Cisco and its partners offer a market-leading portfolio and ecosystem of cloud infrastructure and application services that allow you to uniquely and securely combine cloud business applications and services across multiple cloud delivery models.
The enterprise market will drive IoT device adoption over the next five years. In his session at @ThingsExpo, John Greenough, an analyst at BI Intelligence, division of Business Insider, analyzed how companies will adopt IoT products and the associated cost of adopting those products. John Greenough is the lead analyst covering the Internet of Things for BI Intelligence- Business Insider’s paid research service. Numerous IoT companies have cited his analysis of the IoT. Prior to joining BI Intelligence, he worked analyzing bank technology for Corporate Insight and The Clearing House Payment...
"Optimal Design is a technology integration and product development firm that specializes in connecting devices to the cloud," stated Joe Wascow, Co-Founder & CMO of Optimal Design, in this SYS-CON.tv interview at @ThingsExpo, held June 9-11, 2015, at the Javits Center in New York City.
SYS-CON Events announced today that CommVault has been named “Bronze Sponsor” of SYS-CON's 17th International Cloud Expo®, which will take place on November 3–5, 2015, at the Santa Clara Convention Center in Santa Clara, CA. A singular vision – a belief in a better way to address current and future data management needs – guides CommVault in the development of Singular Information Management® solutions for high-performance data protection, universal availability and simplified management of data on complex storage networks. CommVault's exclusive single-platform architecture gives companies unp...
Electric Cloud and Arynga have announced a product integration partnership that will bring Continuous Delivery solutions to the automotive Internet-of-Things (IoT) market. The joint solution will help automotive manufacturers, OEMs and system integrators adopt DevOps automation and Continuous Delivery practices that reduce software build and release cycle times within the complex and specific parameters of embedded and IoT software systems.
"ciqada is a combined platform of hardware modules and server products that lets people take their existing devices or new devices and lets them be accessible over the Internet for their users," noted Geoff Engelstein of ciqada, a division of Mars International, in this SYS-CON.tv interview at @ThingsExpo, held June 9-11, 2015, at the Javits Center in New York City.
Internet of Things is moving from being a hype to a reality. Experts estimate that internet connected cars will grow to 152 million, while over 100 million internet connected wireless light bulbs and lamps will be operational by 2020. These and many other intriguing statistics highlight the importance of Internet powered devices and how market penetration is going to multiply many times over in the next few years.