• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

How to structure a class with optional parameters

 
Sheriff
Posts: 14990
250
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The power() method can be anything that you can imagine it to be. I could imagine it to look at the user gesture, if it was the X button or Y button. Maybe X and Y have a different effect on how much power or damage a given weapon can inflict. Maybe I could look at the direction that the Character is facing and calculate the distance the hand that's holding the weapon has to the opponent.  From my martial arts principles, the shorter the distance in which an attack is made, the less power you can usually generate to transfer to the receiver of the attack. That's why people who practice Aikido, like me, like to close the gap between them and their opponent. This helps to neutralize the opponent's strength and allows you to position yourself and create more leverage to use against the opponent.

It's really up to you and your imagination and how sophisticated you want your power() logic to be. It could be as simple as what you showed it. However, the principle of abstraction remains. You still want to hide those details from the higher level methods and to them, all they need to know is that they are calling the power() method to get some indication of how much power is being used with a given Weapon to perform an attack on the opponent Character.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:The power() method can be anything that you can imagine it to be. I could imagine it to look at the user gesture, if it was the X button or Y button. Maybe X and Y have a different effect on how much power or damage a given weapon can inflict. Maybe I could look at the direction that the Character is facing and calculate the distance the hand that's holding the weapon has to the opponent.  From my martial arts principles, the shorter the distance in which an attack is made, the less power you can usually generate to transfer to the receiver of the attack. That's why people who practice Aikido, like me, like to close the gap between them and their opponent. This helps to neutralize the opponent's strength and allows you to position yourself and create more leverage to use against the opponent.

It's really up to you and your imagination and how sophisticated you want your power() logic to be. It could be as simple as what you showed it. However, the principle of abstraction remains. You still want to hide those details from the higher level methods and to them, all they need to know is that they are calling the power() method to get some indication of how much power is being used with a given Weapon to perform an attack on the opponent Character.



Let's assume our game is a simple text adventure game with basic logic, and if I wanted to keep the power method as simple as possible, I could simply return an int, correct? It would still be abstract? so doing this:



would be okay?
 
Junilu Lacar
Sheriff
Posts: 14990
250
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hank Emery wrote:Ideally where would you place this type of logic so that we don't violate DRY?


This may seem like a cop-out because it is so non-specific. Here's my answer, given the lack of context: I would put it where it makes the most sense. How do I know it makes sense? It fits in the story line. It fits in with the responsibilities of the class that I'm putting it in and of the method that I'm putting it in. And it doesn't blatantly violate other design principles.

It's a lot of heuristics. I know it doesn't help a beginner like you who might be looking for more of a recipe type answer. Read Andy Hunt's book, Pragmatic Thinking & Learning: Refactor Your Wetware. It's a book that's full of insights about how you think and learn. Programming is all about thinking and learning. When you design you think. When you refactor your think and learn. When you critique your program, you think and learn. When you critique other people's programs you think and learn. The day you stop thinking and learning is the day you should stop trying to program. You can't do one without the other.

Andy writes about how experts use heuristics (instinct, gut feel) because they already have a lot of context in their heads. Beginners don't have context that they can pull from past experience like experts can. They need recipes to do their work. Andy calls these "context-free rules" because you just follow them without knowing the context in which they are applied.  Same idea as in Shu Ha Ri, a concept that comes from martial arts like Aikido. I find it interesting how my practice of Aikido influences a lot of my thinking when I'm doing design and programming.
 
Junilu Lacar
Sheriff
Posts: 14990
250
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hank Emery wrote:would be okay?


I could say it's okay and you'd take my word for it and be done. That would be the real cop-out. I really should say it looks like it would be but it's not my code and it's not my story. Try it out. See if it works in your story. If it doesn't change it. If it does, then you've learned something about how you like to tell your stories and how to tell it well. Leave it for now and come back to it again if it start to give you trouble, making other parts of your story smell. If/When that happens (because there's a good chance it will start to smell after a while), come back and figure out how to clean up the story.

Read the book "Refactoring" by Martin Fowler. It has a catalog of smells that you should familiarize yourself with. Being familiar with those smells really helps improve your story-telling and story-fixing skills.
 
Junilu Lacar
Sheriff
Posts: 14990
250
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gotta go now. It was a great conversation and I think I'm going to bookmark this thread because I've dumped a lot of thoughts into it that I might want to use for my own purposes (folks here have been egging me to write a book and that's what I'm working on right now). I hope you're fine with me using some of your code as examples or as the basis for examples. I think they can help a lot of other people who are in the same boat as you are. I'd rather use real examples than ones I concocted myself, just to keep it all real.
 
Yes, of course, and I accept that blame. In fact, i covet that blame. As does this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!