This page shows how to make an object-oriented program. If you know some basic things about object-oriented programming, you can understand this contents well.
I found the material (this(sorry,
this is Japanese.),this material refers to
this book.)
which shows how to make an object-oriented program. I have understood how I
should make an object-oriented program by this material. I'll introduce the
rules to make good object-oriented programs which I adjusted and the effects of
this.
Also, I'll show what goal should be reached by object-oriented programming.
What is object-oriented program?
At first, I'll show the meaning of words.
- program:the document with orders given a computer. make the document with orders given a computer.
- programming language:the language to give orders for a computer. "language" is similar to Japanese and English. I mean programming language is the tool to communicate with computers.
- object-oriented program:program made of the short and simple orders and the combination of them.
- object-oriented programming language:the programming language designed to make object-oriented program easily. For example, C++, Java, and Python.
"object-oriented" shows how to program.
When you object-oriented program, you don't necessarily have to use object-oriented programming language.
If you use object-oriented programming language, you can't always make an object-oriented program.
Here, I'll introduce "class" in object-oriented programming languages.
"class" have short tasks(functions) and the variable related with it.
For example, if we consider the class "HP", that will include HP as variable, restore, be injured, and judge if the character is dead or not as tasks.
Advantage of object-oriented program
The advantage of object-oriented program is the following.
- prevent bugs:We can prevent bugs because the programs are composed of short and simple orders.
- easy to correct bugs:We can find bugs easily because the programs are composed of short and simple orders.
- easy to change programs:We can change programs easily because we can correct bugs easily.
- reuse:We can reuse the same classes in different locations because they are composed of short and simple orders. We can program fast. Also, if we correct bugs of the programs, it is applied in all locations where they are used.
Rules to make good object-oriented programs
You can understand what object-oriented program is because you can experience object-oriented program if you make object-oriented programs following the rules.
You can keep good object-oriented program without following the rules if you get familiar with object-oriented program.
The rules are the following.
- make class less than 80 rows:have to
If you make a small class, it should be composed of short and simple orders. Therefore, you can prevent bugs, correct them easily, change programs easily. - make simple class:have to
If you make complex class, the class should have complex tasks. When you make simple class, you can prevent bugs, correct them easily, change programs easily. Even if you just follow either this rule or above rule, you can change programs easily. At first, you just can follow it. Then, you can follow the other rules.The following is useful to make classes simple.- Instance variable is less than two
class HP{
private int hp;(This is instance variable) - add less than two indents. It is better to add less than one indent.
- Instance variable is less than two
- don't use "setter":should
class HP{
private int hp;
public int set(int setHp)//This is "setter"
{
hp=setHp;
}
It is better for us not to use "setter". "setter" can change a variable to unexpected value in unexpected location. You can prevent bugs by following this rule. You may think how to change a variable. You should initialize variables by constructor. You should change a value like the following. For example, you can change hp to heal HP like the following. public void heal(int healVal)
{
hp=hp+healVal
}
you can change hp to damage HP like the following.
public void damage(int damageVal)
{
hp=hp-damageVal
}
You should change a value in the function for a certain condition. - Tell, don't ask.:may as well
It is not good for instance variable to be referred at the outside of a class. A task should be done in related class. Let me show an example.
class HP{
private int hp;
public int get()//"Getter"
{
return hp;
}
public boolean deadJudge()//better than "Getter"
{
if(hp<=0)
{
return true;
}
return false;
}
}
--------------------------------------------
The following is bad.
if(HP.get()<=0)
{
(The task is written in the case when a character is dead)
}
The following is good.
if(HP.deadJudge()==true)
{
(The task is written in the case when a character is dead)
}
You can't know how to use "Getter". On the other hands, "HP.deadJudge()" is just used to judge whether a character is dead or not. If "HP.deadJudge()" is used for this judge, it should be wrong. Also, if "HP.deadJudge()" return true when hp is more than 0, it should be wrong. You can recongnize bugs easiliy. - Primitive data type or string data type should be included in a class:may as
well
Let me show an example.
class HP{
private int hp;
public boolean deadJudge()
{
if(hp<=0)
{
return true;
}
return false;
}
}
The task related with a variable is defined in the class with the variable. It gets clear what you should write or where you should or where somethig is written. You can change programs easily by following this rule. - use less than one dot:may as well
Let me show an expample.
The following is a bad example.
class First{
public Second Second;
}
class Second{
public void test()
{
}
}
public static void main(String args[])
{
First.Second.test()
}
The following is a good example.
class First{
private Second Second;
public void test()
{
Second.test();
}
}
class Second{
public void test()
{
}
}
public static void main(String args[])
{
First.test()
}
If you adjust "Second.test()", it is sufficient to adjust "First.test()". You can prevent a correction from causing another correction. - The name of class or function shows what it is even if the name gets
long:may as well
This doesn't agree with the reference. The reference says the long name is bad because the class or function should be complex when its name is long. However, I think the class or function should get simple by the above rules. If the name of a class or function shows what it is, you can prevent from using it wrongly. This prevents bugs.
If you can't remember all rules, it is usefule to remember only the first rule "make class less than 80 rows" or the second rule "make simple class".
It is difficult to make ideal object-oriented programs. At first, you can make programs forgetting ideal object-oriented programs. Then, you can arrange the programs. This arrangement is called "refactoring".
How to combine orders
The object-oriented programs are made of the short and simple orders and the combination of them.
I said "Getter" and "Setter" shouldn't be used in the above.
I'll show how to combine orders without "Getter" and "Setter". You can do that by selecting a class as an argument.
Let me show an example.
class HP{
private int hp;
public boolean deadJudge()
{
if(hp<=0)
{
return true;
}
return false;
}
}
--------------------------------------
class GameOver{
public void execute(HP PlayerHP)
{
if(PlayerHP.deadJudge()==false)
{
return;
}
(The process for "game over")
}