Hard Light Productions Forums

Off-Topic Discussion => General Discussion => Topic started by: Flipside on January 08, 2008, 02:45:11 pm

Title: A Question about Java....
Post by: Flipside on January 08, 2008, 02:45:11 pm
Ok, Im pretty comfortable with basic Java, but there's one thing that confuses me...

Why does the 'This' claimer for variables exist? People really should not be encouraged, in my opinion, to use the same name for variables as you are for fields.

My tutor says that some variable names are too obvious not to use, but I simply tag the name of the Method onto it, so age becomes, for example isSeniorAge if the method is called isSenior, etc.

I'm still trying to figure out the relevance of this command, it just strikes me that it's GOTO, waiting to happen all over again.
Title: Re: A Question about Java....
Post by: Turey on January 09, 2008, 12:15:50 am
I really only use "this" in equals or compareTo methods, mostly because I name the other variable "that".  :P

I do type it a lot though, because typing "this." into Eclipse gives me a listing of all of the methods and fields of "this", sorted by return type (if you're in a spot where it's wanting an int, it'll put the int ones first, then everything else). I make my selection, then erase the "this.".
Title: Re: A Question about Java....
Post by: WMCoolmon on January 09, 2008, 12:42:51 am
I use "this" to make references to member functions more explicit. I also use it for variables if I'm dealing with variables for the current class instance and another class instance and they're of the same time, just to make it clearer. It does get highlighted as a keyword, so it can make it a lot easier when scanning text to figure out what's going on.

As for practical uses, there are sometimes instances where you want to pass a reference to the current class to another method. For example, in Maja, I have MajaSource and MajaEntry classes. MajaSource handles actual files on disk, while MajaEntry handles virtual entries in the current VP project. In order to actually write a VP, MajaEntry calls the MajaSource extract function and passes along the reference to the current MajaEntry instance. MajaSource uses the data from that handle to extract the data from the original file.

It's not probably not ideal object-oriented programming, but it's more efficient than passing along 6+ fields and having to update all the places that the function is used when I want to add or reorder fields.
Title: Re: A Question about Java....
Post by: Flipside on January 09, 2008, 08:45:25 am
That actually makes the purpose a bit clearer, I can see where something like that might come in useful with Polymorphism etc, where you are working with several 'layers' to the data structure, I'll admit, but the way it's being taught at Uni is as though it's for people who want to name Method variables the same as their field variables for that class, which just goes against everything I've ever learned about computer coding.

Edit: Must admit, I can see the usefullness of Java, but I don't see it becoming a favourite with me, I can understand why people avoid the monolithic approach, but Java sometimes takes things from the sublime to the ridiculous. Arraylists annoy me for a start, you have to tell the computer that there is an Arraylist, and then tell it that it is empty. I can understand why it's done, but that whole 2-stage system has caught me out on several occasions with those damn 'Null Pointer' messages.
Title: Re: A Question about Java....
Post by: karajorma on January 09, 2008, 08:56:45 am
I find them a lot better than the C++ method (Crash to desktop with only a memory address as a clue to what went wrong.) :D
Title: Re: A Question about Java....
Post by: Flipside on January 09, 2008, 08:59:02 am
:lol: I haven't taken on C++ yet, and it looks like we'll be doing C# later this year instead. I'm not too bothered, it depends just how effective the memory management is on C#, some reports say it's fast enough to code arcade games on, others say it drags the language down terribly, so I expect it's a matter of context...
Title: Re: A Question about Java....
Post by: SadisticSid on January 09, 2008, 01:44:08 pm
Edit: Must admit, I can see the usefullness of Java, but I don't see it becoming a favourite with me, I can understand why people avoid the monolithic approach, but Java sometimes takes things from the sublime to the ridiculous. Arraylists annoy me for a start, you have to tell the computer that there is an Arraylist, and then tell it that it is empty. I can understand why it's done, but that whole 2-stage system has caught me out on several occasions with those damn 'Null Pointer' messages.

It's not just Java that uses this and explicit scoping is a good habit to get into when your programs start to become huge. Also, you're the first person I've heard to complain about the INconvenience of ArrayLists. :p
Title: Re: A Question about Java....
Post by: karajorma on January 09, 2008, 02:22:11 pm
Yeah. I'm a little puzzled by that one too. What's causing you problems Flip?
Title: Re: A Question about Java....
Post by: Flipside on January 09, 2008, 03:52:56 pm
Hehe, they aren't inconvenient, it's more about the fact that I'd just spent ages looking for why I was getting a Null Pointer on an ArrayList I'd defined, and it turned out I'd declared it, but not defined it as a new ArrayList, my own fault in general, but bloody annoying way to waste 3 hours.
Title: Re: A Question about Java....
Post by: karajorma on January 09, 2008, 04:17:14 pm
:D

One reason I get so annoyed looking at the FS2 code is the fact that it is chock full of declared but undefined variables at the start of every function. That's breaking at least three cardinal rules of good programming style right there.
Title: Re: A Question about Java....
Post by: Flipside on January 09, 2008, 04:28:27 pm
Ewwww.. Not good, I always remember to define ints, doubles etc, I just have to get used to remembering that 'ArrayList' is NOT an Array in the 'old' sense.

To be honest, I can't believe how ridiculously busy I am with University work, the second week in January requires 4 complete assignments to be completed that were handed out at the end of September, Java being one of them, I hate feeling like I'm rushing.
Title: Re: A Question about Java....
Post by: WMCoolmon on January 09, 2008, 04:53:26 pm
How large are those assignments?
Title: Re: A Question about Java....
Post by: karajorma on January 09, 2008, 04:58:55 pm
Ewwww.. Not good, I always remember to define ints, doubles etc, I just have to get used to remembering that 'ArrayList' is NOT an Array in the 'old' sense.

What's really funny is that the very name ArrayList is bad OO - revealing implementation details in the name instead of referring to what the object is. :D Suppose in Java 1.7 they decided to change it so that it wasn't backed by an array internally? :D
Title: Re: A Question about Java....
Post by: Flipside on January 09, 2008, 05:02:12 pm
How large are those assignments?

They aren't massive ones, mostly Statistics, Research and Investigation, Multimedia and Programming, but to be honest, sometimes fitting something like Statistics for smoking in under 1000 words is harder than not having a word limit.

The one that is killing me is the Work-Based project, I'm having to relearn ASP, Javascript etc in order to do it, and on top of anything else, I seem to have no time for anything else these days.

Ewwww.. Not good, I always remember to define ints, doubles etc, I just have to get used to remembering that 'ArrayList' is NOT an Array in the 'old' sense.

What's really funny is that the very name ArrayList is bad OO - revealing implementation details in the name instead of referring to what the object is. :D Suppose in Java 1.7 they decided to change it so that it wasn't backed by an array internally? :D

Agreed, I keep considering them as 'Arrays', which are not initialised, merely declared usually (can you imagine looping through all the arrays at the start of a program and setting them all to '0'?), that's probably where the error creeps in.