Text based game in Java
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
To help with learning code in my class, I've been working on this text based game to keep myself coding (almost) every day. I have a class called BasicUnit
, and in it I have methods to create a custom class. I use 2 methods for this, allowing the user to enter the information for the class. I'm just wondering if I can do this in a more simplified manner?
public void buildCustomClass(int maxHP, int maxMP, int maxSP, int baseMeleeDmg, int baseSpellDmg, int baseAC, int baseSpeed)
this.maxHP = maxHP;
this.maxMP = maxMP;
this.maxSP = maxSP;
this.baseMeleeDmg = baseMeleeDmg;
this.baseSpellDmg = baseSpellDmg;
this.baseAC = baseAC;
this.baseSpeed = baseSpeed;
lvl = 1;
xp = 0;
curHP = maxHP;
curMP = maxMP;
curSP = maxSP;
public void createCustomClass()
kb = new Scanner(System.in);
System.out.println("Enter the information for your class: ");
System.out.println("Enter HP: ");
maxHP = kb.nextInt();
System.out.println("Enter MP: ");
maxMP = kb.nextInt();
System.out.println("Enter SP: ");
maxSP = kb.nextInt();
System.out.println("Enter Base Melee Damage: ");
baseMeleeDmg = kb.nextInt();
System.out.println("Enter Base Spell Damage: ");
baseSpellDmg = kb.nextInt();
System.out.println("Enter AC: ");
baseAC = kb.nextInt();
System.out.println("Enter Speed: ");
baseSpeed = kb.nextInt();
buildCustomClass(maxHP, maxMP, maxSP, baseMeleeDmg, baseSpellDmg, baseAC, baseSpeed);
java beginner object-oriented battle-simulation
add a comment |Â
up vote
1
down vote
favorite
To help with learning code in my class, I've been working on this text based game to keep myself coding (almost) every day. I have a class called BasicUnit
, and in it I have methods to create a custom class. I use 2 methods for this, allowing the user to enter the information for the class. I'm just wondering if I can do this in a more simplified manner?
public void buildCustomClass(int maxHP, int maxMP, int maxSP, int baseMeleeDmg, int baseSpellDmg, int baseAC, int baseSpeed)
this.maxHP = maxHP;
this.maxMP = maxMP;
this.maxSP = maxSP;
this.baseMeleeDmg = baseMeleeDmg;
this.baseSpellDmg = baseSpellDmg;
this.baseAC = baseAC;
this.baseSpeed = baseSpeed;
lvl = 1;
xp = 0;
curHP = maxHP;
curMP = maxMP;
curSP = maxSP;
public void createCustomClass()
kb = new Scanner(System.in);
System.out.println("Enter the information for your class: ");
System.out.println("Enter HP: ");
maxHP = kb.nextInt();
System.out.println("Enter MP: ");
maxMP = kb.nextInt();
System.out.println("Enter SP: ");
maxSP = kb.nextInt();
System.out.println("Enter Base Melee Damage: ");
baseMeleeDmg = kb.nextInt();
System.out.println("Enter Base Spell Damage: ");
baseSpellDmg = kb.nextInt();
System.out.println("Enter AC: ");
baseAC = kb.nextInt();
System.out.println("Enter Speed: ");
baseSpeed = kb.nextInt();
buildCustomClass(maxHP, maxMP, maxSP, baseMeleeDmg, baseSpellDmg, baseAC, baseSpeed);
java beginner object-oriented battle-simulation
3
Could you include the actual class this is in? These 2 methods are used to initialise a certain class but you're not really creating one. Without the surrounding code it's hard to explain the difference.
â Imus
Apr 5 at 8:32
This looks like it could be a pretty good spot for the Builder Pattern. But, I would need to see more code to make a recommendation.
â adpro
Apr 5 at 12:44
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
To help with learning code in my class, I've been working on this text based game to keep myself coding (almost) every day. I have a class called BasicUnit
, and in it I have methods to create a custom class. I use 2 methods for this, allowing the user to enter the information for the class. I'm just wondering if I can do this in a more simplified manner?
public void buildCustomClass(int maxHP, int maxMP, int maxSP, int baseMeleeDmg, int baseSpellDmg, int baseAC, int baseSpeed)
this.maxHP = maxHP;
this.maxMP = maxMP;
this.maxSP = maxSP;
this.baseMeleeDmg = baseMeleeDmg;
this.baseSpellDmg = baseSpellDmg;
this.baseAC = baseAC;
this.baseSpeed = baseSpeed;
lvl = 1;
xp = 0;
curHP = maxHP;
curMP = maxMP;
curSP = maxSP;
public void createCustomClass()
kb = new Scanner(System.in);
System.out.println("Enter the information for your class: ");
System.out.println("Enter HP: ");
maxHP = kb.nextInt();
System.out.println("Enter MP: ");
maxMP = kb.nextInt();
System.out.println("Enter SP: ");
maxSP = kb.nextInt();
System.out.println("Enter Base Melee Damage: ");
baseMeleeDmg = kb.nextInt();
System.out.println("Enter Base Spell Damage: ");
baseSpellDmg = kb.nextInt();
System.out.println("Enter AC: ");
baseAC = kb.nextInt();
System.out.println("Enter Speed: ");
baseSpeed = kb.nextInt();
buildCustomClass(maxHP, maxMP, maxSP, baseMeleeDmg, baseSpellDmg, baseAC, baseSpeed);
java beginner object-oriented battle-simulation
To help with learning code in my class, I've been working on this text based game to keep myself coding (almost) every day. I have a class called BasicUnit
, and in it I have methods to create a custom class. I use 2 methods for this, allowing the user to enter the information for the class. I'm just wondering if I can do this in a more simplified manner?
public void buildCustomClass(int maxHP, int maxMP, int maxSP, int baseMeleeDmg, int baseSpellDmg, int baseAC, int baseSpeed)
this.maxHP = maxHP;
this.maxMP = maxMP;
this.maxSP = maxSP;
this.baseMeleeDmg = baseMeleeDmg;
this.baseSpellDmg = baseSpellDmg;
this.baseAC = baseAC;
this.baseSpeed = baseSpeed;
lvl = 1;
xp = 0;
curHP = maxHP;
curMP = maxMP;
curSP = maxSP;
public void createCustomClass()
kb = new Scanner(System.in);
System.out.println("Enter the information for your class: ");
System.out.println("Enter HP: ");
maxHP = kb.nextInt();
System.out.println("Enter MP: ");
maxMP = kb.nextInt();
System.out.println("Enter SP: ");
maxSP = kb.nextInt();
System.out.println("Enter Base Melee Damage: ");
baseMeleeDmg = kb.nextInt();
System.out.println("Enter Base Spell Damage: ");
baseSpellDmg = kb.nextInt();
System.out.println("Enter AC: ");
baseAC = kb.nextInt();
System.out.println("Enter Speed: ");
baseSpeed = kb.nextInt();
buildCustomClass(maxHP, maxMP, maxSP, baseMeleeDmg, baseSpellDmg, baseAC, baseSpeed);
java beginner object-oriented battle-simulation
edited Apr 12 at 20:58
200_success
123k14142399
123k14142399
asked Apr 5 at 0:25
Brandon
91
91
3
Could you include the actual class this is in? These 2 methods are used to initialise a certain class but you're not really creating one. Without the surrounding code it's hard to explain the difference.
â Imus
Apr 5 at 8:32
This looks like it could be a pretty good spot for the Builder Pattern. But, I would need to see more code to make a recommendation.
â adpro
Apr 5 at 12:44
add a comment |Â
3
Could you include the actual class this is in? These 2 methods are used to initialise a certain class but you're not really creating one. Without the surrounding code it's hard to explain the difference.
â Imus
Apr 5 at 8:32
This looks like it could be a pretty good spot for the Builder Pattern. But, I would need to see more code to make a recommendation.
â adpro
Apr 5 at 12:44
3
3
Could you include the actual class this is in? These 2 methods are used to initialise a certain class but you're not really creating one. Without the surrounding code it's hard to explain the difference.
â Imus
Apr 5 at 8:32
Could you include the actual class this is in? These 2 methods are used to initialise a certain class but you're not really creating one. Without the surrounding code it's hard to explain the difference.
â Imus
Apr 5 at 8:32
This looks like it could be a pretty good spot for the Builder Pattern. But, I would need to see more code to make a recommendation.
â adpro
Apr 5 at 12:44
This looks like it could be a pretty good spot for the Builder Pattern. But, I would need to see more code to make a recommendation.
â adpro
Apr 5 at 12:44
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
5
down vote
Welcome to Code Review and thanks for sharing your code!
General issues
Naming
Finding good names is the hardest part in programming. So always take your time to think carefully of your identifier names.
Naming Conventions
It looks like you already know the
Java Naming Conventions.
Avoid abbreviations
In your code you use some abbreviations such as maxSP
and baseMeleeDmg
.
Although this abbreviation makes sense to you (now) anyone reading your code being not familiar with the problem (like me) has a hard time finding out what this means.
If you do this to save typing work: remember that you way more often read your code than actually typing something. Also for Java you have good IDE support with code completion so that you most likely type a long identifier only once and later on select it from the IDEs code completion proposals.
The other identifiers make perfect sense to me. [because of context] â AJD
Context looks like your friend but in fact it is your enemy.
There are two reasons:
Context depends on knowlegde and experience. But different persons have different knowlegde and experience, so for one it might be easy to remember the context and for another it might be hard. Also your own knowledge and experiences change over time so you might find it hard to remember the context of any given code snipped you wrote when you come back to it in 3 in years or even 3 moth.
The point is that in any case you need (more or less) time to bring the context back to you brain.
But the only thing that you usually don't have when you do programming as a business ist time. So anything that makes you faster in understanding the code is a money worth benefit. And not needing to remember any context is such a time saver.You may argue that we have a very simple promblem with an easy and common context. Thats true.
But:Real life projects usually have higher complexity and less eays to remember contexts. The point here is:
At which point is your context so complext that you switch from "acronym naming" to "verbose nameing"?Again this point changes with you knowledge and your experience wich may lead to code that others have a hard time to understand.
The much better way to deal with it is to always white your code in a way that the dumbest person you know may be able to understand it. And this includes not to use akronyms in your identifiers that might need context to understand.
This is a training project. When you train a physical skill like Highjumping you start with a very low bar that you can easily pass even not using the flop technique just to have a safe environment.
Same is here: The problem may be simple enough to be understood having the acronymed identifiers, but for the sake of training you should avoid acronyms.
Avoid misleading naming
Both of your method names are misleading: They claim to create and build something but in reality neither one is creating or building anything.
One method is doing user interaction and the other is configuring the object.
The names of the methods should reflect that.
Add units to identifiers for physical values
Physical values do mean nothing without a unit. This is a special case of the context problem mentioned above. The most famous example is the fail of the two space missions Mars Climate Orbiter and Mars Polar Lander. The Flight control software was build by NASA and worked with metric measurement (i.e. meters
, meters per second
or newton seconds
) while the engine (and their driver software) has been build by Lockheed Martin which use imperial units (i.e. feet
, feet per minute
or Pound-force second
).
The point is: not having units of physical values in your identifiers forces you to think if there is a problem or not:
double acceleration = flightManagement.calculateAcceleration();
engine.accelerate(acceleration);
But usually you don't quesion it unless you have a reason...
Having the units in the identifier names the problem becomes obvious:
double meterPerSquareSeconds =
flightManagement.calculateAccelerationInMeterPerSquareSecond();
engine.accelerateByFeedPerSquareMinute(
meterPerSquareSeconds); // oops
And we have the same argument again: your particular code is so easy and so small that we don't need the overhead.
But then: How do you decide at which point the overhead is needed? And again it depends on knowledge and experience which still are different among people...
Flawed implementation
Both methods using the same member variables.
E.g. you variable maxSP
: in createCustomClass()
you assign it the result of kb.nextInt()
. Then you pass this value as parameter to buildCustomClass()
where you again assign the parameter value again to the same member variable.
Beside this being useless it may lead to confusing bugs later.
keep same level of abstraction
Methods should either do "primitive" opeerations or call other methods, not both at the same time.
At the end of your method createCustomClass()
you call the other method (buildCustomClass()
). The better way to do this is to extract the code before the call to buildCustomClass()
in a separate (private) method:
public void createCustomClass()
aquireDataFromUser();
buildCustomClass(
maxHP,
maxMP,
maxSP,
baseMeleeDmg,
baseSpellDmg,
baseAC,
baseSpeed);
private void aquireDataFromUser()
kb = new Scanner(System.in);
System.out.println("Enter the information for your class: ");
System.out.println("Enter HP: ");
maxHP = kb.nextInt();
System.out.println("Enter MP: ");
maxMP = kb.nextInt();
System.out.println("Enter SP: ");
maxSP = kb.nextInt();
System.out.println("Enter Base Melee Damage: ");
baseMeleeDmg = kb.nextInt();
System.out.println("Enter Base Spell Damage: ");
baseSpellDmg = kb.nextInt();
System.out.println("Enter AC: ");
baseAC = kb.nextInt();
System.out.println("Enter Speed: ");
baseSpeed = kb.nextInt();
Beside making createCustomClass()
shorter it makes the useless reassingment of the member variables obvious.
1
@AJD "The other identifiers make perfect sense to me." - If this is your start into a programmers career keep in mind that you will have to collaborate with others. And the identifiers you choose must make "perfect sense" to your futur team mates too. Also: you asked for improvements. Accept them or not. There is no need to explain yourself...
â Timothy Truckle
Apr 6 at 7:49
1
@AJD So your argument is that cryptic identifier names are OK because of context? Well my experience in the programming business is only 20 years. But I for myself find it easier to work with clear names without acronyms even if they are somewhat lengthly. And please keep in mind: modern languages (like java or even C) have virtually no limit for the length of identifiers, and actual monitors can show more than 80x25 characters, Some progress happened here in the last 40 years... ;o)
â Timothy Truckle
Apr 6 at 8:07
1
@AJD "his is common parlance in that context" And BTW this was the reason why two mars missions failed (Mars Climate Orbiter and Mars Polar Lander): some teams contributing software had their own context (metric vs imperial units). So yes: I think that identifiers should not have any acronyms and only short living local variables may be very common abbreviations.
â Timothy Truckle
Apr 6 at 8:28
1
I think the question to ask is "why?" Why would you use abbreviated names? The answer used to be simple. To save space and typing. But, with better monitors and autocomplete, those reasons have mostly gone by the way side. So, it is always good for a developer to be descriptive when naming things in these modern languages, whether or not the context is understood or will only be used by specific people that understand it. It's always good for a developer to code using descriptive naming; building good habits. There are exceptions, like JavaScript (size of files), but that doesn't apply here.
â adpro
Apr 6 at 11:20
1
@AJD, you're saying the same stuff you said before. It's also about building good habits and making sure the code is maximally readable by all developers. Abbreviating variable names is frowned upon in order to create good habits. It absolutely should bemaxHitPoints
. This developer is asking for a code review. Someone that knows the language may not understand the context and not understand whatmaxHP
means in this very question. So, the abbreviation would create confusion when asking for code review on a site like this. This question is an example of why descriptive naming is important.
â adpro
Apr 9 at 15:32
 |Â
