How do I combine two methods that have identical calculations but operate (read and write) different fields of the class.
A VERY simplified aircode example:
class TileCalculator
{
int length;
int width;
int tileLength;
int tileWidth
int cols;
int rows;
void calculateColumns()
{
this.cols = this.width/this.tileWidth;
}
void calculateRows()
{
this.rows = this.length/this.tileLength;
}
}
As these two methods do exactly the same calculation(s) but just using different fields for their input and output it would seem sensible to combine them but I don't know how.
UPDATE: I think I may have oversimplified it to the point where answerers are trying to solve the specific case. A more realistic example is:
void calculateCols()
{
int tileCols = width/tileWidth;
int remainder = width%tileWidth;
if (remainder==0) {
// there is an exact number of whole tiles
fullTileCols = tileCols;
firstT = tileWidth;
usedTileCols = tileCols;
} else {
// there is a remainder
fullTileCols = tileCols - 1;
firstT = (remainder+tileWidth)/2;
usedTileCols = tileCols + 1;
}
}
void calculateRows()
{
int tileRows = length/tileLength;
int remainder = length%tileLength;
if (remainder==0) {
// there is an exact number of whole tiles
fullTileRows = tileRows;
firstCut = tileLength;
usedTileRows = tileRows;
} else {
// there is a remainder
fullTileRows = tileRows - 1;
firstCut = (remainder+tileLength)/2;
usedTileRows = tileRows + 1;
}
}
I'm not saying a redesign isn't the answer but as you can see there are multiple fields involved so a simple return value probably isn't going to cut it. This is why I am using fields rather than a simple function and the maintainability of the current setup is of concern to me.
No, I wouldn't combine them, I would change them.
I'd get rid of rows and cols fields
I'd get rid of the above methods as it makes your object's state dependent on these methods always being called before an object is used -- a risky proposition.
Instead I'd create two calculated getter methods. This way the calculations are guaranteed to be done when needed.
e.g.,
public int getColumns() {
return width / tileWidth;
}
public int getRows() {
return length / tileLength;
}
Edit
I suppose you could create a RowCol class that has full, first, and used fields, and that has but one equation for doing the calculation above, and that you create two instances, one for row and one for column in the containing class, but if the rationale for this is to just combine these small methods, I question the need for this, or the benefit. Yes, you should follow the DNRY rule, but I worry more about this when I have three or more repeats of the same code.
You could make a convenience method. In the case you have shown this is actually more typing, longer program, extra complexity etc for no benefit. But if the calculation was more complicated it could be worth it
int calculate(int a, int b)
{
return a/b;
}
void calculateColumns()
{
this.cols = this.calculate(this.width, this.tileWidth);
}
after the update
you actually want 3 return values(full, first, used) so alter the "calculate" to either return a special class with 3 int or an array of int
Then feed in a and b as before but with the adjusted logic and return the 3 values and set them in the calling function
There is no easy way to do this in Java prior to Java 8. You can do it, but it involves using private internal interfaces and anonymous classes. It isn't worth it unless you're really talking about a lot of common lines of code.
With Java 8 though, you'll be able to use closures which will greatly simplify this kind of cases.
Related
I have a unit(soldier) class which contains a getAttackBonus() method and a getResistBonus() method. These must return different values for every time a soldier either attacks or is attacked.
To be specific, getResistBonus() can for example start at 8 but for every time the soldier is attacked, it will decrease by 2 until it reaches a certain value (for example 2 as a final resist bonus) where it will no longer decrease. How would I go about doing this?
Currently I am using in my method which does not work when I attempt to test it as a JUnit class, it keeps giving me 6 as the integer:
public int getResistBonus() {
int resist = 8;
while(resist != 2) {
return resist -= 2;
}
return 2;
}
You need to change a bit your code.
First you need to define resist at instance level, not method level.
Second it is better to use an if instead of a while because you are not making a loop, but only checking a single condition.
So the code can be something similar to that:
public class YourClass {
// Define resist at instance level
private int resist = 8;
....
public int getResistBonus() {
// Replace the while with an if
if (resist > 2) {
resist -= 2;
}
return resist;
}
}
I write plugins for Minecraft and often hit a situation where I have to loop through every block in a 3-dimensional space to process the block in some way. For example:
for (int x = minX; x <= maxX; x++) {
for (int y = minY; y <= maxY; y++) {
for (int z = minZ; z <= maxZ; z++) {
// Do something
}
}
}
These kind of loops happen a lot and it struck me that perhaps there was a better way to do it that would make the code more concise but still be as fast to execute. All I really want to do is loop through every coordinate in a 3 dimensional type.
The Bukkit API has such a type - a Vector class that can hold x,y,z. The Vector class has methods such as getBlockX, getBlockY and getBlockZ that return integers. So, I was thinking of creating a utility method like this:
Stream<Vector> getXYZStream(minVector, maxVector)
This would return a stream of all the vectors (x,y,z) values between the min and max vectors given. So then I could do:
getXYZStream(minVector, maxVector).forEach(v -> doSomething);
How can I make a stream of vectors in this regard? If I can code this approach, then I'll measure the difference in performance and see if it makes sense.
Or is there a better way to do this?
Or should I accept these loops are necessary and the best approach?
I found an approach where one can make a stream using a custom iterator, so one could do something like this:
public Stream<Vector> get3dStream(Vector minVector, Vector maxVector) {
Iterator<Vector> it = new CustomIterator(minVector, maxVector);
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(it, Spliterator.DISTINCT), false);
}
class CustomIterator implements Iterator<Vector> {
private Vector minVector;
private Vector maxVector;
BoundingBox b = new BoundingBox();
// constructor
CustomIterator(Vector minVector, Vector maxVector) {
// initialize cursor
this.minVector = minVector;
this.maxVector = maxVector.add(new Vector(1,1,1));
}
// Checks if the next element exists
#Override
public boolean hasNext() {
return !minVector.equals(maxVector);
}
// moves the cursor/iterator to next element
#Override
public Vector next() {
Vector r = minVector;
increment();
return r;
}
private void increment() {
// Increment the vector - this takes a lot of code to do
}
}
However, as you can see, tracking the next() in the custom iterator will require quite a lot of code and comparing to the three for loops is much more complex and likely to be slower. I agree with the comment from kaya3, it could hide some of the looping, but it's not going to be faster, which is 100% a requirement.
Is it a good practice to use properties as local variable. In cases where there are many methods which uses some variables, In each method the variable value changes. This avoids many times creating new variables and the code increases. Any suggestion?
private void method1(){
int totalLength = length1 + 10;
int totalBreath = (breath1 + breath2) + 20;
int size = (totalLength * totalLength);
System.out.println(size);
}
private void method2(){
int totalLength = length1 + 20;
int totalBreath = (breath1 + breath2) + 30;
int size = (totalLength * totalLength);
System.out.println(size);
}
private void method3(){
int totalLength = length1 + 60;
int totalBreath = (breath1 + breath2) + 10;
int size = (totalLength * totalLength);
System.out.println(size);
}
As you can see, totalLength, totalBreath, size is repeated in every method. Can i make them as fields of the class? So, i need not declare it in every method.
private void method1(){
totalLength = length1 + 10;
totalBreath = (breath1 + breath2) + 20;
size = (totalLength * totalLength);
System.out.println(size);
}
I read your question as, "When should a local variable be promoted to be a field of the class?"
The best answer is "it depends" but then again, its accuracy is quickly eclipsed by its lack of usefullness.
Fields should have a relationship to the class itself, as in is the field an attribute of the class? I include an example below to illustrate the syntax difference, but I agree with this post that it should be avoided if it pollutes the meaning of the class.
Usually you only need to have a field when you need the value of the field to be maintained between calls to different methods for a given instance of the class, with the option of making it static when the value should be maintained between method calls for all instances of the class. It will depend on several factors like shop convensions, performance goals, existing codebase, etc. so there is no single right answer without specific code. This question seems to include similar points. If you find yourself using the approach below, you might consider other approaches like refactor the behavior into a help class.
Another question asks the same question but from the perspective of a programming student.
Examples:
public class VariableScope {
int field1 = 3;
void foo() {
int a = 2;
// variable passing in width
bar1(1);
bar2(1);
// variable passing in depth
bar3(a);
// uses a field to reduce variable passing
baz1();
baz2();
}
void bar1(int param) {
System.out.println("param=" + param);
}
void bar2(int param) {
System.out.println("param=" + param);
}
void bar3(int param)
{
System.out.println("Passing param to bar4");
bar4(param);
}
void bar4(int param){
System.out.println("param=" + param);
}
void baz1() {
System.out.print("field1=" + field1);
}
void baz2() {
System.out.print("field1=" + field1);
}
}
From what it sounds like, if you're using the variable for multiple methods you're should declare the variable as a global variable. But yes, If no other method needs that variable , and you don't want to be writing a bunch of return statements you can use local variables
I suppose you mean a field by property which usually has accessor and mutator (get,set-methods).
In common you should keep the scope of a variable as small as possible. An example if you use many for loops like:
for ( int i = 0 ; i < 10 ; i++ ) {
}
and replace this by
int i;
method1() {
for ( i = 0 ; i < 10 ; i++ ) {
// some code;
}
}
method2() {
for ( i = 0 ; i < 10 ; i++ ) {
// some code;
}
}
If one thread calls method1() and another one method2() you would face a race condititon.
You can easily introduce hard to find bugs in your code.
I assume you mean something like this:
Class foo() {
int x;
public bar() {
for(x = 0; x <100; ++x) ...
} }
No, it's not good practice.
One place where it can even be harmful is in the synchronization/concurrency/multi-threaded case: if you are working with class members, they are going to need be synchronized which will eat into performance. Otherwise you risk multiple threads overwriting the value of the field and that can lead to errors in your program (likely hard to debug).
I am creating a game for a school project, and I have a 2 classes called Pieces and Powers, with subclasses Piece_Yellow, Piece_Blue ... and Power_Explode, Power_ChangeColor etc...
I was doing it with enum and someone from this website (mikera to be more precise) help me changing that and creating a better interaction with the pieces and the powers.
But now I have to change the old code, and I have problems with that because I was doing like this :
int x = 10, y = 5;
for (int i=0; i<10; i++) {
if (pecas[x][y] == null)
pecas[x][y] = new Piece(arrayPecas.get(rand.nextInt(arrayPecas.size())));
}
Like this my array was partially filled with 10 new pieces Objects with different colors (but no powers, that was the problem), and every single one had a different hashcode (for finding , comparing and deleting the pieces later)...
But Since we can't initiliaze an abstract class, the only solution that I've found to this problem was to do this :
int x = 10, y = 5;
for (int i=0; i<10; i++) {
if (pecas[x][y] == null)
pecas[x][y] = arrayPecas.get(rand.nextInt(arrayPecas.size()));
}
Like this the object is still added, but now I have 1 problems :
1 - All the hashcode from the pieces with the same color are the same... I don't really know how to solve this. I've read that I could override the hashcode method but there is no information to make the difference between them (and I can't store the position because I had to change it every time the piece change position).
It doesn't seem that really the hashCodes that are your problem. It's that the objects are simply equal. Code that relies on two unequal objects having different hashCodes isn't strictly correct. return 1; is a perfectly legal implementation of Object#hashCode().
The simplest thing seems to be to put a clone() method on Piece that all the subclasses can implement in order to return copies in places where you want different, distinct, objects.
public abstract class Piece {
public abstract Piece clonePiece();
}
public class YellowPiece extends Piece {
#Override
public Piece clonePiece() {
return new YellowPiece(this.relevantThing1, this.relevantThing2 // etc etc)
}
}
int x = 10, y = 5;
for (int i=0; i<10; i++) {
if (pecas[x][y] == null)
pecas[x][y] = arrayPecas.get(rand.nextInt(arrayPecas.size())).clonePiece();
}
I'm fairly confident that there's no way this could work, but I wanted to ask anyway just in case I'm wrong:
I've heard many times that whenever you have a certain number of lines of very similar code in one batch, you should always loop through them.
So say I have something like the following.
setPos1(getCard1());
setPos2(getCard2());
setPos3(getCard3());
setPos4(getCard4());
setPos5(getCard5());
setPos6(getCard6());
setPos7(getCard7());
setPos8(getCard8());
setPos9(getCard9());
setPos10(getCard10());
setPos11(getCard11());
setPos12(getCard12());
There is no way to cut down on lines of code as, e.g., below, right?
for (i = 0; i < 12; i++) {
setPos + i(getCard + i)());
}
I'm sure this will have been asked before somewhere, but neither Google nor SO Search turned up with a negative proof.
Thanks for quickly confirming this!
No way to do that specifically in Java without reflection, and I don't think it would be worth it. This looks more like a cue that you should refactor your getcard function to take an integer argument. Then you could loop.
This is a simple snippet that shows how to loop through the getters of a certain object to check if the returned values are null, using reflection:
for (Method m : myObj.getClass().getMethods()) {
// The getter should start with "get"
// I ignore getClass() method because it never returns null
if (m.getName().startsWith("get") && !m.getName().equals("getClass")) {
// These getters have no arguments
if (m.invoke(myObj) == null) {
// Do something
}
}
}
Like the others stated, probably it's not an elegant implementation. It's just for the sake of completeness.
You could do it via reflection, but it would be cumbersome. A better approach might be to make generic setPos() and getCard() methods into which you could pass the index of the current item.
You need to ditch the getter/setter pairs, and use a List to store your objects rather then trying to stuff everything into one God object.
Here's a contrived example:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Foo {
public static class Card {
int val;
public Card(int val) {
this.val = val;
}
public int getVal() {
return val;
}
}
public static class Position {
int value;
public Position(Card card) {
this.value = card.getVal();
}
}
public static void main(String[] args) {
List<Card> cards = new ArrayList<Card>(Arrays.asList(new Card(1), new Card(2), new Card(3)));
List<Position> positions = new ArrayList<Position>();
for (Card card : cards) {
positions.add(new Position(card));
}
}
}
You can't dynamically construct a method name and then invoke it (without reflection). Even with reflection it would be a bit brittle.
One option is to lump all those operations into one method like setAllPositions and just call that method.
Alternatively, you could have an array of positions, and then just loop over the array, setting the value at each index.
Card[] cardsAtPosition = new Card[12];
and then something like
public void setCardsAtEachPosition(Card[] valuesToSet) {
// check to make sure valuesToSet has the required number of cards
for (i = 0; i < cardsAtPosition.length; i++) {
cardsAtPosition[i] = valuesToSet[i];
}
}
Reflection would be your only option for your example case.