Hey you coders
Nov. 25th, 2006 11:58 am![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
These questions aren't language-specific; consider the questions as pseudocode. Pretend that the underscores are actually studlyCaps if you'd like.
Also, don't worry about return values of the mutators, which is a separate question.
[Poll #875101]
Also, don't worry about return values of the mutators, which is a separate question.
[Poll #875101]
no subject
Date: 2006-11-25 05:11 pm (UTC)I have an intrinsic dislike -- learned from LISP, where this caused a bug that took about a week to track down -- of having method names that look like variable names.
no subject
Date: 2006-11-25 05:19 pm (UTC)Anyway, I ask this coz Perl culture has long used dual-purpose accessors, but adherents to "Damian style", led by the Perl Best Practices book I just read, advocate explicit separation of getters and setters, and would agree with the reason you give. So I'm of two minds about it right now.
no subject
Date: 2006-11-25 05:25 pm (UTC)For a while I stuck with studLy for public methods variables, and lower_underscore() for private methods and internal variables. This got old really fast. Nowadays I just dislike seeing underscores at all, mostly because of people with LJ handles like _________I___AM___K00L_________ and that sort of crap. (Though I retain the _initial_underscore morphology for reserved variables...)
you can grep "set_foo()" easier than "foo"
Date: 2006-11-25 09:02 pm (UTC)no subject
Date: 2006-11-26 11:45 am (UTC)get_foo()
could be doing all sorts of random things behind the scenes and you'll never know.I think the real argument against having the same method used as accessor and mutator is the same argument against having the same-named method doing entirely semantically different things depending on the type of an argument or the presence/absence of an optional argument. If
munge(x)
does something different frommunge()
, really have no business giving it the same name.I also hate studly caps. If you're going to have multiple words in your variable names, then, dammit, have actual multiple words complete with spaces to make them legible -- that is what you need the underscores for. As for them being ugly, ugliness is in the eye of the beholder.
no subject
Date: 2006-11-26 05:01 pm (UTC)Real programmers use hyphens. :)
no subject
Date: 2006-11-25 07:07 pm (UTC)no subject
Date: 2006-11-25 07:51 pm (UTC)no subject
Date: 2006-11-25 07:35 pm (UTC)no subject
Date: 2006-11-25 07:55 pm (UTC)Although what I really prefer is object.foo and object.foo = new value, but we'll have to wait for Perl 6 to really do that properly.
no subject
Date: 2006-11-27 02:19 am (UTC)no subject
Date: 2006-11-27 02:25 am (UTC)no subject
Date: 2006-11-27 03:19 am (UTC)no subject
Date: 2006-11-27 06:08 pm (UTC)no subject
Date: 2006-11-25 09:50 pm (UTC)(foo object)
and(setf (foo object) new-foo-value)
, which I've also started to use in Scheme dialects that have extensibleset!
(like Swindle). But since Scheme is a Lisp1 instead of a Lisp2, it does sometimes get in the way when you want to have a variable with the same name as an accessor function. I haven't decided yet if this matters enough.no subject
Date: 2006-11-25 09:52 pm (UTC)(setf (foo object) new-foo-value)
is pretty much equivalent to theobject.foo = new_foo_value
thatno subject
Date: 2006-11-25 11:41 pm (UTC)I'd say 'overloading is overrated' but there are times when it's really handy. I'll just say 'overloading is easily abused'.
no subject
Date: 2006-11-26 02:47 am (UTC)The get/set style in any form is to be vastly perferred. As someone else here pointed out, it can be searched on more easily, and that alone is sufficient reason to prefer it to foo() foo(new foo).
I neither know nor care about Java, as Perl and Python are my preferred scripting languages, but in C++ most of the libs I use, and the coding standards I adhere to in most of my professional work, and the code my xml-driven code generator writes (now available in a new and better incarnation), all use some variant of the get/set style.
Software development is hard, and it became very clear in the 90's, if not before, that conciseness is not in general a virtue, even in algorithmic code. Reminding people, especially the poor (usually junior) maintenance coders, what code is doing in multiple ways is a best practice.
For commericial development of application code that will be maintained over many years, there is really no alternative to get/set, preferably in the getX/setX form rather than with underscores, which are ugly and hard to read (but that's an unjustifiable personal preference, unlike the superiority of get/set.)
no subject
Date: 2006-11-26 04:26 pm (UTC)