News for hackers. Stuff that matters.
Re:Coding Skill and the Decline of Stagnation by jarrett on Friday January 13, 2012 @03:02PM (#3462130)
Controversial might be too strong a word, but there is a counterargument to be made.

Suppose you have a member named "x." Someday, you might want to stop storing x, and instead make it a calculated value. At which point you'll need a method. Or, when setting x, you may someday want to increment a counter, or transform the input data, or take some other action. Again, you'll need a method.

Yes, it's very easy to add an accessor later. But then you'll most likely have to edit every line of code that accessed the now-defunct member variable. Your IDE may make this easy, but I'd still prefer not to have to do it.

That's not to say writing accessors is always the best choice. I'm just saying there can be good reasons to do it. Like all things in software engineering, there's no one-size-fits-all rule about this. Notch is right about the bloat accessors create, so you have to weight the advantages and disadvantages yourself in each case.

Re:Coding Skill and the Decline of Stagnation by jiggy2011 on Friday January 13, 2012 @03:11PM (#3462180)
I think this idea is that you can use "properties" rather than public class variables.

Then you can convert your public vars into properties at a later date.

This means that you will not have to (necessarily) rewrite your code as the language knows that

a = myobj.myvar

now should call getmyvar() in the containing class and:

myob.myvar = a

should call setmyvar(a) , I know that C# certainly allows this.

Re:Coding Skill and the Decline of Stagnation by esrauch on Friday January 13, 2012 @06:00PM (#3462785)
C# allows it but but Java doesn't have this. Hence why it's standard operating procedure to just use setters from the beginning, because it might be nontrivial to update all references.
Re:Coding Skill and the Decline of Stagnation by jiggy2011 on Friday January 13, 2012 @06:26PM (#3462864)
Play! framework allows you to use getters and setters with public members as you would in C#.

Not sure how this is implemented.

Re:Coding Skill and the Decline of Stagnation by aaimnr on Friday January 13, 2012 @07:09PM (#3463000)
Spring Roo does the same through the use of aspect programming (code generation, essentially - it generates the boilerplate behind the scenes).
Re:Coding Skill and the Decline of Stagnation by cstuder on Friday January 13, 2012 @03:12PM (#3462189)
No.

C# and Objective-C (And I assume similar languages too) can have public accessors which are not used like function. No need to add ()'s everywhere.

Re:Coding Skill and the Decline of Stagnation by InclinedPlane on Friday January 13, 2012 @03:21PM (#3462227)
The way current C# does accessors / properties is just about perfect, I wish more languages would adopt it.
Re:Coding Skill and the Decline of Stagnation by masklinn on Friday January 13, 2012 @06:08PM (#3462809)
> The way current C# does accessors / properties is just about perfect

As far as I'm concerned it's quite far from it, because C# still allows public fields, properties and fields are incompatible, properties and methods are separate and Microsoft specifies different naming conventions for fields on one hand and properties & methods on the other.

Perfection is Smalltalk's way of doing it.

And in a syntactic line closer to ALGOL, Ruby is about as good as it gets: no public fields, "properties" are normal methods (setters have a little bit of syntactic sugar, but not much) and auto-generating a bunch of getters, setters or both is a class-level method call.

Re:Coding Skill and the Decline of Stagnation by de90 on Friday January 13, 2012 @06:29PM (#3462869)
What is wrong with different naming conventions for fields vs. properties, and methods? It makes it immediately obvious what is what at a glance.

*Honest question - I first learned programming with C#, and so those conventions seem 'correct' to me.

Re:Coding Skill and the Decline of Stagnation by masklinn on Saturday January 14, 2012 @04:42AM (#3463903)
> What is wrong with different naming conventions for fields vs. properties, and methods? It makes it immediately obvious what is what at a glance.

Which is precisely one of the problems of properties in C#, it makes fields and properties look and feel extremely different from the outside which violates the uniform access principle.

As I noted, ideally C# should not have public fields in the first place.

Re:Coding Skill and the Decline of Stagnation by jarrett on Friday January 13, 2012 @06:35PM (#3462889)
I should have been more clear--I was referring to Java, which seems to be what the OP was talking about.
Re:Coding Skill and the Decline of Stagnation by adgar on Friday January 13, 2012 @03:51PM (#3462339)
You say "No." so defiantly, but the reality for Java - which is what everybody else was talking about in this thread and the article - the answer is quite often "Yes."
Re:Coding Skill and the Decline of Stagnation by figglesonrails on Friday January 13, 2012 @03:24PM (#3462239)
Code bloat, at least _compiled_ code bloat, tends to be less on an issue with trivial accessors in C++ since they are generally inlined, but it does add a lot ()s. For non-trivial accessors, yeah, you can inline a lot of copies of that logic.

Vector v1(v2.getX(), v2.getY(), v2.getZ()); vs Vector v1(v2.x, v2.y, v2.z);

Re:Coding Skill and the Decline of Stagnation by Sufrostico on Friday January 13, 2012 @03:18PM (#3462212)
IMHO most software 'service life' it's not long enough to justify the annoyance of the accessors.

Most software are outdated after a couple of years.

But I still think that accessors are a must if you know that the software will be immortal (like Internet banking applications)

Re:Coding Skill and the Decline of Stagnation by figglesonrails on Friday January 13, 2012 @04:01PM (#3462388)
That's a pretty brutal stance. :|

I can't say I know the average shelf life of a software component in various languages, but I can say that at least things like Java have managed to justify their use of accessors by that metric. Maybe the focus should be on writing less throwaway code than deciding whether to "invest" a few extra minutes or not on accessors.

Re:Coding Skill and the Decline of Stagnation by Garbage on Saturday January 14, 2012 @12:09PM (#3464608)
That is an understatement. I am currently working on a code which was written at 10 years back. And that code is still running on production server.

tl;dr; Usually enterprise Java applications does have long "service life".

Re:Coding Skill and the Decline of Stagnation by akg on Saturday January 14, 2012 @01:38PM (#3464874)
I think most companies have tons of legacy code that is stilly lying around and in production use. When I was at Dreamworks Animation, we were still using tools written back in the 1980s to create animated features; albeit we kept improving on it whenever it didn't fit the bill.
Re:Coding Skill and the Decline of Stagnation by dextorious on Saturday January 14, 2012 @01:32PM (#3464854)
"""Most software are outdated after a couple of years."""

You'd be surprised. Tons of code runs in production, even in the latest of shiny systems, that was written 10 and 20 and 30 years ago -- either in whole or in parts, refactored etc.

From 1986's NeXT OS that is now OS X Lion and iOS 5, to Bill Joy's TCP/IP, to Emacs.

And tons of enterprise/banking/financial/military systems use ancient code, even 70's COBOL...

Re:Coding Skill and the Decline of Stagnation by tikhonj on Friday January 13, 2012 @03:28PM (#3462256)
Gosu supports a property syntax that lets you use = to assign things to get/set method pairs. In fact, when you load existing Java code, it replaces getFoo() and setFoo(foo) methods with a property.

This makes the code neater and enforces the abstraction--the user of the property does not need to know whether you've implemented it as a simple variable or a complicated method.

A bunch of other language let you do this as well.

Re:Coding Skill and the Decline of Stagnation by haldean on Saturday January 14, 2012 @01:32AM (#3463656)
Re:Coding Skill and the Decline of Stagnation by lclarkmichalek on Friday January 13, 2012 @03:08PM (#3462162)
A lot of languages have constructs that allow changing an attribute into a method call transparently (@property in python and D)
Re:Coding Skill and the Decline of Stagnation by dextorious on Saturday January 14, 2012 @07:15AM (#3464095)
"""Yes, it's very easy to add an accessor later. But then you'll most likely have to edit every line of code that accessed the now-defunct member variable. Your IDE may make this easy, but I'd still prefer not to have to do it."""

Easy is an understatement. It's 2-3 clicks away in Eclipse.