Click here to close now.

Welcome!

Microsoft Cloud Authors: Liz McMillan, Elizabeth White, Pat Romanski, Jaynesh Shah, Carmen Gonzalez

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
How do APIs and IoT relate? The answer is not as simple as merely adding an API on top of a dumb device, but rather about understanding the architectural patterns for implementing an IoT fabric. There are typically two or three trends: Exposing the device to a management framework Exposing that management framework to a business centric logic Exposing that business layer and data to end users. This last trend is the IoT stack, which involves a new shift in the separation of what stuff happens, where data lives and where the interface lies. For instance, it's a mix of architectural styles ...
We certainly live in interesting technological times. And no more interesting than the current competing IoT standards for connectivity. Various standards bodies, approaches, and ecosystems are vying for mindshare and positioning for a competitive edge. It is clear that when the dust settles, we will have new protocols, evolved protocols, that will change the way we interact with devices and infrastructure. We will also have evolved web protocols, like HTTP/2, that will be changing the very core of our infrastructures. At the same time, we have old approaches made new again like micro-services...
Connected devices and the Internet of Things are getting significant momentum in 2014. In his session at Internet of @ThingsExpo, Jim Hunter, Chief Scientist & Technology Evangelist at Greenwave Systems, examined three key elements that together will drive mass adoption of the IoT before the end of 2015. The first element is the recent advent of robust open source protocols (like AllJoyn and WebRTC) that facilitate M2M communication. The second is broad availability of flexible, cost-effective storage designed to handle the massive surge in back-end data in a world where timely analytics is e...
Collecting data in the field and configuring multitudes of unique devices is a time-consuming, labor-intensive process that can stretch IT resources. Horan & Bird [H&B], Australia’s fifth-largest Solar Panel Installer, wanted to automate sensor data collection and monitoring from its solar panels and integrate the data with its business and marketing systems. After data was collected and structured, two major areas needed to be addressed: improving developer workflows and extending access to a business application to multiple users (multi-tenancy). Docker, a container technology, was used to ...
The true value of the Internet of Things (IoT) lies not just in the data, but through the services that protect the data, perform the analysis and present findings in a usable way. With many IoT elements rooted in traditional IT components, Big Data and IoT isn’t just a play for enterprise. In fact, the IoT presents SMBs with the prospect of launching entirely new activities and exploring innovative areas. CompTIA research identifies several areas where IoT is expected to have the greatest impact.
The 4th International Internet of @ThingsExpo, co-located with the 17th International Cloud Expo - to be held November 3-5, 2015, at the Santa Clara Convention Center in Santa Clara, CA - announces that its Call for Papers is open. The Internet of Things (IoT) is the biggest idea since the creation of the Worldwide Web more than 20 years ago.
The Industrial Internet revolution is now underway, enabled by connected machines and billions of devices that communicate and collaborate. The massive amounts of Big Data requiring real-time analysis is flooding legacy IT systems and giving way to cloud environments that can handle the unpredictable workloads. Yet many barriers remain until we can fully realize the opportunities and benefits from the convergence of machines and devices with Big Data and the cloud, including interoperability, data security and privacy.
The Internet of Things is tied together with a thin strand that is known as time. Coincidentally, at the core of nearly all data analytics is a timestamp. When working with time series data there are a few core principles that everyone should consider, especially across datasets where time is the common boundary. In his session at Internet of @ThingsExpo, Jim Scott, Director of Enterprise Strategy & Architecture at MapR Technologies, discussed single-value, geo-spatial, and log time series data. By focusing on enterprise applications and the data center, he will use OpenTSDB as an example t...
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 will addresses this very serious issue of profound change in the industry.
Scott Jenson leads a project called The Physical Web within the Chrome team at Google. Project members are working to take the scalability and openness of the web and use it to talk to the exponentially exploding range of smart devices. Nearly every company today working on the IoT comes up with the same basic solution: use my server and you'll be fine. But if we really believe there will be trillions of these devices, that just can't scale. We need a system that is open a scalable and by using the URL as a basic building block, we open this up and get the same resilience that the web enjoys.
We are reaching the end of the beginning with WebRTC, and real systems using this technology have begun to appear. One challenge that faces every WebRTC deployment (in some form or another) is identity management. For example, if you have an existing service – possibly built on a variety of different PaaS/SaaS offerings – and you want to add real-time communications you are faced with a challenge relating to user management, authentication, authorization, and validation. Service providers will want to use their existing identities, but these will have credentials already that are (hopefully) i...
All major researchers estimate there will be tens of billions devices - computers, smartphones, tablets, and sensors - connected to the Internet by 2020. This number will continue to grow at a rapid pace for the next several decades. With major technology companies and startups seriously embracing IoT strategies, now is the perfect time to attend @ThingsExpo, June 9-11, 2015, at the Javits Center in New York City. Learn what is going on, contribute to the discussions, and ensure that your enterprise is as "IoT-Ready" as it can be
Container frameworks, such as Docker, provide a variety of benefits, including density of deployment across infrastructure, convenience for application developers to push updates with low operational hand-holding, and a fairly well-defined deployment workflow that can be orchestrated. Container frameworks also enable a DevOps approach to application development by cleanly separating concerns between operations and development teams. But running multi-container, multi-server apps with containers is very hard. You have to learn five new and different technologies and best practices (libswarm, sy...
SYS-CON Events announced today that DragonGlass, an enterprise search platform, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. After eleven years of designing and building custom applications, OpenCrowd has launched DragonGlass, a cloud-based platform that enables the development of search-based applications. These are a new breed of applications that utilize a search index as their backbone for data retrieval. They can easily adapt to new data sets and provide access to both structured and unstruc...
The Internet of Things is a misnomer. That implies that everything is on the Internet, and that simply should not be - especially for things that are blurring the line between medical devices that stimulate like a pacemaker and quantified self-sensors like a pedometer or pulse tracker. The mesh of things that we manage must be segmented into zones of trust for sensing data, transmitting data, receiving command and control administrative changes, and peer-to-peer mesh messaging. In his session at @ThingsExpo, Ryan Bagnulo, Solution Architect / Software Engineer at SOA Software, focused on desi...
An entirely new security model is needed for the Internet of Things, or is it? Can we save some old and tested controls for this new and different environment? In his session at @ThingsExpo, New York's at the Javits Center, Davi Ottenheimer, EMC Senior Director of Trust, reviewed hands-on lessons with IoT devices and reveal a new risk balance you might not expect. Davi Ottenheimer, EMC Senior Director of Trust, has more than nineteen years' experience managing global security operations and assessments, including a decade of leading incident response and digital forensics. He is co-author of t...
SYS-CON Events announced today that MetraTech, now part of Ericsson, has been named “Silver Sponsor” of SYS-CON's 16th International Cloud Expo®, which will take place on June 9–11, 2015, at the Javits Center in New York, NY. Ericsson is the driving force behind the Networked Society- a world leader in communications infrastructure, software and services. Some 40% of the world’s mobile traffic runs through networks Ericsson has supplied, serving more than 2.5 billion subscribers.
While great strides have been made relative to the video aspects of remote collaboration, audio technology has basically stagnated. Typically all audio is mixed to a single monaural stream and emanates from a single point, such as a speakerphone or a speaker associated with a video monitor. This leads to confusion and lack of understanding among participants especially regarding who is actually speaking. Spatial teleconferencing introduces the concept of acoustic spatial separation between conference participants in three dimensional space. This has been shown to significantly improve comprehe...
Buzzword alert: Microservices and IoT at a DevOps conference? What could possibly go wrong? In this Power Panel at DevOps Summit, moderated by Jason Bloomberg, the leading expert on architecting agility for the enterprise and president of Intellyx, panelists will peel away the buzz and discuss the important architectural principles behind implementing IoT solutions for the enterprise. As remote IoT devices and sensors become increasingly intelligent, they become part of our distributed cloud environment, and we must architect and code accordingly. At the very least, you'll have no problem fil...
The Domain Name Service (DNS) is one of the most important components in networking infrastructure, enabling users and services to access applications by translating URLs (names) into IP addresses (numbers). Because every icon and URL and all embedded content on a website requires a DNS lookup loading complex sites necessitates hundreds of DNS queries. In addition, as more internet-enabled ‘Things' get connected, people will rely on DNS to name and find their fridges, toasters and toilets. According to a recent IDG Research Services Survey this rate of traffic will only grow. What's driving t...