• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question(s) about standard/acceptable code organizing/formatting  RSS feed

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm fairly new to Java (taken one class) and am writing some code on my own for fun/experience while I wait for next semester to start.

Something that keeps coming up is I'm not sure what the best way or organize and or format my code. I guess what I'm asking is what the conventional ways to write my code are. I'll give some example questions:

(abridged version of code..it is an RPG-type of thing)




Here is a switch statement I am using. I thought it looked more visually appealing in sort of a block format like it currently is. Is this acceptable?



So, where should I be placing the aforementioned variables? Is it even 'OK' to have variables floating around like that outside of methods?

Also, in the displayHpCheck method should I be using brackets with my if statements since there is only one line of code?

Is there anything else with my code formatting that looks wrong/unconventional? I figure I'm a beginner so I should get stuff like readability down now.

Sorry if I goofed up any of the proper wording.

Thanks for any suggestions.
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The formatting and naming conventions that you use look fine, and look like the de-facto standard code conventions.

I wouldn't call a class Character, because there's already a standard class named java.lang.Character, which could be confused with your own class.

you should only use member variables to store the state of your object. Don't create a member variable just because you want to use the same type of variable in multiple methods; for example 'scan' (line 10) should not be a member variable. Also, always make member variables 'private' unless there's a good reason not to. Note that 'description' and 'scan' have no access modifier, which means they're visible inside all classes that are in the same package as class Character.

Member variables should conventionally be declared at the top of the class.

A book tip: Effective Java is a great book to learn more about Java once you're past the absolute beginner stage.
 
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A sidenote: I would prefer this


instead of this:

 
Hans Hovan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestions.

Jesper: Is an instance variable the same thing as a member variable?

You mention not to create a member variable just because I want to use the same type of variable in multiple methods, so does this mean each I should instantiate a scan object separately within each method that needs it?

And then something like the ArrayList 'ArrayList<String> scrolls = new ArrayList<String>();' should have an access modifier (encapsulation) and be placed at the top of the class?

Thanks for the book tip. I will check it out.

Ivan: Thanks, I didn't know you could do that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!