show 13 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
Welcome to Code Review and thanks for sharing your code!
General issues
Naming
Finding good names is the hardest part in programming. So always take your time to think carefully of your identifier names.
Naming Conventions
It looks like you already know the
Java Naming Conventions.
Avoid abbreviations
In your code you use some abbreviations such as maxSP
and baseMeleeDmg
.
Although this abbreviation makes sense to you (now) anyone reading your code being not familiar with the problem (like me) has a hard time finding out what this means.
If you do this to save typing work: remember that you way more often read your code than actually typing something. Also for Java you have good IDE support with code completion so that you most likely type a long identifier only once and later on select it from the IDEs code completion proposals.
The other identifiers make perfect sense to me. [because of context] â AJD
Context looks like your friend but in fact it is your enemy.
There are two reasons:
Context depends on knowlegde and experience. But different persons have different knowlegde and experience, so for one it might be easy to remember the context and for another it might be hard. Also your own knowledge and experiences change over time so you might find it hard to remember the context of any given code snipped you wrote when you come back to it in 3 in years or even 3 moth.
The point is that in any case you need (more or less) time to bring the context back to you brain.
But the only thing that you usually don't have when you do programming as a business ist time. So anything that makes you faster in understanding the code is a money worth benefit. And not needing to remember any context is such a time saver.You may argue that we have a very simple promblem with an easy and common context. Thats true.
But:Real life projects usually have higher complexity and less eays to remember contexts. The point here is:
At which point is your context so complext that you switch from "acronym naming" to "verbose nameing"?Again this point changes with you knowledge and your experience wich may lead to code that others have a hard time to understand.
The much better way to deal with it is to always white your code in a way that the dumbest person you know may be able to understand it. And this includes not to use akronyms in your identifiers that might need context to understand.
This is a training project. When you train a physical skill like Highjumping you start with a very low bar that you can easily pass even not using the flop technique just to have a safe environment.
Same is here: The problem may be simple enough to be understood having the acronymed identifiers, but for the sake of training you should avoid acronyms.
Avoid misleading naming
Both of your method names are misleading: They claim to create and build something but in reality neither one is creating or building anything.
One method is doing user interaction and the other is configuring the object.
The names of the methods should reflect that.
Add units to identifiers for physical values
Physical values do mean nothing without a unit. This is a special case of the context problem mentioned above. The most famous example is the fail of the two space missions Mars Climate Orbiter and Mars Polar Lander. The Flight control software was build by NASA and worked with metric measurement (i.e. meters
, meters per second
or newton seconds
) while the engine (and their driver software) has been build by Lockheed Martin which use imperial units (i.e. feet
, feet per minute
or Pound-force second
).
The point is: not having units of physical values in your identifiers forces you to think if there is a problem or not:
double acceleration = flightManagement.calculateAcceleration();
engine.accelerate(acceleration);
But usually you don't quesion it unless you have a reason...
Having the units in the identifier names the problem becomes obvious:
double meterPerSquareSeconds =
flightManagement.calculateAccelerationInMeterPerSquareSecond();
engine.accelerateByFeedPerSquareMinute(
meterPerSquareSeconds); // oops
And we have the same argument again: your particular code is so easy and so small that we don't need the overhead.
But then: How do you decide at which point the overhead is needed? And again it depends on knowledge and experience which still are different among people...
Flawed implementation
Both methods using the same member variables.
E.g. you variable maxSP
: in createCustomClass()
you assign it the result of kb.nextInt()
. Then you pass this value as parameter to buildCustomClass()
where you again assign the parameter value again to the same member variable.
Beside this being useless it may lead to confusing bugs later.
keep same level of abstraction
Methods should either do "primitive" opeerations or call other methods, not both at the same time.
At the end of your method createCustomClass()
you call the other method (buildCustomClass()
). The better way to do this is to extract the code before the call to buildCustomClass()
in a separate (private) method:
public void createCustomClass()
aquireDataFromUser();
buildCustomClass(
maxHP,
maxMP,
maxSP,
baseMeleeDmg,
baseSpellDmg,
baseAC,
baseSpeed);
private void aquireDataFromUser()
kb = new Scanner(System.in);
System.out.println("Enter the information for your class: ");
System.out.println("Enter HP: ");
maxHP = kb.nextInt();
System.out.println("Enter MP: ");
maxMP = kb.nextInt();
System.out.println("Enter SP: ");
maxSP = kb.nextInt();
System.out.println("Enter Base Melee Damage: ");
baseMeleeDmg = kb.nextInt();
System.out.println("Enter Base Spell Damage: ");
baseSpellDmg = kb.nextInt();
System.out.println("Enter AC: ");
baseAC = kb.nextInt();
System.out.println("Enter Speed: ");
baseSpeed = kb.nextInt();
Beside making createCustomClass()
shorter it makes the useless reassingment of the member variables obvious.
1
@AJD "The other identifiers make perfect sense to me." - If this is your start into a programmers career keep in mind that you will have to collaborate with others. And the identifiers you choose must make "perfect sense" to your futur team mates too. Also: you asked for improvements. Accept them or not. There is no need to explain yourself...
â Timothy Truckle
Apr 6 at 7:49
1
@AJD So your argument is that cryptic identifier names are OK because of context? Well my experience in the programming business is only 20 years. But I for myself find it easier to work with clear names without acronyms even if they are somewhat lengthly. And please keep in mind: modern languages (like java or even C) have virtually no limit for the length of identifiers, and actual monitors can show more than 80x25 characters, Some progress happened here in the last 40 years... ;o)
â Timothy Truckle
Apr 6 at 8:07
1
@AJD "his is common parlance in that context" And BTW this was the reason why two mars missions failed (Mars Climate Orbiter and Mars Polar Lander): some teams contributing software had their own context (metric vs imperial units). So yes: I think that identifiers should not have any acronyms and only short living local variables may be very common abbreviations.
â Timothy Truckle
Apr 6 at 8:28
1
I think the question to ask is "why?" Why would you use abbreviated names? The answer used to be simple. To save space and typing. But, with better monitors and autocomplete, those reasons have mostly gone by the way side. So, it is always good for a developer to be descriptive when naming things in these modern languages, whether or not the context is understood or will only be used by specific people that understand it. It's always good for a developer to code using descriptive naming; building good habits. There are exceptions, like JavaScript (size of files), but that doesn't apply here.
â adpro
Apr 6 at 11:20
1
@AJD, you're saying the same stuff you said before. It's also about building good habits and making sure the code is maximally readable by all developers. Abbreviating variable names is frowned upon in order to create good habits. It absolutely should bemaxHitPoints
. This developer is asking for a code review. Someone that knows the language may not understand the context and not understand whatmaxHP
means in this very question. So, the abbreviation would create confusion when asking for code review on a site like this. This question is an example of why descriptive naming is important.
â adpro
Apr 9 at 15:32
 |Â
