INITIALIZE versus MOVE.

OS/VS COBOL, COBOL II, Enterprise COBOL for z/OS. OpenCOBOL and OOCobol.
Locked
Pragya
Registered Member
Posts: 65
Joined: Wed Jun 19, 2013 9:49 am

INITIALIZE versus MOVE.

Post by Pragya »

Hi,

I have this code written:

Code: Select all

05 WS-AVG-COST  PIC S9(13)V99 COMP-3.                   
                                                        
INITIALIZE WS-AVG-COST.                                 
                                                        
       MOVE INB-AVG-COST TO WS-AVG-COST                 
       COMPUTE WS-AVG-COST ROUNDED = INB-AVG-COST * 1   
       MOVE WS-AVG-COST TO SYU12-INPUT-FIELD            
        MOVE INB-AVG-COST TO SYU12-INPUT-FIELD          
                                                        
        MOVE INI-AVG-COST TO WS-AVG-COST                
        COMPUTE WS-AVG-COST ROUNDED = INI-AVG-COST * 1  
        MOVE WS-AVG-COST TO SYU12-INPUT-FIELD           
        MOVE INI-AVG-COST TO SYU12-INPUT-FIELD          
                                                        
   MOVE INI-AVG-COST TO WS-AVG-COST                     
   COMPUTE WS-AVG-COST ROUNDED = INI-AVG-COST * 1       
To which one of my Team lead has said,

It's better to use MOVE instead of INITIALIZE, however for this code, it should be fine. Is there a rule to use MOVE and not INITIALIZE? What is the difference between the two?

Also he said that

Code: Select all

MOVE INB-AVG-COST TO WS-AVG-COST
and

Code: Select all

MOVE INI-AVG-COST TO WS-AVG-COST
are redundant as WS-AVG-COST is being calculated in the next statement, so I should remove it. I'm just thinking what difference will it make if keep it there/
nicc
Global Moderator
Global Moderator
Posts: 691
Joined: Wed Apr 23, 2014 8:45 pm

Re: INITIALIZE versus MOVE.

Post by nicc »

what difference will it make if keep it there
To your processing - none. To confusing others - everything. To maintenance - a nightmare (have I deleted some code by mistake? Why is that there? etc).
Regards
Nic
Pragya
Registered Member
Posts: 65
Joined: Wed Jun 19, 2013 9:49 am

Re: INITIALIZE versus MOVE.

Post by Pragya »

Thanks. I got the correct feedback from my lead then. Thanks.
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1896
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

Re: INITIALIZE versus MOVE.

Post by Robert Sample »

Is there a rule to use MOVE and not INITIALIZE? What is the difference between the two?
For an elementary variable, there is no difference. For a group variable, though, there is a VAST difference -- MOVE copies the source data into the group variable, left-to-right from the first byte. If the source data is smaller than the length of the group, it is filled with spaces on the right. INITIALIZE sets each eligible elementary variable within the group to its initial value (based on the type of the variable, any VALUE clause provided, and so forth). Furthermore, INITIALIZE does not do anything with FILLER (explicit OR implicit) while the MOVE will overlay FILLER as well as elementary variables.

For elementary variables, use MOVE or INITIALIZE -- whichever you want. For group variables, the decision of which to use depends upon the application and the specific requirements.
Dino
Registered Member
Posts: 52
Joined: Tue Jun 18, 2013 12:12 am

Re: INITIALIZE versus MOVE.

Post by Dino »

Robert Sample wrote: Mon Jan 21, 2019 7:46 pm Furthermore, INITIALIZE does not do anything with FILLER (explicit OR implicit) while the MOVE will overlay FILLER as well as elementary variables.
SO is that not more work compared to INITIALIZE as that's what is final expected result? Please advise.
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1896
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

Re: INITIALIZE versus MOVE.

Post by Robert Sample »

SO is that not more work compared to INITIALIZE as that's what is final expected result? Please advise.
Your comment and question are not clear since you did NOT specify what "work" you mean. For the computer, INITIALIZE uses vastly more CPU cycles than a MOVE (which can be as little as a single assembler instruction). For the programmer, MOVE LOW-VALUES (or MOVE SPACES) TO GROUP-VARIABLE doesn't require a whole lot more time than INITIALIZE GROUP-VARIABLE. If you meant something else, you'll have to explain more.
Pragya
Registered Member
Posts: 65
Joined: Wed Jun 19, 2013 9:49 am

Re: INITIALIZE versus MOVE.

Post by Pragya »

Thanks all for the discussion. I learned that INITIALIZE should be used wisely, right?
nicc
Global Moderator
Global Moderator
Posts: 691
Joined: Wed Apr 23, 2014 8:45 pm

Re: INITIALIZE versus MOVE.

Post by nicc »

Everything should be used 'wisely'.
Regards
Nic
Pragya
Registered Member
Posts: 65
Joined: Wed Jun 19, 2013 9:49 am

Re: INITIALIZE versus MOVE.

Post by Pragya »

Well...
Locked

Return to “IBM COBOL, GnuCOBOL (OpenCOBOL), OOCobol.”