Click here to close now.

Welcome!

.NET Authors: Carmen Gonzalez, Elizabeth White, Liz McMillan, Greg O'Connor, Jason Bloomberg

Related Topics: PowerBuilder, .NET

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
From telemedicine to smart cars, digital homes and industrial monitoring, the explosive growth of IoT has created exciting new business opportunities for real time calls and messaging. In his session at @ThingsExpo, Ivelin Ivanov, CEO and Co-Founder of Telestax, shared some of the new revenue sources that IoT created for Restcomm – the open source telephony platform from Telestax. Ivelin Ivanov is a technology entrepreneur who founded Mobicents, an Open Source VoIP Platform, to help create, deploy, and manage applications integrating voice, video and data. He is the co-founder of TeleStax, a...
SYS-CON Events announced today that Akana, formerly SOA Software, has been named “Bronze Sponsor” of SYS-CON's 16th International Cloud Expo® New York, which will take place June 9-11, 2015, at the Javits Center in New York City, NY. Akana’s comprehensive suite of API Management, API Security, Integrated SOA Governance, and Cloud Integration solutions helps businesses accelerate digital transformation by securely extending their reach across multiple channels – mobile, cloud and Internet of Things. Akana enables enterprises to share data as APIs, connect and integrate applications, drive part...
VoxImplant has announced full WebRTC support in the newest versions of its Android SDK and iOS SDK. The updated SDKs, which enable audio and video calls on mobile devices, are now compatible with the WebRTC standard to allow any mobile app to communicate with WebRTC-enabled browsers, including Google Chrome, Mozilla Firefox, Opera, and, when available, Microsoft Spartan. The WebRTC-updated SDKs represent VoxImplant's continued leadership in simplifying the development of real-time communications (RTC) services for app developers. VoxImplant (built by Zingaya, the real-time communication servi...
SYS-CON Events announced today that CommVault has been named “Bronze 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 City, NY, and the 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 sim...
SYS-CON Events announced today that SafeLogic has been named “Bag Sponsor” of SYS-CON's 16th International Cloud Expo® New York, which will take place June 9-11, 2015, at the Javits Center in New York City, NY. SafeLogic provides security products for applications in mobile and server/appliance environments. SafeLogic’s flagship product CryptoComply is a FIPS 140-2 validated cryptographic engine designed to secure data on servers, workstations, appliances, mobile devices, and in the Cloud.
SYS-CON Events announced today that StorPool Storage 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. StorPool is distributed storage software that allows service providers, enterprises and other cloud builders to run data storage on standard x86 servers, instead of using expensive and inefficient storage arrays (SAN).
The IoT Bootcamp is coming to Cloud Expo | @ThingsExpo on June 9-10 at the Javits Center in New York. Instructor. Registration is now available at http://iotbootcamp.sys-con.com/ Instructor Janakiram MSV previously taught the famously successful Multi-Cloud Bootcamp at Cloud Expo | @ThingsExpo in November in Santa Clara. Now he is expanding the focus to Janakiram is the founder and CTO of Get Cloud Ready Consulting, a niche Cloud Migration and Cloud Operations firm that recently got acquired by Aditi Technologies. He is a Microsoft Regional Director for Hyderabad, India, and one of the f...
SYS-CON Events announced today that kintone has been named “Bronze 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 City, NY, and the 17th International Cloud Expo®, which will take place on November 3–5, 2015, at the Santa Clara Convention Center in Santa Clara, CA. kintone promotes cloud-based workgroup productivity, transparency and profitability with a seamless collaboration space, build your own business application (BYOA) platform, and workflow automation system.
SYS-CON Events announced today that Site24x7, the cloud infrastructure monitoring service, 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. Site24x7 is a cloud infrastructure monitoring service that helps monitor the uptime and performance of websites, online applications, servers, mobile websites and custom APIs. The monitoring is done from 50+ locations across the world and from various wireless carriers, thus providing a global perspective of the end-user experience. Site24x7 supports monitoring H...
SYS-CON Events announced today that Intelligent Systems Services 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. Established in 1994, Intelligent Systems Services Inc. is located near Washington, DC, with representatives and partners nationwide. ISS’s well-established track record is based on the continuous pursuit of excellence in designing, implementing and supporting nationwide clients’ mission-critical systems. ISS has completed many successful projects in Healthcare, Commercial, Manufacturing, ...
WebRTC is an up-and-coming standard that enables real-time voice and video to be directly embedded into browsers making the browser a primary user interface for communications and collaboration. WebRTC runs in a number of browsers today and is currently supported in over a billion installed browsers globally, across a range of platform OS and devices. Today, organizations that choose to deploy WebRTC applications and use a host machine that supports audio through USB or Bluetooth can use Plantronics products to connect and transit or receive the audio associated with the WebRTC session.
The best mobile applications are augmented by dedicated servers, the Internet and Cloud services. Mobile developers should focus on one thing: writing the next socially disruptive viral app. Thanks to the cloud, they can focus on the overall solution, not the underlying plumbing. From iOS to Android and Windows, developers can leverage cloud services to create a common cross-platform backend to persist user settings, app data, broadcast notifications, run jobs, etc. This session provides a high level technical overview of many cloud services available to mobile app developers, includi...
SYS-CON Events announced today that B2Cloud, a provider of enterprise resource planning software, 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. B2cloud develops the software you need. They have the ideal tools to help you work with your clients. B2Cloud’s main solutions include AGIS – ERP, CLOHC, AGIS – Invoice, and IZUM
The WebRTC Summit 2015 New York, to be held June 9-11, 2015, at the Javits Center in New York, NY, announces that its Call for Papers is open. Topics include all aspects of improving IT delivery by eliminating waste through automated business models leveraging cloud technologies. WebRTC Summit is co-located with 16th International Cloud Expo, @ThingsExpo, Big Data Expo, and DevOps Summit.
SYS-CON Events announced today that Tufin, the market-leading provider of Security Policy Orchestration Solutions, 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. As the market leader of Security Policy Orchestration, Tufin automates and accelerates network configuration changes while maintaining security and compliance. Tufin's award-winning Orchestration Suite™ gives IT organizations the power and agility to enforce security policy across complex, multi-vendor enterprise networks. With more than 1...
As enterprises move to all-IP networks and cloud-based applications, communications service providers (CSPs) – facing increased competition from over-the-top providers delivering content via the Internet and independently of CSPs – must be able to offer seamless cloud-based communication and collaboration solutions that can scale for small, midsize, and large enterprises, as well as public sector organizations, in order to keep and grow market share. The latest version of Oracle Communications Unified Communications Suite gives CSPs the capability to do just that. In addition, its integration ...
SYS-CON Events announced today that Cloudian, Inc., the leading provider of hybrid cloud storage solutions, 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. Cloudian, Inc., is a Foster City, California - based software company specializing in cloud storage software. The main product is Cloudian, an Amazon S3-compliant cloud object storage platform, the bedrock of cloud computing systems, that enables cloud service providers and enterprises to build reliable, affordable and scalable cloud storage solu...
SYS-CON Events announced today that Gridstore™, the leader in hyper-converged infrastructure purpose-built to optimize Microsoft workloads, 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. Gridstore™ is the leader in hyper-converged infrastructure purpose-built for Microsoft workloads and designed to accelerate applications in virtualized environments. Gridstore’s hyper-converged infrastructure is the industry’s first all flash version of HyperConverged Appliances that include both compute and storag...
SYS-CON Events announced today that IDenticard 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. IDenticard™ is the security division of Brady Corp (NYSE: BRC), a $1.5 billion manufacturer of identification products. We have small-company values with the strength and stability of a major corporation. IDenticard offers local sales, support and service to our customers across the United States and Canada. Our partner network encompasses some 300 of the world's leading systems integrators and security s...
BroadSoft on Tuesday announced that it is a recipient of the 2014 Frost & Sullivan Market Leadership Award in the Hosted/Cloud Internet Protocol (IP) Telephony market for Latin America. According to Frost & Sullivan market research, the Latin America (LATAM) hosted/cloud Internet Protocol (IP) telephony market, including integrated unified communications and collaboration (UC&C) applications, is currently experiencing a rapid growth trajectory and is expected to exhibit a tenfold rise in annual revenues in the 2013-2020 period. With more than 600 cloud deployments internationally, BroadSoft w...