show 13 more comments
up vote
5
down vote
Welcome to Code Review and thanks for sharing your code!
General issues
Naming
Finding good names is the hardest part in programming. So always take your time to think carefully of your identifier names.
Naming Conventions
It looks like you already know the
Java Naming Conventions.
Avoid abbreviations
In your code you use some abbreviations such as maxSP
and baseMeleeDmg
.
Although this abbreviation makes sense to you (now) anyone reading your code being not familiar with the problem (like me) has a hard time finding out what this means.
If you do this to save typing work: remember that you way more often read your code than actually typing something. Also for Java you have good IDE support with code completion so that you most likely type a long identifier only once and later on select it from the IDEs code completion proposals.
The other identifiers make perfect sense to me. [because of context] â AJD
Context looks like your friend but in fact it is your enemy.
There are two reasons:
Context depends on knowlegde and experience. But different persons have different knowlegde and experience, so for one it might be easy to remember the context and for another it might be hard. Also your own knowledge and experiences change over time so you might find it hard to remember the context of any given code snipped you wrote when you come back to it in 3 in years or even 3 moth.
The point is that in any case you need (more or less) time to bring the context back to you brain.
But the only thing that you usually don't have when you do programming as a business ist time. So anything that makes you faster in understanding the code is a money worth benefit. And not needing to remember any context is such a time saver.You may argue that we have a very simple promblem with an easy and common context. Thats true.
But:Real life projects usually have higher complexity and less eays to remember contexts. The point here is:
At which point is your context so complext that you switch from "acronym naming" to "verbose nameing"?Again this point changes with you knowledge and your experience wich may lead to code that others have a hard time to understand.
The much better way to deal with it is to always white your code in a way that the dumbest person you know may be able to understand it. And this includes not to use akronyms in your identifiers that might need context to understand.
This is a training project. When you train a physical skill like Highjumping you start with a very low bar that you can easily pass even not using the flop technique just to have a safe environment.
Same is here: The problem may be simple enough to be understood having the acronymed identifiers, but for the sake of training you should avoid acronyms.
Avoid misleading naming
Both of your method names are misleading: They claim to create and build something but in reality neither one is creating or building anything.
One method is doing user interaction and the other is configuring the object.
The names of the methods should reflect that.
Add units to identifiers for physical values
Physical values do mean nothing without a unit. This is a special case of the context problem mentioned above. The most famous example is the fail of the two space missions Mars Climate Orbiter and Mars Polar Lander. The Flight control software was build by NASA and worked with metric measurement (i.e. meters
, meters per second
or newton seconds
) while the engine (and their driver software) has been build by Lockheed Martin which use imperial units (i.e. feet
, feet per minute
or Pound-force second
).
The point is: not having units of physical values in your identifiers forces you to think if there is a problem or not:
double acceleration = flightManagement.calculateAcceleration();
engine.accelerate(acceleration);
But usually you don't quesion it unless you have a reason...
Having the units in the identifier names the problem becomes obvious:
double meterPerSquareSeconds =
flightManagement.calculateAccelerationInMeterPerSquareSecond();
engine.accelerateByFeedPerSquareMinute(
meterPerSquareSeconds); // oops
And we have the same argument again: your particular code is so easy and so small that we don't need the overhead.
But then: How do you decide at which point the overhead is needed? And again it depends on knowledge and experience which still are different among people...
Flawed implementation
Both methods using the same member variables.
E.g. you variable maxSP
: in createCustomClass()
you assign it the result of kb.nextInt()
. Then you pass this value as parameter to buildCustomClass()
where you again assign the parameter value again to the same member variable.
Beside this being useless it may lead to confusing bugs later.
keep same level of abstraction
Methods should either do "primitive" opeerations or call other methods, not both at the same time.
At the end of your method createCustomClass()
you call the other method (buildCustomClass()
). The better way to do this is to extract the code before the call to buildCustomClass()
in a separate (private) method:
public void createCustomClass()
aquireDataFromUser();
buildCustomClass(
maxHP,
maxMP,
maxSP,
baseMeleeDmg,
baseSpellDmg,
baseAC,
baseSpeed);
private void aquireDataFromUser()
kb = new Scanner(System.in);
System.out.println("Enter the information for your class: ");
System.out.println("Enter HP: ");
maxHP = kb.nextInt();
System.out.println("Enter MP: ");
maxMP = kb.nextInt();
System.out.println("Enter SP: ");
maxSP = kb.nextInt();
System.out.println("Enter Base Melee Damage: ");
baseMeleeDmg = kb.nextInt();
System.out.println("Enter Base Spell Damage: ");
baseSpellDmg = kb.nextInt();
System.out.println("Enter AC: ");
baseAC = kb.nextInt();
System.out.println("Enter Speed: ");
baseSpeed = kb.nextInt();
Beside making createCustomClass()
shorter it makes the useless reassingment of the member variables obvious.
1
@AJD "The other identifiers make perfect sense to me." - If this is your start into a programmers career keep in mind that you will have to collaborate with others. And the identifiers you choose must make "perfect sense" to your futur team mates too. Also: you asked for improvements. Accept them or not. There is no need to explain yourself...
â Timothy Truckle
Apr 6 at 7:49
1
@AJD So your argument is that cryptic identifier names are OK because of context? Well my experience in the programming business is only 20 years. But I for myself find it easier to work with clear names without acronyms even if they are somewhat lengthly. And please keep in mind: modern languages (like java or even C) have virtually no limit for the length of identifiers, and actual monitors can show more than 80x25 characters, Some progress happened here in the last 40 years... ;o)
â Timothy Truckle
Apr 6 at 8:07
1
@AJD "his is common parlance in that context" And BTW this was the reason why two mars missions failed (Mars Climate Orbiter and Mars Polar Lander): some teams contributing software had their own context (metric vs imperial units). So yes: I think that identifiers should not have any acronyms and only short living local variables may be very common abbreviations.
â Timothy Truckle
Apr 6 at 8:28
1
I think the question to ask is "why?" Why would you use abbreviated names? The answer used to be simple. To save space and typing. But, with better monitors and autocomplete, those reasons have mostly gone by the way side. So, it is always good for a developer to be descriptive when naming things in these modern languages, whether or not the context is understood or will only be used by specific people that understand it. It's always good for a developer to code using descriptive naming; building good habits. There are exceptions, like JavaScript (size of files), but that doesn't apply here.
â adpro
Apr 6 at 11:20
1
@AJD, you're saying the same stuff you said before. It's also about building good habits and making sure the code is maximally readable by all developers. Abbreviating variable names is frowned upon in order to create good habits. It absolutely should bemaxHitPoints
. This developer is asking for a code review. Someone that knows the language may not understand the context and not understand whatmaxHP
means in this very question. So, the abbreviation would create confusion when asking for code review on a site like this. This question is an example of why descriptive naming is important.
â adpro
Apr 9 at 15:32
 |Â
