Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Clearing a local variable

Bhaskar_Tripath
Participant
0 Likes
4,982

Hi All,

I was having a discussion with someone who wanted me to remove CLEAR statement with local variable. I use to follow the rule that we should always clear a variable before use. But now I couldn't find any reason why we should do this. Expert comments needed. Also, what does official SAP standards say?

Thanks,

Bhaskar

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
3,072

Hi,

Clearing variable before use is always better. But in this case you are using a local field which will get  declared only in that subroutine. Hence even if you dont clear it, field will be initial.

But inside a loop, you will have to clear(depending on circumstances).

Thanks,

Sam.

7 REPLIES 7
Read only

Former Member
0 Likes
3,073

Hi,

Clearing variable before use is always better. But in this case you are using a local field which will get  declared only in that subroutine. Hence even if you dont clear it, field will be initial.

But inside a loop, you will have to clear(depending on circumstances).

Thanks,

Sam.

Read only

Former Member
0 Likes
3,072

Hi :If you are using subroutines, f.m , methods in a loop its always good to clear local variables since you follow this practice i would advice there is no harm in doing so performance wise etc. only exist for as long as the procedure is running.  Here procedure is subroutine,f.m , methods

From official standards:

Global variables:Variables that you declare with the DATA statement live for as long as the context in which they are defined. So variables in an ABAP main program exist for the entire runtime of the program,

local variables: in subroutines, function modules , methods

Hope this helps in explaining the fact that clearing local variable depends on circumstances.

Read only

Former Member
0 Likes
3,072

Clearing a local variable prior to use is almost a replication of the data statement that created it.  I can't figure out why one of my ex-colleagues did that in every form...endform.  I can't begin to imagine how many of these time-wasters I've removed...

Question: what's the point of:

form....

data: lv_var(1) type c.

        clear lv_var.

...

Answer:  Just looks like an amateur wrote the code?

Read only

Former Member
0 Likes
3,072

Hi,

local vars do not need to be clear prior to use. They are by definition cleared after the data declaration.

Of course as mentioned before if you are using them in a LOOP then you might need to clear, depending on the situation.

Cheers

   Adi

Read only

Former Member
0 Likes
3,072

Hi,

I believe we not need to clear local variables (Of course on circumstances).

As we know the purpose of local variable is just within some scope (FORM...ENDFORM, Function Module, etc), It is no need to clear local variables all the time as global variable.

Regards,

nimz

Read only

Former Member
0 Likes
3,072

HI,

Yes this piece of code is absolutely not required for local variables used inside the sub-routines.Well few things should always be applied depending on LOGIC and may not require to search for so called SAP standards.

Cheers.

Supratim Saha.

Read only

0 Likes
3,072

Hello,

All the above statements are correct.

But, I believe that is better to write an extra line of code clearing a local variable that to omit it and under certain circumstances ( loop etc ) have a data error in a report.

I don't think that the code is written by amateurs when I see a clear statement for a local variable as mentioned above.

Regards.