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

Public Read-Only Attributes

Former Member
0 Likes
2,647

I'm trying to pick up ABAP on my own by reading through various resources I've collected and writing my own programs. Unfortunately I don't have an experienced ABAP guru to ask questions of. I'm hoping someone can help me with the following:

Based on my other programming experience I know that it is very common for object attributes to be defined as private and then accessor/mutator (getter/setter) methods to be defined to provide access to the attribute.

In my study I noted that ABAP allows attributes to be made public but declared to be read-only. This would seem to permit a structure where accessor/getter methods are not needed (the attribute can be read externally) and one would only have to define a mutator/setter method.

My question: what is typically done in 'real-world' programs? Is there any reason why using read-only would be more or less preferred?

Thanks for any help that can be provided.

1 ACCEPTED SOLUTION
Read only

nomssi
Active Contributor
0 Likes
2,210

Hello Tony,

a public attribute gives up encapsulation for ease of use. Read only attributes gives access control back, but a subclass still cannot redefine the getter (e.g. for validation, tracking... purposes).

Remember [Tell don't ask|http://www.pragprog.com/articles/tell-dont-ask]? Even getters breaks encapsulation, so IMHO real-world usage is a trade-off and I will not consider typical usage but only propose two worthwhile contexts for public read only attributes:

- the class interface is not [published|http://martinfowler.com/ieeeSoftware/published.pdf] (PDF) or

- immutable [value objects|http://devlicio.us/blogs/casey/archive/2009/02/13/ddd-entities-and-value-objects.aspx], e.g. attribute is setup once and never changed.

regards,

J.N.N.

I'm trying to pick up ABAP on my own by reading through various resources I've collected and writing my own programs. Unfortunately I don't have an experienced ABAP guru to ask questions of. I'm hoping someone can help me with the following:

Based on my other programming experience I know that it is very common for object attributes to be defined as private and then accessor/mutator (getter/setter) methods to be defined to provide access to the attribute.

In my study I noted that ABAP allows attributes to be made public but declared to be read-only. This would seem to permit a structure where accessor/getter methods are not needed (the attribute can be read externally) and one would only have to define a mutator/setter method.

My question: what is typically done in 'real-world' programs? Is there any reason why using read-only would be more or less preferred?

Thanks for any help that can be provided.

6 REPLIES 6
Read only

uwe_schieferstein
Active Contributor
0 Likes
2,210

Hello Tony

Below I have listed a few examples from standard ABAP classes. I cannot say whether you find more public read-only attributes than GETTER methods - you will find examples for both of them.

I could imagine that before it was possible to use the functional method call in ABAP that public read-only attributes were an alternative to the "verbose" method cal (see below)l.


" RE-FX: Contract
CL_RECN_CONTRACT-IF_RECN_CONTRACT~MS_DETAIL   " public read-only attribute

" MM: Material
CL_IBASE_R3_MATERIAL->GET_MARA  " GETTER method

"FI/CO: Company Code
CL_REEXC_COMPANY_CODE=>GET_DETAIL  " GETTER method 

" HR: Employee
CL_PT_EMPLOYEE->IF_PT_EMPLOYEE~PERNR   " public read-only attribute


" Assumption: CL_IBASE_R3_MATERIAL would have 
" a public read-only attribute MS_DETAIL (of TYPE mara)

" Normal method call (verbose):
CALL METHOD go_material->GET_MARA
  RECEIVING
    R_MARA = ls_mara.

" Functional method call:
  ls_mara = go_material->get_mara( ).

" Accessing public attribute
  ls_mara = go_material->ms_detail.

Regards

Uwe

Read only

nomssi
Active Contributor
0 Likes
2,211

Hello Tony,

a public attribute gives up encapsulation for ease of use. Read only attributes gives access control back, but a subclass still cannot redefine the getter (e.g. for validation, tracking... purposes).

Remember [Tell don't ask|http://www.pragprog.com/articles/tell-dont-ask]? Even getters breaks encapsulation, so IMHO real-world usage is a trade-off and I will not consider typical usage but only propose two worthwhile contexts for public read only attributes:

- the class interface is not [published|http://martinfowler.com/ieeeSoftware/published.pdf] (PDF) or

- immutable [value objects|http://devlicio.us/blogs/casey/archive/2009/02/13/ddd-entities-and-value-objects.aspx], e.g. attribute is setup once and never changed.

regards,

J.N.N.

Read only

matt
Active Contributor
0 Likes
2,210

I default to setters and getters. I break that when it's clearly overkill. I don't think I've ever used read-only public attributes - either I give full access or none.

Read only

Former Member
0 Likes
2,210

I just think of public read-only attributes as private attributes with getters written for me. If you get in the habit of using these instead of private attributes with public getters, you simply write less code!

There is no drawback I know of except it may styalistically clash with the other way.

Read only

naimesh_patel
Active Contributor
0 Likes
2,210

My 2 cents:

- It is cumbersome to use the GETTER methods till release 702 EhP2, as we can't use the chaining statement. We need to use the helper variables to temporarily store the values and pass it to next method.

- If you care for performance, it is better to use READ-ONLY attribute compare to GETTER methods.

You can read more at [READ-ONLY attribute vs GETTER methods|http://help-abap.blogspot.com/2011/02/read-only-attribute-vs-getter-methods.html]

Regards,

Naimesh Patel

Read only

0 Likes
2,210

Great discussion everyone. Thanks for your thoughts!