show 13 more comments
up vote
5
down vote
up vote
5
down vote
Welcome to Code Review and thanks for sharing your code!
General issues
Naming
Finding good names is the hardest part in programming. So always take your time to think carefully of your identifier names.
Naming Conventions
It looks like you already know the
Java Naming Conventions.
Avoid abbreviations
In your code you use some abbreviations such as maxSP
and baseMeleeDmg
.
Although this abbreviation makes sense to you (now) anyone reading your code being not familiar with the problem (like me) has a hard time finding out what this means.
If you do this to save typing work: remember that you way more often read your code than actually typing something. Also for Java you have good IDE support with code completion so that you most likely type a long identifier only once and later on select it from the IDEs code completion proposals.
The other identifiers make perfect sense to me. [because of context] â AJD
Context looks like your friend but in fact it is your enemy.
There are two reasons:
Context depends on knowlegde and experience. But different persons have different knowlegde and experience, so for one it might be easy to remember the context and for another it might be hard. Also your own knowledge and experiences change over time so you might find it hard to remember the context of any given code snipped you wrote when you come back to it in 3 in years or even 3 moth.
The point is that in any case you need (more or less) time to bring the context back to you brain.
But the only thing that you usually don't have when you do programming as a business ist time. So anything that makes you faster in understanding the code is a money worth benefit. And not needing to remember any context is such a time saver.You may argue that we have a very simple promblem with an easy and common context. Thats true.
But:Real life projects usually have higher complexity and less eays to remember contexts. The point here is:
At which point is your context so complext that you switch from "acronym naming" to "verbose nameing"?Again this point changes with you knowledge and your experience wich may lead to code that others have a hard time to understand.
The much better way to deal with it is to always white your code in a way that the dumbest person you know may be able to understand it. And this includes not to use akronyms in your identifiers that might need context to understand.
This is a training project. When you train a physical skill like Highjumping you start with a very low bar that you can easily pass even not using the flop technique just to have a safe environment.
Same is here: The problem may be simple enough to be understood having the acronymed identifiers, but for the sake of training you should avoid acronyms.
Avoid misleading naming
Both of your method names are misleading: They claim to create and build something but in reality neither one is creating or building anything.
One method is doing user interaction and the other is configuring the object.
The names of the methods should reflect that.
Add units to identifiers for physical values
Physical values do mean nothing without a unit. This is a special case of the context problem mentioned above. The most famous example is the fail of the two space missions Mars Climate Orbiter and Mars Polar Lander. The Flight control software was build by NASA and worked with metric measurement (i.e. meters
, meters per second
or newton seconds
) while the engine (and their driver software) has been build by Lockheed Martin which use imperial units (i.e. feet
, feet per minute
or Pound-force second
).
The point is: not having units of physical values in your identifiers forces you to think if there is a problem or not:
double acceleration = flightManagement.calculateAcceleration();
engine.accelerate(acceleration);
But usually you don't quesion it unless you have a reason...
Having the units in the identifier names the problem becomes obvious:
double meterPerSquareSeconds =
flightManagement.calculateAccelerationInMeterPerSquareSecond();
engine.accelerateByFeedPerSquareMinute(
meterPerSquareSeconds); // oops
And we have the same argument again: your particular code is so easy and so small that we don't need the overhead.
But then: How do you decide at which point the overhead is needed? And again it depends on knowledge and experience which still are different among people...
Flawed implementation
Both methods using the same member variables.
E.g. you variable maxSP
: in createCustomClass()
you assign it the result of kb.nextInt()
. Then you pass this value as parameter to buildCustomClass()
where you again assign the parameter value again to the same member variable.
Beside this being useless it may lead to confusing bugs later.
keep same level of abstraction
Methods should either do "primitive" opeerations or call other methods, not both at the same time.
At the end of your method createCustomClass()
you call the other method (buildCustomClass()
). The better way to do this is to extract the code before the call to buildCustomClass()
in a separate (private) method:
public void createCustomClass()
aquireDataFromUser();
buildCustomClass(
maxHP,
maxMP,
maxSP,
baseMeleeDmg,
baseSpellDmg,
baseAC,
baseSpeed);
private void aquireDataFromUser()
kb = new Scanner(System.in);
System.out.println("Enter the information for your class: ");
System.out.println("Enter HP: ");
maxHP = kb.nextInt();
System.out.println("Enter MP: ");
maxMP = kb.nextInt();
System.out.println("Enter SP: ");
maxSP = kb.nextInt();
System.out.println("Enter Base Melee Damage: ");
baseMeleeDmg = kb.nextInt();
System.out.println("Enter Base Spell Damage: ");
baseSpellDmg = kb.nextInt();
System.out.println("Enter AC: ");
baseAC = kb.nextInt();
System.out.println("Enter Speed: ");
baseSpeed = kb.nextInt();
Beside making createCustomClass()
shorter it makes the useless reassingment of the member variables obvious.
Welcome to Code Review and thanks for sharing your code!
General issues
Naming
Finding good names is the hardest part in programming. So always take your time to think carefully of your identifier names.
Naming Conventions
It looks like you already know the
Java Naming Conventions.
Avoid abbreviations
In your code you use some abbreviations such as maxSP
and baseMeleeDmg
.
Although this abbreviation makes sense to you (now) anyone reading your code being not familiar with the problem (like me) has a hard time finding out what this means.
If you do this to save typing work: remember that you way more often read your code than actually typing something. Also for Java you have good IDE support with code completion so that you most likely type a long identifier only once and later on select it from the IDEs code completion proposals.
The other identifiers make perfect sense to me. [because of context] â AJD
Context looks like your friend but in fact it is your enemy.
There are two reasons:
Context depends on knowlegde and experience. But different persons have different knowlegde and experience, so for one it might be easy to remember the context and for another it might be hard. Also your own knowledge and experiences change over time so you might find it hard to remember the context of any given code snipped you wrote when you come back to it in 3 in years or even 3 moth.
The point is that in any case you need (more or less) time to bring the context back to you brain.
But the only thing that you usually don't have when you do programming as a business ist time. So anything that makes you faster in understanding the code is a money worth benefit. And not needing to remember any context is such a time saver.You may argue that we have a very simple promblem with an easy and common context. Thats true.
But:Real life projects usually have higher complexity and less eays to remember contexts. The point here is:
At which point is your context so complext that you switch from "acronym naming" to "verbose nameing"?Again this point changes with you knowledge and your experience wich may lead to code that others have a hard time to understand.
The much better way to deal with it is to always white your code in a way that the dumbest person you know may be able to understand it. And this includes not to use akronyms in your identifiers that might need context to understand.
This is a training project. When you train a physical skill like Highjumping you start with a very low bar that you can easily pass even not using the flop technique just to have a safe environment.
Same is here: The problem may be simple enough to be understood having the acronymed identifiers, but for the sake of training you should avoid acronyms.
Avoid misleading naming
Both of your method names are misleading: They claim to create and build something but in reality neither one is creating or building anything.
One method is doing user interaction and the other is configuring the object.
The names of the methods should reflect that.
Add units to identifiers for physical values
Physical values do mean nothing without a unit. This is a special case of the context problem mentioned above. The most famous example is the fail of the two space missions Mars Climate Orbiter and Mars Polar Lander. The Flight control software was build by NASA and worked with metric measurement (i.e. meters
, meters per second
or newton seconds
) while the engine (and their driver software) has been build by Lockheed Martin which use imperial units (i.e. feet
, feet per minute
or Pound-force second
).
The point is: not having units of physical values in your identifiers forces you to think if there is a problem or not:
double acceleration = flightManagement.calculateAcceleration();
engine.accelerate(acceleration);
But usually you don't quesion it unless you have a reason...
Having the units in the identifier names the problem becomes obvious:
double meterPerSquareSeconds =
flightManagement.calculateAccelerationInMeterPerSquareSecond();
engine.accelerateByFeedPerSquareMinute(
meterPerSquareSeconds); // oops
And we have the same argument again: your particular code is so easy and so small that we don't need the overhead.
But then: How do you decide at which point the overhead is needed? And again it depends on knowledge and experience which still are different among people...
Flawed implementation
Both methods using the same member variables.
E.g. you variable maxSP
: in createCustomClass()
you assign it the result of kb.nextInt()
. Then you pass this value as parameter to buildCustomClass()
where you again assign the parameter value again to the same member variable.
Beside this being useless it may lead to confusing bugs later.
keep same level of abstraction
Methods should either do "primitive" opeerations or call other methods, not both at the same time.
At the end of your method createCustomClass()
you call the other method (buildCustomClass()
). The better way to do this is to extract the code before the call to buildCustomClass()
in a separate (private) method:
public void createCustomClass()
aquireDataFromUser();
buildCustomClass(
maxHP,
maxMP,
maxSP,
baseMeleeDmg,
baseSpellDmg,
baseAC,
baseSpeed);
private void aquireDataFromUser()
kb = new Scanner(System.in);
System.out.println("Enter the information for your class: ");
System.out.println("Enter HP: ");
maxHP = kb.nextInt();
System.out.println("Enter MP: ");
maxMP = kb.nextInt();
System.out.println("Enter SP: ");
maxSP = kb.nextInt();
System.out.println("Enter Base Melee Damage: ");
baseMeleeDmg = kb.nextInt();
System.out.println("Enter Base Spell Damage: ");
baseSpellDmg = kb.nextInt();
System.out.println("Enter AC: ");
baseAC = kb.nextInt();
System.out.println("Enter Speed: ");
baseSpeed = kb.nextInt();
Beside making createCustomClass()
shorter it makes the useless reassingment of the member variables obvious.
edited Apr 12 at 19:45
answered Apr 5 at 15:31
Timothy Truckle
4,673316
4,673316
1
@AJD "The other identifiers make perfect sense to me." - If this is your start into a programmers career keep in mind that you will have to collaborate with others. And the identifiers you choose must make "perfect sense" to your futur team mates too. Also: you asked for improvements. Accept them or not. There is no need to explain yourself...
â Timothy Truckle
Apr 6 at 7:49
1
@AJD So your argument is that cryptic identifier names are OK because of context? Well my experience in the programming business is only 20 years. But I for myself find it easier to work with clear names without acronyms even if they are somewhat lengthly. And please keep in mind: modern languages (like java or even C) have virtually no limit for the length of identifiers, and actual monitors can show more than 80x25 characters, Some progress happened here in the last 40 years... ;o)
â Timothy Truckle
Apr 6 at 8:07
1
@AJD "his is common parlance in that context" And BTW this was the reason why two mars missions failed (Mars Climate Orbiter and Mars Polar Lander): some teams contributing software had their own context (metric vs imperial units). So yes: I think that identifiers should not have any acronyms and only short living local variables may be very common abbreviations.
â Timothy Truckle
Apr 6 at 8:28
1
I think the question to ask is "why?" Why would you use abbreviated names? The answer used to be simple. To save space and typing. But, with better monitors and autocomplete, those reasons have mostly gone by the way side. So, it is always good for a developer to be descriptive when naming things in these modern languages, whether or not the context is understood or will only be used by specific people that understand it. It's always good for a developer to code using descriptive naming; building good habits. There are exceptions, like JavaScript (size of files), but that doesn't apply here.
â adpro
Apr 6 at 11:20
1
@AJD, you're saying the same stuff you said before. It's also about building good habits and making sure the code is maximally readable by all developers. Abbreviating variable names is frowned upon in order to create good habits. It absolutely should bemaxHitPoints
. This developer is asking for a code review. Someone that knows the language may not understand the context and not understand whatmaxHP
means in this very question. So, the abbreviation would create confusion when asking for code review on a site like this. This question is an example of why descriptive naming is important.
â adpro
Apr 9 at 15:32
 |Â
