Page 1 of 1

INITIALIZE versus MOVE.

Posted: Mon Jan 21, 2019 12:35 pm
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/

Re: INITIALIZE versus MOVE.

Posted: Mon Jan 21, 2019 2:30 pm
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).

Re: INITIALIZE versus MOVE.

Posted: Mon Jan 21, 2019 3:54 pm
by Pragya
Thanks. I got the correct feedback from my lead then. Thanks.

Re: INITIALIZE versus MOVE.

Posted: Mon Jan 21, 2019 7:46 pm
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.

Re: INITIALIZE versus MOVE.

Posted: Wed Jan 30, 2019 2:31 pm
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.

Re: INITIALIZE versus MOVE.

Posted: Wed Jan 30, 2019 6:21 pm
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.

Re: INITIALIZE versus MOVE.

Posted: Sun Feb 03, 2019 12:18 am
by Pragya
Thanks all for the discussion. I learned that INITIALIZE should be used wisely, right?

Re: INITIALIZE versus MOVE.

Posted: Sun Feb 03, 2019 6:41 pm
by nicc
Everything should be used 'wisely'.

Re: INITIALIZE versus MOVE.

Posted: Wed Feb 06, 2019 7:05 pm
by Pragya
Well...