tags:

views:

589

answers:

10

If you're writing something by yourself, whether to practice, solve a personal problem, or just for entertainment, is it ok, once in a while, to have a public field? Maybe?

+10  A: 

I think it's reasonable if you're consciously throwing the code away afterwards. In particular, if you're experimenting with something completely different, taking shortcuts makes sense. Just don't let it lead to habits which cross over into "real" code.

Jon Skeet
Emphasis on *throwing the code away*.
Martinho Fernandes
Why is everyone forgetting about the DTO pattern?
Justin Johnson
Perhaps not everyone is familiar with the DTO pattern.
Mike Hofer
+4  A: 

Violating general principles is always "ok"! It is not an error to violate a principle but it is a trade off. The cost of not writing clean code will be higher the longer your software will survive. My take on this is: If in doubt make it clean!

jens
+3  A: 

Of course it's OK. It's your code, you can do whatever you want with it. Personally, I try to stick to good practice also in my private code, just to make it a natural habit so I don't have to think about it.

Fredrik Mörk
+2  A: 

The short answer is yes, if you believe that you're gaining a lot by making things public instead of private with accessors you are welcome to do so. Consistency, I think, is the biggest thing to keep in mind. For instance, don't make some variables straight public, and some not. Do the same across the board if you break with best practices. It comes back to a trade-off. Almost no-one follows many of the IEEE specs for how Software Engineering should be executed and documented because the overhead is far too great, and it can get unmanageable. The same is true for personal, light-weight programming. It's okay to do something quick and dirty, just do not get used to it.

TahoeWolverine
+30  A: 

Let me give you an analogy.
I come from a part of the world where English is not the primary language. But it’s necessary for all things in life.
During one of those usual days during my pre-teen years I said something very funny in English. Then my Dad said, “Son, think in English. Then you’ll get fluent

I think it applies perfectly to this situation.

Think,try and question best practices in your playground. You will soon realize what’s best for what.Why are properties needed in the first place. Why should this be public? Why should I not call a virtual member from the constructor? Let me try using "new" modifier for a method call. What happens when I write 10 nested levels of if-then-else and try reading it again after 10 days. Why the heck should I use a factory pattern for a simple project. Et cetera.

And then you’ll realize without shooting at your foot, why design patterns are patterns...

Cherian
+1 good answer, I like it
Juri
Don't forget that there are design patterns that call for public members, such as DTO.
Justin Johnson
+1  A: 

Public members are acceptable in the Data Transfer Object design patter:

Typically, the members in the Transfer Object are defined as public, thus eliminating the need for get and set methods.

Justin Johnson
+1  A: 

One of the key advantages of OOP is for scaling and maintainability. By encapsulating code, one can hide the implementation. This means other programmers don't have to know the implementation, and can't change your object's internal state. If you language doesn't support properties, you end up with a lot of code which obfuscates and bloats your project. If the code doesn't need to be worked on by multiple programmers, you aren't producing a reusable component, and YOU are the maintenance programmer, then code in whatever manner allows you to get things done.

Does a maid need to make his/her own bed in the morning in order to practice properly making a bed?

brianegge
A: 

Side note: it also depends on the language:

In Scala, according to the Uniform Access Principle, clients read and write field values as if they are publicly accessible, even though in some case they are actually calling methods. The maintainer of the class has the freedom to change the implementation without forcing users to make code changes.

Scala keeps field and method names in the same namespace.
Many languages, like Java, keep field and method names in separate namespaces.
However, these languages can’t support the uniform access principle as a result, unless they build in ad hoc support in their grammars or compilers.


So the real question is:

What service are you exposing (here by having a public field)?.

If the service (get/set a given type value) makes sense for your API, then the "shortcut" is legitimate.
As long as you encapsulate that field eventually, is it ok because you made the shortcut for the "right" reason (API and service exposure), versus the "wrong" reason (quick ad-hoc access).
A good unit test (thinking like the user of your API) can help you check if that field should be accessed directly or if it is only useful for internal development of other classes within your program.

VonC
Agreed. In python, there is no real enforcement of public/private, it is rather more like a trust that you will not access __ or _ prefixed attributes, or that you are aware they may break. Similarly, you can replace a .property access with a real property, later, when the complexity of the class requires it. Do it when it is required, not before.
Matthew Schinckel
+1  A: 

I cant vote i would second cherans answer

Aneef
Add it as a comment and not as an answer...
Cherian
IIRC, you can't comment with 1 rep, either.
Matthew Schinckel
There, now you can vote. Go crazy.
Michael Myers
A: 

Here's my take on it:

  1. I'd advise avoiding public fields. They have a nasty habit of biting you later on because you can't control them. (The word you're looking for here is volatility.) Further, if you decide to change their internal implementation, you have to touch a lot more code.

  2. Then again, that's what refactoring tools are for. If you have a decent refactoring tool, that's not nearly so difficult.

  3. There is no silver bullet. I can't repeat this enough. If you have work to do, and you need to get it done in a hurry, writing one line of code instead of eight (as is the case in Visual Basic) is certainly faster.

  4. Rules were meant to be broken. If a rule doesn't necessarily apply in your case, don't use it. Design patterns, coding guidelines, laws and best practices should not be treated as a straightjacket that requires you to needlessly complicate your code to the point where it is enormously complex and difficult to understand and maintain. Don't let someone force you into a practice just because it's popular or "standard" when it doesn't fit your requirements.

Again, this is a subjective opinion, and your mileage may vary.

Mike Hofer