Welcome!

.NET Authors: Yeshim Deniz, Carmen Gonzalez, Greg O'Connor, Pat Romanski, Elizabeth White

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
SYS-CON Events announced today that Red Hat, the world's leading provider of open source solutions, will exhibit at Internet of @ThingsExpo, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Red Hat is the world's leading provider of open source software solutions, using a community-powered approach to reliable and high-performing cloud, Linux, middleware, storage and virtualization technologies. Red Hat also offers award-winning support, training, and consulting services. As the connective hub in a global network of enterprises, partners, a...
Almost everyone sees the potential of Internet of Things but how can businesses truly unlock that potential. The key will be in the ability to discover business insight in the midst of an ocean of Big Data generated from billions of embedded devices via Systems of Discover. Businesses will also need to ensure that they can sustain that insight by leveraging the cloud for global reach, scale and elasticity.
SYS-CON Events announced today that SOA Software, an API management leader, will exhibit at SYS-CON's 15th International Cloud Expo®, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. SOA Software is a leading provider of API Management and SOA Governance products that equip business to deliver APIs and SOA together to drive their company to meet its business strategy quickly and effectively. SOA Software’s technology helps businesses to accelerate their digital channels with APIs, drive partner adoption, monetize their assets, and achieve a...
From a software development perspective IoT is about programming "things," about connecting them with each other or integrating them with existing applications. In his session at @ThingsExpo, Yakov Fain, co-founder of Farata Systems and SuranceBay, will show you how small IoT-enabled devices from multiple manufacturers can be integrated into the workflow of an enterprise application. This is a practical demo of building a framework and components in HTML/Java/Mobile technologies to serve as a platform that can integrate new devices as they become available on the market.
BSQUARE is a global leader of embedded software solutions. We enable smart connected systems at the device level and beyond that millions use every day and provide actionable data solutions for the growing Internet of Things (IoT) market. We empower our world-class customers with our products, services and solutions to achieve innovation and success. For more information, visit www.bsquare.com.
SYS-CON Events announced today that Utimaco will exhibit at SYS-CON's 15th International Cloud Expo®, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Utimaco is a leading manufacturer of hardware based security solutions that provide the root of trust to keep cryptographic keys safe, secure critical digital infrastructures and protect high value data assets. Only Utimaco delivers a general-purpose hardware security module (HSM) as a customizable platform to easily integrate into existing software solutions, embed business logic and build s...
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 style...
Connected devices are changing the way we go about our everyday life, from wearables to driverless cars, to smart grids and entire industries revolutionizing business opportunities through smart objects, capable of two-way communication. But what happens when objects are given an IP-address, and we rely on that connection, sometimes with our lives? How do we secure those vast data infrastructures and safe-keep the privacy of sensitive information? This session will outline how each and every connected device can uphold a core root of trust via a unique cryptographic signature – a “bir...
Internet of @ThingsExpo Silicon Valley announced on Thursday its first 12 all-star speakers and sessions for its upcoming event, which will take place November 4-6, 2014, at the Santa Clara Convention Center in California. @ThingsExpo, the first and largest IoT event in the world, debuted at the Javits Center in New York City in June 10-12, 2014 with over 6,000 delegates attending the conference. Among the first 12 announced world class speakers, IBM will present two highly popular IoT sessions, which will take place November 4-6, 2014 at the Santa Clara Convention Center in Santa Clara, Calif...
SYS-CON Events announced today that Matrix.org has been named “Silver Sponsor” of Internet of @ThingsExpo, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Matrix is an ambitious new open standard for open, distributed, real-time communication over IP. It defines a new approach for interoperable Instant Messaging and VoIP based on pragmatic HTTP APIs and WebRTC, and provides open source reference implementations to showcase and bootstrap the new standard. Our focus is on simplicity, security, and supporting the fullest feature set.
P2P RTC will impact the landscape of communications, shifting from traditional telephony style communications models to OTT (Over-The-Top) cloud assisted & PaaS (Platform as a Service) communication services. The P2P shift will impact many areas of our lives, from mobile communication, human interactive web services, RTC and telephony infrastructure, user federation, security and privacy implications, business costs, and scalability. In his session at Internet of @ThingsExpo, Robin Raymond, Chief Architect at Hookflash Inc., will walk through the shifting landscape of traditional telephone a...
WebRTC defines no default signaling protocol, causing fragmentation between WebRTC silos. SIP and XMPP provide possibilities, but come with considerable complexity and are not designed for use in a web environment. In his session at Internet of @ThingsExpo, Matthew Hodgson, technical co-founder of the Matrix.org, will discuss how Matrix is a new non-profit Open Source Project that defines both a new HTTP-based standard for VoIP & IM signaling and provides reference implementations.

