Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #33150 > unrolled thread
| Started by | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| First post | 2015-11-20 09:21 +0100 |
| Last post | 2015-11-29 22:51 -0800 |
| Articles | 8 — 7 participants |
Back to article view | Back to comp.lang.java.programmer
Using this Cecil Westerhof <Cecil@decebal.nl> - 2015-11-20 09:21 +0100
Re: Using this Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-20 08:15 -0500
Re: Using this Eric Douglas <e.d.programmer@gmail.com> - 2015-11-20 05:43 -0800
Re: Using this Martin Gregorie <martin@address-in-sig.invalid> - 2015-11-20 21:52 +0000
Re: Using this Martin Gregorie <martin@address-in-sig.invalid> - 2015-11-20 22:41 +0000
Re: Using this Arne Vajhøj <arne@vajhoej.dk> - 2015-11-20 20:26 -0500
Re: Using this Brixomatic <brixomatic@yahoo.com> - 2015-11-29 16:25 +0100
Re: Using this Lew <lewbloch@gmail.com> - 2015-11-29 22:51 -0800
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Date | 2015-11-20 09:21 +0100 |
| Subject | Using this |
| Message-ID | <87poz5ytcf.fsf@Equus.decebal.nl> |
What is the ‘best’ way to use this? I normally only use it when necessary. (A function has a parameter with the same name.) But at the company I am working there are people who say you should use it always. This resulted at on line of code with four times a this. Made the line a lot longer and I find it very ugly. Is there a rational for constantly using this? -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof
[toc] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2015-11-20 08:15 -0500 |
| Message-ID | <n2n68r$6j2$1@dont-email.me> |
| In reply to | #33150 |
On 11/20/2015 3:21 AM, Cecil Westerhof wrote:
> What is the ‘best’ way to use this? I normally only use it when
> necessary. (A function has a parameter with the same name.) But at the
> company I am working there are people who say you should use it
> always. This resulted at on line of code with four times a this. Made
> the line a lot longer and I find it very ugly. Is there a rational for
> constantly using this?
Maybe you should ask the people who tell you to use it.
Speaking for myself (and not for those people whose opinions
I don't know), I use `this' explicitly:
- In constructors, to invoke a different constructor version,
- In constructors, to set an instance field from a parameter
of the same name,
- In equals() methods, where I like to compare instance fields
of `this' object and `that' object,
- When the phase of the moon is favorable.
Perhaps your associates feel that an explicit `this' is a good way
to distinguish element uses from variable or parameter uses? I
don't know; ask them.
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
[toc] | [prev] | [next] | [standalone]
| From | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| Date | 2015-11-20 05:43 -0800 |
| Message-ID | <32d97b3b-41ca-40b9-88b1-08e092be89d7@googlegroups.com> |
| In reply to | #33150 |
On Friday, November 20, 2015 at 3:44:11 AM UTC-5, Cecil Westerhof wrote: > What is the 'best' way to use this? I normally only use it when > necessary. (A function has a parameter with the same name.) But at the > company I am working there are people who say you should use it > always. This resulted at on line of code with four times a this. Made > the line a lot longer and I find it very ugly. Is there a rational for > constantly using this? I only use it where necessary, but it's often just a personal preference, so if you're working on someone else's API you probably want to stay consistent to any convention they've used. I don't know many scenarios where I've personally used it offhand, mostly in overloading constructors. If for instance you use Eclipse to auto-write a getter-setter method they use the class variable as the method variable name and put in this.var=var by default but I think that's ugly if not confusing. I think class variables and method variables should have different names.
[toc] | [prev] | [next] | [standalone]
| From | Martin Gregorie <martin@address-in-sig.invalid> |
|---|---|
| Date | 2015-11-20 21:52 +0000 |
| Message-ID | <n2o4m1$tim$1@dont-email.me> |
| In reply to | #33150 |
On Fri, 20 Nov 2015 17:27:38 +0000, Stefan Ram wrote: > Cecil Westerhof <Cecil@decebal.nl> writes: >>What is the ?best? way to use this? I normally only use it when >>necessary. (A function has a parameter with the same name.) But at the >>company I am working there are people who say you should use it always. > > If the people are your boss (lead programmer or some such), you should > use it always. > That depends whether your boss is a bozo. Example: on one project where I was technical lead, the Programming Manager issued a fairly sensible coding spec (including the use of meaningful variable and function names, e.g. maxvalue for a limiting int value and search_namelist() for a function that searched for a match and returned a pointer to the matching namestring or NULL, and then went off on holiday for two weeks. Meanwhile we got stuck in and wrote/unit tested the system's infrastructure code, naturally adhering to the coding spec. Project Manager can back and immediately issued a new coding spec he'd evidently written while on holiday. This mandated - bloody Hungarian Notation. This standard would have required that int to be called iMaxvalue and the search function to be called psNamelist and, apart from making code less readable, means that what should be trivial changes like replacing an int with a long or a double with a float, would require all references to those variable to be hunted down and their names changed. The bozo had heard that coding specs can never be changed once code exists. His reaction, once we;d pointed out how much code would need changing an retesting, was to effectively drive a truck through any and all coding standards by saying that existing code didn't need to be changed but all code written after then gad to use Hungarian notation. May $codingDeity protect you from ever having to work for such an idiot. -- martin@ | Martin Gregorie gregorie. | Essex, UK org |
[toc] | [prev] | [next] | [standalone]
| From | Martin Gregorie <martin@address-in-sig.invalid> |
|---|---|
| Date | 2015-11-20 22:41 +0000 |
| Message-ID | <n2o7ip$den$1@dont-email.me> |
| In reply to | #33166 |
On Fri, 20 Nov 2015 21:52:01 +0000, Martin Gregorie wrote: > On Fri, 20 Nov 2015 17:27:38 +0000, Stefan Ram wrote: > >> Cecil Westerhof <Cecil@decebal.nl> writes: >>>What is the ?best? way to use this? I normally only use it when >>>necessary. (A function has a parameter with the same name.) But at the >>>company I am working there are people who say you should use it always. >> >> If the people are your boss (lead programmer or some such), you >> should use it always. >> > That depends whether your boss is a bozo. Example: on one project where > I was technical lead, the Programming Manager issued a fairly sensible > coding spec (including the use of meaningful variable and function > names, > e.g. maxvalue for a limiting int value and search_namelist() for a > function that searched for a match and returned a pointer to the > matching namestring or NULL, and then went off on holiday for two weeks. > Meanwhile we got stuck in and wrote/unit tested the system's > infrastructure code, naturally adhering to the coding spec. > > Project Manager can back and immediately issued a new coding spec he'd > evidently written while on holiday. This mandated - bloody Hungarian > Notation. This standard would have required that int to be called > iMaxvalue and the search function to be called psNamelist and, apart s/psNamelist/psSearchNamelist/ > from making code less readable, means that what should be trivial > changes like replacing an int with a long or a double with a float, > would require all references to those variable to be hunted down and > their names changed. > > The bozo had heard that coding specs can never be changed once code s/had heard/had not heard/ > exists. His reaction, once we'd pointed out how much code would need > changing and retesting, was to effectively drive a truck through any and > all coding standards by saying that existing code didn't need to be > changed but all code written after then had to use Hungarian notation. > > May $codingDeity protect you from ever having to work for such an idiot. Apologies for the typos. -- martin@ | Martin Gregorie gregorie. | Essex, UK org |
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2015-11-20 20:26 -0500 |
| Message-ID | <n2oh7h$mvm$1@speranza.aioe.org> |
| In reply to | #33150 |
On 11/20/2015 3:21 AM, Cecil Westerhof wrote:
> What is the ‘best’ way to use this? I normally only use it when
> necessary. (A function has a parameter with the same name.) But at the
> company I am working there are people who say you should use it
> always. This resulted at on line of code with four times a this. Made
> the line a lot longer and I find it very ugly. Is there a rational for
> constantly using this?
I think there are 3 practices:
A) only use it when needed
B) also use it whenever the surrounding code use it
C) always use it
It is my impression that:
usage(A) > usage(B) > usage(C)
I am somewhat inclined to B myself.
public class X {
private int a;
private int b;
public X(int a) {
this.a = a;
this.b = FOOBAR; // this is not needed here but because it is
used at the line above then I think it looks more readable this way
}
...
}
Arne
[toc] | [prev] | [next] | [standalone]
| From | Brixomatic <brixomatic@yahoo.com> |
|---|---|
| Date | 2015-11-29 16:25 +0100 |
| Message-ID | <MPG.30c5372517331a259896a6@news.aioe.org> |
| In reply to | #33150 |
In article <87poz5ytcf.fsf@Equus.decebal.nl>, Cecil Westerhof (Cecil@decebal.nl) says... > > What is the ?best? way to use this? I normally only use it when > necessary. (A function has a parameter with the same name.) But at the > company I am working there are people who say you should use it > always. This resulted at on line of code with four times a this. Made > the line a lot longer and I find it very ugly. Is there a rational for > constantly using this? A matter of coding guidelines. Coding guidelines should be agreed on in the group and kept rather stable to not introduce too many changes that are not changes in functionality. Anyway. The coding guidelines that I've seen the most, and that I find the most practical too, are to use "this" only when necessary. Some guidelines discourage shadowing a field name with parameter names, so the field would be called "x" and the method parameter would be called "_x" or "theX" or something. But I got to hate that. Yes, it may protect you from using a field/paremeter when you really want to use the other, but seriously: 1992 is gone, todays's code editing tools have syntax highlighting that show the difference. Plus it is annoying when you change a method from say static to instance bound or back and use the field values (or the other way round) and have to rename all the usages (yes, IDEs make that easier too, but it sucks anyway). I have wasted more time changing the names for not good reason other than to adhere to that code guiseline, than solving problems with shadowing. And that's almost the same annoyance that you see get with excessively using "this". But as I said: You should dicuss that as a team and then stick to it. Also: Sharing the auto-formatter settings of your IDE in the source code repository is helpful to enforce it. Kind regards, Wanja
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2015-11-29 22:51 -0800 |
| Message-ID | <6c10f3f0-8b50-44a5-9dec-147cbc529844@googlegroups.com> |
| In reply to | #33150 |
On Friday, November 20, 2015 at 12:44:11 AM UTC-8, Cecil Westerhof wrote: > What is the 'best' way to use this? I normally only use it when > necessary. (A function has a parameter with the same name.) But at the > company I am working there are people who say you should use it > always. This resulted at on line of code with four times a this. Made > the line a lot longer and I find it very ugly. Is there a rational for > constantly using this? Most Java (and related) code I see, except that by certain rote-trained coders, uses 'this.' for variables being set by setters or constructors, otherwise it is not needed and not used. I'm most comfortable with that personally. However, house standards will prevail. As one programming manager said one gig long ago, "You will hate our coding standard. Everyone here does. I hate it. But we all follow it."
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web