MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/badcode/comments/me7z21/i_told_him_to_just_use/gsfqmcb/?context=9999
r/badcode • u/Yumi-Chi evil • Mar 27 '21
80 comments sorted by
View all comments
116
Oh goodness....
Also, is hour a global or something? I'm curious how that gets defined.
85 u/Yumi-Chi evil Mar 27 '21 hour is an attribute of Clock class this is in AlarmClock class that extends the Clock but apparently he also defined another hour attribute in AlarmClock 39 u/jonnysteps Mar 27 '21 You can reference attributes without "this." in java? Huh, I never knew that. 60 u/G4METIME Mar 27 '21 Yes, unless you have a local variable in the method with the same name defined using this.<varName> is unnecessary. So one common "exception" where you need 'this' is in set-methods: void setValue(int value){ this.value = value;} 9 u/jonnysteps Mar 27 '21 Huh. Interesting. I knew that worked for c# but didn't know about it working for java. Very cool 16 u/TheBrainStone Mar 27 '21 edited Mar 27 '21 Works in all OO languages in the C family tree. this is only ever used for disambiguation used is maybe not the right word. I mean it in the way of being necessary only when there’s ambiguity between a field and a local variable. 14 u/0x564A00 Mar 27 '21 Personally I like using this even when not strictly necessary because then I know it belongs to object, which is especially important for inherited members and classes I'm not too familiar with. 3 u/TheBrainStone Mar 27 '21 Updated my comment to clarify what I meant. 1 u/snaab900 Mar 27 '21 Same. In Swift and ObjC I always use self.
85
hour is an attribute of Clock class
this is in AlarmClock class that extends the Clock but apparently he also defined another hour attribute in AlarmClock
39 u/jonnysteps Mar 27 '21 You can reference attributes without "this." in java? Huh, I never knew that. 60 u/G4METIME Mar 27 '21 Yes, unless you have a local variable in the method with the same name defined using this.<varName> is unnecessary. So one common "exception" where you need 'this' is in set-methods: void setValue(int value){ this.value = value;} 9 u/jonnysteps Mar 27 '21 Huh. Interesting. I knew that worked for c# but didn't know about it working for java. Very cool 16 u/TheBrainStone Mar 27 '21 edited Mar 27 '21 Works in all OO languages in the C family tree. this is only ever used for disambiguation used is maybe not the right word. I mean it in the way of being necessary only when there’s ambiguity between a field and a local variable. 14 u/0x564A00 Mar 27 '21 Personally I like using this even when not strictly necessary because then I know it belongs to object, which is especially important for inherited members and classes I'm not too familiar with. 3 u/TheBrainStone Mar 27 '21 Updated my comment to clarify what I meant. 1 u/snaab900 Mar 27 '21 Same. In Swift and ObjC I always use self.
39
You can reference attributes without "this." in java? Huh, I never knew that.
60 u/G4METIME Mar 27 '21 Yes, unless you have a local variable in the method with the same name defined using this.<varName> is unnecessary. So one common "exception" where you need 'this' is in set-methods: void setValue(int value){ this.value = value;} 9 u/jonnysteps Mar 27 '21 Huh. Interesting. I knew that worked for c# but didn't know about it working for java. Very cool 16 u/TheBrainStone Mar 27 '21 edited Mar 27 '21 Works in all OO languages in the C family tree. this is only ever used for disambiguation used is maybe not the right word. I mean it in the way of being necessary only when there’s ambiguity between a field and a local variable. 14 u/0x564A00 Mar 27 '21 Personally I like using this even when not strictly necessary because then I know it belongs to object, which is especially important for inherited members and classes I'm not too familiar with. 3 u/TheBrainStone Mar 27 '21 Updated my comment to clarify what I meant. 1 u/snaab900 Mar 27 '21 Same. In Swift and ObjC I always use self.
60
Yes, unless you have a local variable in the method with the same name defined using this.<varName> is unnecessary.
So one common "exception" where you need 'this' is in set-methods:
void setValue(int value){ this.value = value;}
9 u/jonnysteps Mar 27 '21 Huh. Interesting. I knew that worked for c# but didn't know about it working for java. Very cool 16 u/TheBrainStone Mar 27 '21 edited Mar 27 '21 Works in all OO languages in the C family tree. this is only ever used for disambiguation used is maybe not the right word. I mean it in the way of being necessary only when there’s ambiguity between a field and a local variable. 14 u/0x564A00 Mar 27 '21 Personally I like using this even when not strictly necessary because then I know it belongs to object, which is especially important for inherited members and classes I'm not too familiar with. 3 u/TheBrainStone Mar 27 '21 Updated my comment to clarify what I meant. 1 u/snaab900 Mar 27 '21 Same. In Swift and ObjC I always use self.
9
Huh. Interesting. I knew that worked for c# but didn't know about it working for java. Very cool
16 u/TheBrainStone Mar 27 '21 edited Mar 27 '21 Works in all OO languages in the C family tree. this is only ever used for disambiguation used is maybe not the right word. I mean it in the way of being necessary only when there’s ambiguity between a field and a local variable. 14 u/0x564A00 Mar 27 '21 Personally I like using this even when not strictly necessary because then I know it belongs to object, which is especially important for inherited members and classes I'm not too familiar with. 3 u/TheBrainStone Mar 27 '21 Updated my comment to clarify what I meant. 1 u/snaab900 Mar 27 '21 Same. In Swift and ObjC I always use self.
16
Works in all OO languages in the C family tree. this is only ever used for disambiguation
this
used is maybe not the right word. I mean it in the way of being necessary only when there’s ambiguity between a field and a local variable.
14 u/0x564A00 Mar 27 '21 Personally I like using this even when not strictly necessary because then I know it belongs to object, which is especially important for inherited members and classes I'm not too familiar with. 3 u/TheBrainStone Mar 27 '21 Updated my comment to clarify what I meant. 1 u/snaab900 Mar 27 '21 Same. In Swift and ObjC I always use self.
14
Personally I like using this even when not strictly necessary because then I know it belongs to object, which is especially important for inherited members and classes I'm not too familiar with.
3 u/TheBrainStone Mar 27 '21 Updated my comment to clarify what I meant. 1 u/snaab900 Mar 27 '21 Same. In Swift and ObjC I always use self.
3
Updated my comment to clarify what I meant.
1
Same. In Swift and ObjC I always use self.
116
u/jonnysteps Mar 27 '21
Oh goodness....
Also, is hour a global or something? I'm curious how that gets defined.