show 13 more comments
1
@AJD "The other identifiers make perfect sense to me." - If this is your start into a programmers career keep in mind that you will have to collaborate with others. And the identifiers you choose must make "perfect sense" to your futur team mates too. Also: you asked for improvements. Accept them or not. There is no need to explain yourself...
â Timothy Truckle
Apr 6 at 7:49
1
@AJD So your argument is that cryptic identifier names are OK because of context? Well my experience in the programming business is only 20 years. But I for myself find it easier to work with clear names without acronyms even if they are somewhat lengthly. And please keep in mind: modern languages (like java or even C) have virtually no limit for the length of identifiers, and actual monitors can show more than 80x25 characters, Some progress happened here in the last 40 years... ;o)
â Timothy Truckle
Apr 6 at 8:07
1
@AJD "his is common parlance in that context" And BTW this was the reason why two mars missions failed (Mars Climate Orbiter and Mars Polar Lander): some teams contributing software had their own context (metric vs imperial units). So yes: I think that identifiers should not have any acronyms and only short living local variables may be very common abbreviations.
â Timothy Truckle
Apr 6 at 8:28
1
I think the question to ask is "why?" Why would you use abbreviated names? The answer used to be simple. To save space and typing. But, with better monitors and autocomplete, those reasons have mostly gone by the way side. So, it is always good for a developer to be descriptive when naming things in these modern languages, whether or not the context is understood or will only be used by specific people that understand it. It's always good for a developer to code using descriptive naming; building good habits. There are exceptions, like JavaScript (size of files), but that doesn't apply here.
â adpro
Apr 6 at 11:20
1
@AJD, you're saying the same stuff you said before. It's also about building good habits and making sure the code is maximally readable by all developers. Abbreviating variable names is frowned upon in order to create good habits. It absolutely should bemaxHitPoints
. This developer is asking for a code review. Someone that knows the language may not understand the context and not understand whatmaxHP
means in this very question. So, the abbreviation would create confusion when asking for code review on a site like this. This question is an example of why descriptive naming is important.
â adpro
Apr 9 at 15:32
1
1
@AJD "The other identifiers make perfect sense to me." - If this is your start into a programmers career keep in mind that you will have to collaborate with others. And the identifiers you choose must make "perfect sense" to your futur team mates too. Also: you asked for improvements. Accept them or not. There is no need to explain yourself...
â Timothy Truckle
Apr 6 at 7:49
@AJD "The other identifiers make perfect sense to me." - If this is your start into a programmers career keep in mind that you will have to collaborate with others. And the identifiers you choose must make "perfect sense" to your futur team mates too. Also: you asked for improvements. Accept them or not. There is no need to explain yourself...
â Timothy Truckle
Apr 6 at 7:49
1
1
@AJD So your argument is that cryptic identifier names are OK because of context? Well my experience in the programming business is only 20 years. But I for myself find it easier to work with clear names without acronyms even if they are somewhat lengthly. And please keep in mind: modern languages (like java or even C) have virtually no limit for the length of identifiers, and actual monitors can show more than 80x25 characters, Some progress happened here in the last 40 years... ;o)
â Timothy Truckle
Apr 6 at 8:07
@AJD So your argument is that cryptic identifier names are OK because of context? Well my experience in the programming business is only 20 years. But I for myself find it easier to work with clear names without acronyms even if they are somewhat lengthly. And please keep in mind: modern languages (like java or even C) have virtually no limit for the length of identifiers, and actual monitors can show more than 80x25 characters, Some progress happened here in the last 40 years... ;o)
â Timothy Truckle
Apr 6 at 8:07
1
1
@AJD "his is common parlance in that context" And BTW this was the reason why two mars missions failed (Mars Climate Orbiter and Mars Polar Lander): some teams contributing software had their own context (metric vs imperial units). So yes: I think that identifiers should not have any acronyms and only short living local variables may be very common abbreviations.
â Timothy Truckle
Apr 6 at 8:28
@AJD "his is common parlance in that context" And BTW this was the reason why two mars missions failed (Mars Climate Orbiter and Mars Polar Lander): some teams contributing software had their own context (metric vs imperial units). So yes: I think that identifiers should not have any acronyms and only short living local variables may be very common abbreviations.
â Timothy Truckle
Apr 6 at 8:28
1
1
I think the question to ask is "why?" Why would you use abbreviated names? The answer used to be simple. To save space and typing. But, with better monitors and autocomplete, those reasons have mostly gone by the way side. So, it is always good for a developer to be descriptive when naming things in these modern languages, whether or not the context is understood or will only be used by specific people that understand it. It's always good for a developer to code using descriptive naming; building good habits. There are exceptions, like JavaScript (size of files), but that doesn't apply here.
â adpro
Apr 6 at 11:20
I think the question to ask is "why?" Why would you use abbreviated names? The answer used to be simple. To save space and typing. But, with better monitors and autocomplete, those reasons have mostly gone by the way side. So, it is always good for a developer to be descriptive when naming things in these modern languages, whether or not the context is understood or will only be used by specific people that understand it. It's always good for a developer to code using descriptive naming; building good habits. There are exceptions, like JavaScript (size of files), but that doesn't apply here.
â adpro
Apr 6 at 11:20
1
1
@AJD, you're saying the same stuff you said before. It's also about building good habits and making sure the code is maximally readable by all developers. Abbreviating variable names is frowned upon in order to create good habits. It absolutely should be
maxHitPoints
. This developer is asking for a code review. Someone that knows the language may not understand the context and not understand what maxHP
means in this very question. So, the abbreviation would create confusion when asking for code review on a site like this. This question is an example of why descriptive naming is important.â adpro
Apr 9 at 15:32
@AJD, you're saying the same stuff you said before. It's also about building good habits and making sure the code is maximally readable by all developers. Abbreviating variable names is frowned upon in order to create good habits. It absolutely should be
maxHitPoints
. This developer is asking for a code review. Someone that knows the language may not understand the context and not understand what maxHP
means in this very question. So, the abbreviation would create confusion when asking for code review on a site like this. This question is an example of why descriptive naming is important.â adpro
Apr 9 at 15:32
 |Â
show 13 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191289%2ftext-based-game-in-java%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
3
Could you include the actual class this is in? These 2 methods are used to initialise a certain class but you're not really creating one. Without the surrounding code it's hard to explain the difference.
â Imus
Apr 5 at 8:32
This looks like it could be a pretty good spot for the Builder Pattern. But, I would need to see more code to make a recommendation.
â adpro
Apr 5 at 12:44