[java] java programming habit

Started by
16 comments, last by tebriel 18 years, 9 months ago
Im getting into a habit. I have subclasses and, whenever I want to access something from the main class, I always make long message calls such as: vanMain.myGame.randomInteger=5; Is there any other way around this? I fear my programs are using lots of bad practices that not only make code hard to read, but also make the program slower.
Advertisement
That's not necessarily a bad thing, but not using way too many nested classes seems to make the most sense.
Not directly related to your problem, but a book has been written by four developers (known as the 'Gang of Four'), where 21 design patterns where discussed. These patterns are elegant solutions for many issues, and you should take a look.

Also, Sun has code conventions for the Java programming language.

Son Of Cain
a.k.a javabeats at yahoo.ca
I really hate Sun's insistence on not putting opening curly braces on its own line.

That's always bothered me for some reason.
What your descibing is known as the java train or something like that, where a field is accessed by drilling down through layers of classes. look into the facade design pattern which basically is a class that acts as an interface to a group of sub classes.

so instead of calling

myClass.someClass.anotherClass.field

you create a top level class with methods that handles all the drilling down to the property you want.


so now you just call:

myClass.accessField();

and the access field method accesses the field and does whatever you want with it
An example of a facade pattern would be a class that manages a database, opening closing adding removing. one class would provide an interface to all this fuctionallity but in practically the functionality is spread over a number of hidden subclasses

hope that helps:)
Quote:Original post by Strife
I really hate Sun's insistence on not putting opening curly braces on its own line.

That's always bothered me for some reason.


I also hate that, and I do not follow this. I always put the opening curly braces in the same line of the end of the method signature, or class declaration, etc...

And I didn't knew it was on the convention to use these braces in a new line. I never did it, and I don't plan to change the way I do it ;)

Son Of Cain

a.k.a javabeats at yahoo.ca
Been using the both styles for years, and I can say the Sun's convention wins anytime. Easier to type, saves space and looks nicer when you get used to it. Especially in single-line methods.
(K&R braces versus Allman braces) = dumb. It is absolutely trivial to conceptually switch between the two styles.
Quote:Original post by Sneftel
(K&R braces versus Allman braces) = dumb. It is absolutely trivial to conceptually switch between the two styles.


Agreed =D. Pick the one who best suits you, and go for it.
a.k.a javabeats at yahoo.ca
I hope this is what you uys are talking about.

if (x>9){
.....some code
}

compared to

if (x>9)
{
.....some code
}

Personally I like the second way better you can see where the brackets start and end much easier because they line up it is especially good if you have lots of nesting going on.

This topic is closed to new replies.

Advertisement