Application Development 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: 

Do not use fields and field symbols () globally

former_member853013
Participant

I know how to fix this warning. But I want to know what is the advantage of this SLIN warning? I no where found the good documentation on this. I see more disadvantages than the advantages. but my client wants to simply follow whatever SAP said and they want clean SLIN output even though those are just warnings and messages.

I am completely against to this and strictly follow not to use local variables (unless it is needed for reusable routine).

As per my understandings, 

The advantages are low Memory usage during run time and clearing variables point to point.

And the disadvantages are less readability, not easy maintainable, reviewing the code is bit tough, when diagnosing an issue - can't find the variable usage directly, can't use Watch points more efficiently in the debugger.

I am thinking that I may be wrong and putting here to get experts response. so I can change my mind and respect SAP decision. If I am correct, how can I let SAP know to correct this warning in next release?

Thanks in advance.

- Eswar

4 REPLIES 4

custodio_deoliveira
Active Contributor
0 Kudos

Hi Eswar,

The best way to get rid of this message is: DO NOT USE FIELDS AND FIELD SYMBOLS GLOBALLY!!!!!

Global variables are bad. Just google it and you will see. Also, you can look at SAP's Oficial Programming Guidelines.

Use globals only where you really NEED them: selection screens, dynpros, etc.

Regards,

Custodio

former_member185132
Active Contributor
0 Kudos

Hi Eswar,

I would second Custodio's view: global variables should be avoided as far as possible!

The biggest disadvantage of globals is not readability or reviewing (though those are bad enough), it is the fact that they are a recipe for unpredictable behaviour and therefore disastrous errors. When accessing (i.e. viewing or changing) the value of a global variable, you do not know whether any other code has modified/unassigned the variable/FS and this can lead to unwanted results or even crashes/dumps. This property of "touch this and something else might explode" also makes refactoring impossible.

That's why global variables are considered harmful in most other programming languages too - so rather than changing SAP's very apt warning, it would be better to change this rather risky programming practice and use globals only when absolutely essential.

Regards,

Suhas

matt
Active Contributor
0 Kudos

Eswar K wrote:

....

And the disadvantages are less readability, not easy maintainable, reviewing the code is bit tough, when diagnosing an issue - can't find the variable usage directly, can't use Watch points more efficiently in the debugger.

I am thinking that I may be wrong...

You are. The advantage of correctly scoped variables is well covered. It leads to

a) More readable code

b) More robust code

Think about it - with a global variable you have to know how the whole program handles it. With a local variable you only need to know what it is doing there.

I recommend you do some study on concepts such as encapsulation, scope and modularisation. This book is a good start. The Pragmatic Bookshelf | Welcome, Pragmatic Programmer!

former_member853013
Participant
0 Kudos

When I do global search with T_OUTPUT, I can easily know where it is being populated/appended, modified, cleared/refreshed and where/how it is being used. Watch points works better in debugger. I am still not clear on how local variable code is "More readable".

But I am satisfied with Suhas Karnik point,


Suhas Karnik wrote:

... it is the fact that they are a recipe for unpredictable behaviour and therefore disastrous errors. When accessing (i.e. viewing or changing) the value of a global variable, you do not know whether any other code has modified/unassigned the variable/FS and this can lead to unwanted results or even crashes/dumps. This property of "touch this and something else might explode" also makes refactoring impossible.

That's why global variables are considered harmful in most other programming languages too ...

I will follow this now strictly ..

Thanks,

Eswar