Too many variables in Java? [closed]

2019-07-18 12:23发布

问题:

I'm still fairly new to Java, but I personally feel my code is sloppy, having way too many variables. I know I could hide them in methods or classes, but I heard that putting them in a separate class is bad practice and most of them aren't static. Can anyone give me some tips? Thanks in advance!

public static float c = .2f; //cube dimensions
private float separation = 4.0f*c;
private float angle = 0f; //set to 0
private float rotate = 1.0f;
private float h_height = 1.0f;
private float h_width = 1.0f;
private float h_length = 1.0f;
private float b_height = 1.5f;
private float b_width = .5f;
private float b_length = 1.0f;
private float a_height = 1.5f;
private float a_width = .5f;
private float a_length = .5f;
private float l_height = 1.5f;
private float l_width = .5f;
private float l_length = .5f;
private float h_x_s = 0.0f*c;
private float h_y_s = 5.0f*c;
private float h_z_s = 0.0f*c;
private float h_x;
private float h_y;
private float h_z;
private float b_x_s = 0.0f*c;
private float b_y_s = 2.5f*c;
private float b_z_s = 0.0f*c;
private float b_x;
private float b_y;
private float b_z;
private float a_x_s = 1.5f*c;
private float a_y_s = 2.5f*c;
private float a_z_s = .0f*c;
private float a_x;
private float a_y;
private float a_z;
private float l_x_s = .5f*c;
private float l_y_s = -.5f*c;
private float l_z_s = 0.0f*c;
private float l_x;
private float l_y;
private float l_z;
private float a_rotate = 0f;
private float a_speed = .6f;
private float l_rotate = 0;
private float l_speed = .6f;
private double a_c;
private double l_c;
private double a_s;
private double l_s;
private float max_a = 30f; //angle
private float max_l = 30f; //angle
private boolean a_forward;
private boolean l_forward;
private float move_z = 0; //set to 0
private float speed_z = .01f*separation;
private float max_z = 2*separation;
private boolean forward_z;

P.S. The comments aren't actually what the variables are for, just reminders to me while debugging. Also, my variable names will probably meet much disapproval from professional developers, but it's something I can keep track of easily. And like I said before, the majority of these variables are actively changing, and used in multiple methods for that matter, but a few I'm away I can hide away.

回答1:

It looks like your variables are screaming for you to put them in classes.

I'm just inferring based on your variable names, but I'm almost certain that's what you need.

For example, you might implement 3 classes like the ones at the bottom of this answer.

This would allow your variable declarations to look something like this:

Variable declarations:

Polygon polygonA = new Polygon(1.5, .5, .5);
Polygon polygonB = new Polygon(1.5, .5, 1.0);
Polygon polygonL = new Polygon(1.5, .5, .5);

Coordinate coordH = new Coordinate();
Coordinate coordB = new Coordinate();

Transform transformA = new Transform(0, .6);
Transform transformL = new Transform(0, .6);

Example class definitions: (each of these goes in a separate file)

class Polygon {

    float height;
    float width;
    float length;

    public Polygon(int h, int w, int l) {
        height = h;
        width = w;
        length = l;
    }
}

class Coordinate {

    float x; 
    float y;

    public Coordinate() {}

    public Coordinate(float xCoord, float yCoord) {
        x = xCoord;
        y = yCoord;
    }
}

class Transform {

    float rotate;
    float speed;

    public Transform(float r, float s) {
        rotate = r;
        speed = s;
    }
}


回答2:

I'm not sure where you got the idea that it is not done to put variables in separate classes. I don't know the intent of your code, but I guess it has something to do with cubes.

You seem to have four types of variables: h, b, a and l, and they are very similar. You should abstract them into a class. Something like this:

public class Cube
{
    private float height;
    private float width;
    private float length;

    private float x_s;
    private float y_s;
    private float z_s;

    private float x;
    private float y;
    private float z;

    // Add getters, setters, constructors, etc...
}

And then you can reduce your current code to:

private Cube h;
private Cube b;
private Cube a;
private Cube l;

Quite neat, right?

And you could further abstract the coordinates and sizes into a Vector3 object:

public class Vector3
{
    private float x;
    private float y;
    private float z;
}

Making your Cube:

public class Cube
{
    private Vector3 size;
    private Vector3 position_s;
    private Vector3 position;
}

Or, if size and position are really different things, you could even make a Point class for the X, Y, Z coordinates, and a Size class for the height, length, width values.

By the way, you really should use meaningfull variable names. Things like h, a, b, l and x_s don't mean anything to me. And if you look at your code after some time, it won't mean anything to you either.



标签: java oop