Jump to content
  • Advertisement
Sign in to follow this  
bgilb

[java] java programming habit

This topic is 4812 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
That's not necessarily a bad thing, but not using way too many nested classes seems to make the most sense.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
I really hate Sun's insistence on not putting opening curly braces on its own line.

That's always bothered me for some reason.

Share this post


Link to post
Share on other sites
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:)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
(K&R braces versus Allman braces) = dumb. It is absolutely trivial to conceptually switch between the two styles.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!