IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Testing for equality


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
himanshu_pant

New User


Joined: 08 Jul 2014
Posts: 14
Location: India

PostPosted: Fri Jul 10, 2015 9:19 pm
Reply with quote

Hi,

I thought of a logic today, that wish to both consult with moderators and share on this forum.

Many a times we need to check the values of multiple variables to the same value. Like for e.g. we have


Code:

IF      A  = SPACES 
   OR B  = SPACES
   OR C  = SPACES
   OR D  = SPACES
      DO SOMETHING
END-IF   



instead of checking each variable equal to SPACES, I thought of writing this code as

Code:

IF    SPACES  = ( A OR B OR C OR D)   
      DO SOMETHING
END-IF   


because basically this IF condition is check of true or false.

I compiled it, and it executed fine.


This sort of thing we have seen more in SQL QUERIES LIKE (1=1)
more . (Evaluate I think work similarly).

I wanted to confirm moderators if it is OK if we write our code like this or does it have any potential drawbacks?
Back to top
View user's profile Send private message
Terry Heinze

JCL Moderator


Joined: 14 Jul 2008
Posts: 1249
Location: Richfield, MN, USA

PostPosted: Fri Jul 10, 2015 9:34 pm
Reply with quote

I don't see anything wrong with either way it's coded although the 1st way seems more straightforward and easier to read. I would prefer a 3rd way though -- an EVALUATE statement. Oftentimes, your shop standards dictate your coding conventions.
Back to top
View user's profile Send private message
Bill O'Boyle

CICS Moderator


Joined: 14 Jan 2008
Posts: 2501
Location: Atlanta, Georgia, USA

PostPosted: Fri Jul 10, 2015 9:37 pm
Reply with quote

In your example, the letters "A" through "D" are consecutive in the EBCDIC collating sequence, so (for example) instead of testing for each letter, you could say NOT < 'A' and NOT > 'D', which would consider A-D as valid letters.

But, there are various other methods to do this, such as IF ALPHABETIC AND NOT = SPACE, which would verify only the upper-case letters of A-Z are acceptable.

HTH....
Back to top
View user's profile Send private message
Bill Woodger

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Fri Jul 10, 2015 9:55 pm
Reply with quote

Code:
IF DELIVERED-TODAY
OR DELIVERED-THIS-WEEK
OR DELIVERED-THIS-MONTH
OR DELIVERED-THIS-QUARTER
    DO THIS-EXACT-SOMETHING
END-IF


Where:

Code:
01 A PIC X.
    88 DELIVERED-TODAY VALUE SPACE.

01  B PIC X.
    88 DELIVERED-THIS-WEEK VALUE SPACE.

01  C PIC X.
    88 DELIVERED-THIS-MONTH VALUE SPACE.

01  D PIC X.
    88 DELIVERED-THIS-QUARTER VALUE SPACE.


I don't think EVALUATE helps here, as you'll get four places where DO THIS-EXACT-SOMETHING is referenced.

You example is certainly a way of rewriting your original code, and will operate as you expect.

You'll confuse people looking at/maintain your program, though, because they won't be familiar with that.

This will also give you your expected output (with your code):

Code:
IF    SPACES  =  A OR B OR C OR D   
      DO SOMETHING
END-IF


However, someone looking at that, to understand what it is doing, will have to look at the definitions of B and C and D, because one or more of those could be an 88.

I don't see how you think it is the same as (1=1), or what use it would be if it was.

People from a non-COBOL background are more likely to write like that, as a way of "protecting" the use of an "=" in an IF. However, since in COBOL an = is never "an assignment operator" there is absolutely no need to do that.

So, meaningful condition-names, meaningful data-names, and write for ease of understanding not for shortness of code, which will confuse. As Terry says, your site standards may have a lot of impact on how you should write it anyway.
Back to top
View user's profile Send private message
himanshu_pant

New User


Joined: 08 Jul 2014
Posts: 14
Location: India

PostPosted: Fri Jul 10, 2015 10:31 pm
Reply with quote

Thanks guys.

Bill,

Sure we could achieve the same feat using conditional names as you have explained and I myself use them more often than not as you have explained.

Moreover this was just and example where I had used variable names a A, B, C or D (in COBOL coding, we seldom use A, B , C or D as variable names either unless a specific need) and all I wanted to know if it works the way that I have explained without any drawbacks( I concur readability is one, but you know as coders our minds always look for all possible solutions same thing, icon_biggrin.gif and then picking the best one of the lot, given the time you have).

I agree the coding standards in an organization dictate how one writes his code. This has nothing to do with coding standards in my organization.
Back to top
View user's profile Send private message
Bill Woodger

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Sat Jul 11, 2015 3:59 am
Reply with quote

If you consult the section on Relation Conditions in the Language Reference you'll see that all is fine with coding it in that way as far as COBOL is concerned:

Quote:
operand-1
The subject of the relation condition. Can be an identifier, literal, function-identifier, arithmetic expression, or index-name.


The reason it may be seen is due to the fear I mentioned earlier. In some languages "if a = 'a string'" is an assignment statement, usually unintended. "if 'a string' = a" won't compile, so putting any literal first gives some "protection" (but only with a string).

I don't see a benefit of doing it that way in COBOL, as people if people aren't used to that.
Back to top
View user's profile Send private message
himanshu_pant

New User


Joined: 08 Jul 2014
Posts: 14
Location: India

PostPosted: Mon Jul 13, 2015 2:46 pm
Reply with quote

What!! Its already there???
I was thinking of making it my patent. icon_smile.gif icon_biggrin.gif

Thanks Bill. icon_smile.gif
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Related to Unit Testing Testing & Performance 2
No new posts IMS DB / DB2 simulator for ETL testin... IMS DB/DC 1
No new posts Regression testing of date format con... CLIST & REXX 0
No new posts Testing rerad cursor for status with ... DB2 8
No new posts Test for multiple equality CLIST & REXX 2
Search our Forums:

Back to Top