SUNNYVALE, Calif., Oct. 20, 2014 /PRNewswire/ -- Spansion Inc. (NYSE: CODE), a global leader in embedded systems, today added 96 new products to the Spansion® FM4 Family of flexible microcontrollers (MCUs). Based on the ARM® Cortex®-M4F core, the new MCUs boast a 200 MHz operating frequency and support a diverse set of on-chip peripherals for enhanced human machine interfaces (HMIs) and machine-to-machine (M2M) communications. The rich set of periphera...

SYS-CON Events announced today that Aria Systems, the recurring revenue expert, has been named "Bronze Sponsor" of SYS-CON's 15th International Cloud Expo®, which will take place on November 4-6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Aria Systems helps leading businesses connect their customers with the products and services they love. Industry leaders like Pitney Bowes, Experian, AAA NCNU, VMware, HootSuite and many others choose Aria to power their recurring revenue business and deliver exceptional experiences to their customers.
The Internet of Things (IoT) is going to require a new way of thinking and of developing software for speed, security and innovation. This requires IT leaders to balance business as usual while anticipating for the next market and technology trends. Cloud provides the right IT asset portfolio to help today’s IT leaders manage the old and prepare for the new. Today the cloud conversation is evolving from private and public to hybrid. This session will provide use cases and insights to reinforce the value of the network in helping organizations to maximize their company’s cloud experience.
The Internet of Things (IoT) is making everything it touches smarter – smart devices, smart cars and smart cities. And lucky us, we’re just beginning to reap the benefits as we work toward a networked society. However, this technology-driven innovation is impacting more than just individuals. The IoT has an environmental impact as well, which brings us to the theme of this month’s #IoTuesday Twitter chat. The ability to remove inefficiencies through connected objects is driving change throughout every sector, including waste management. BigBelly Solar, located just outside of Boston, is trans...
SYS-CON Events announced today that Matrix.org has been named “Silver Sponsor” of Internet of @ThingsExpo, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Matrix is an ambitious new open standard for open, distributed, real-time communication over IP. It defines a new approach for interoperable Instant Messaging and VoIP based on pragmatic HTTP APIs and WebRTC, and provides open source reference implementations to showcase and bootstrap the new standard. Our focus is on simplicity, security, and supporting the fullest feature set.
Predicted by Gartner to add $1.9 trillion to the global economy by 2020, the Internet of Everything (IoE) is based on the idea that devices, systems and services will connect in simple, transparent ways, enabling seamless interactions among devices across brands and sectors. As this vision unfolds, it is clear that no single company can accomplish the level of interoperability required to support the horizontal aspects of the IoE. The AllSeen Alliance, announced in December 2013, was formed with the goal to advance IoE adoption and innovation in the connected home, healthcare, education, aut...
SYS-CON Events announced today that Red Hat, the world's leading provider of open source solutions, will exhibit at Internet of @ThingsExpo, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Red Hat is the world's leading provider of open source software solutions, using a community-powered approach to reliable and high-performing cloud, Linux, middleware, storage and virtualization technologies. Red Hat also offers award-winning support, training, and consulting services. As the connective hub in a global network of enterprises, partners, a...
The only place to be June 9-11 is Cloud Expo & @ThingsExpo 2015 East at the Javits Center in New York City. Join us there as delegates from all over the world come to listen to and engage with speakers & sponsors from the leading Cloud Computing, IoT & Big Data companies. Cloud Expo & @ThingsExpo are the leading events covering the booming market of Cloud Computing, IoT & Big Data for the enterprise. Speakers from all over the world will be hand-picked for their ability to explore the economic strategies that utility/cloud computing provides. Whether public, private, or in a hybrid form, clo...