Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Object orientation is a design approach that attempts to model real world objects in the code.
Try to think in objects.
Make use of the visibility of objects.
Hide your implementation from the user.
On the other hand, not every class needs to have a set of private data members with a full-blown set of Setters and Getters. It is ok to have a class with only public data members.
Rule: What belongs together logically should be handled together
Example:

(thumbs up)

(thumbs down)

Code Block
class SimpleParameterClass {
public int spaceX;
public double tanPolarPhi;
public float cosLine1Line2;
}
...
SimpleParameterClass parms = new SimpleParameterClass();
parms.spaceX = 17; // you can simply set it
...
System.out.printf("%f", parms.cosLine1Line2); // or get it -- no need for encapsulation
Code Block

double[3] parms = new double[] {17, 3.4, 2.5};
parms[2] = 2.5; // Which one is it ???

Optimization

Optimization obfuscates code. It also forces you to change your intuitive approach.
Rule: First, write your code. Then profile it. Then, if necessary, optimize it.

...

(thumbs up)

(thumbs down)

Code Block
enum MyClassFlags {doThingX, doThingY, doThingZ};
    class MyClass {
    MyClassFlags flags;
    void setFlags(EnumSet f) {flags = f;}
}
...
EnumSet<MyClassFlags> myFlags = new EnumSet<MyClassFlags>();
myFlags.add(MyClassFlags.doThingX); // yes, it is more verbose. That is a good thing !
MyClass c;
c.setFlags(myFlags);
Code Block
class Obscure {
    int obscureFlag;
    void setObscureFlag(int o) {obscureFlag = o;}
}
...
Obscure o
o.setObscureFlag(3);
Note

Never hardcode numbers like this

"Overloaded Operators"

It is very confusing to have members like add(Object o1, Object o2) as members of the classes. This is counter-intuitive. Either there ought to be a supporter class with static members only, or the member of the type class expects only one argument.
So, design it to look like thisThe Java language does not support the concept of overloading operators. If you decide to write a custom operator for your class, consider the following:

(thumbs up)

(thumbs down)

Code Block
vec1.add(vec2);

or

Code Block
import static x.y.add; add(vec1, vec2);
Code Block
vec1.add(vec1, vec2);

...

Strings should be used for input/output operations only. They are slow to parse and cannot be checked for typos at compile time.
Using them to set flags and parameters is not recommended.
See the section about status codes above. If you need to set different parameters with the same function, use an EnumMap