Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does it make sense to use a public field?

Tags:

This is a question I have had for a while now:

When does it make sense to expose a field publicly like so?

public class SomeClass() {    public int backing; } 

The downside of doing this (in addition to angering OOP elitists) is that you have to introduce a breaking change to your API if you ever need to add any logic on top of this data. I suppose that is what the elitists are on about.

Best practice in Java and C# has long been to use getters/setters or properties to access fields.

public class SomeClass() {    private int backing;     public int getBacking()    {       return backing;    }     public void setBacking(int v)    {       backing = v;    } } 

C# has evolved this to pretty simple syntax with automatic properties:

public class SomeClass() {    public int Backing { get; set; } } 

Lazy me still feels this is too long though since it something I find myself doing a lot. More importantly, I am not sure I know where a public field would make more sense.

Why not just treat publicly declared fields as properties (or methods) behind the scenes? That way it would be impossible to anger decoupling gods and be a bit less typing to boot.

public class SomeClass() {    public int backing;   // The same as public int backing { get; set; } } 

For a property that does nothing but wrap the underlying field, I am pretty sure the JIT optimizes the method calls away so performance is probably not the issue. Any thoughts? (other than the proper case convention for the field name)

EDIT: Thanks for all the responses. I feel like perhaps not everyone is understanding my question though. When would a public field be a better choice than a property? Why is it a better choice? If the only reason is convenience (less typing and clutter), then what would be the downside of having the compiler generate a property "under-the-hood" whenever a public field is encountered. Basically, it would be impossible to create a true public field (because they would all be properties). What would be wrong with that? Thanks.

like image 433
Justin Avatar asked Aug 08 '11 23:08

Justin


People also ask

When to use field vs property?

A field is a variable of any type that is declared directly in a class. A property is a member that provides a flexible mechanism to read, write or compute the value of a private field. A field can be used to explain the characteristics of an object or a class.

Can a field be public?

Fields can be marked as public, private, protected, internal, protected internal, or private protected. These access modifiers define how users of the type can access the fields. For more information, see Access Modifiers. A field can optionally be declared static.

When should a field be private?

Fields should be declared private unless there is a good reason for not doing so. One of the guiding principles of lasting value in programming is "Minimize ripple effects by keeping secrets." When a field is private , the caller cannot usually get inappropriate direct access to the field.

What are public fields in Java?

public is a Java keyword which declares a member's access as public. Public members are visible to all other classes. This means that any other class can access a public field or method. Further, other classes can modify public fields unless the field is declared as final .


2 Answers

In my opinion, when you design a structure of a class you should pay more attention to future changes and should always be friendly to them. If a future requirement need you to do some logic before returning a value instead of just returnning the value of the field, you'll have to change the interface of the class, and all users of your library will have to change. That usually become a disaster.

Keep in mind, the open / close principle.

like image 99
gekowa Avatar answered Oct 18 '22 08:10

gekowa


When does it make sense to expose a field publicly?

Among other places, on private classes as below.

public class MyOuterClass {       private class MyInnerClass       {             public int val ; // why bother w/boiler plate       } } 
like image 31
emory Avatar answered Oct 18 '22 09:10

emory