[JAVA] The reason why haze remains when trying to regard object orientation as "English"

0. Conclusion

Let's distinguish between transitive verbs and intransitive verbs. That's it.

1. Introduction

You've probably heard that object-oriented, reasonably readable code can be considered English. To specifically read the code as English, read as follows.

oop↔eng


subject.predicate();//1st sentence pattern
subject.predicate(complement);//2nd sentence pattern
subject.predicate(Object);//Third sentence pattern
subject.predicate(Object, Object);//4th sentence pattern
subject.predicate(Object,complement);//5th sentence pattern

2. Problem

2-1. Easy-to-understand problems

However, depending on the type of predicate (verb), it may be doubtful whether the "subject" is really the "subject". For example, in java's List (for those who don't know, it's like an array) list.add(obj); You can write like this. This code means adding ʻobjto the instancelist of List. ** Never add list" ga "(somewhere) ʻobj **. Therefore, in English, it becomes "JVM adds list obj.", And list is no longer the subject. If you pursue English correctness, "JVM adds list obj." Is also incorrect. Since add does not take the 4th sentence pattern, it should be written obediently as "JVM adds obj to list." In the 3rd sentence pattern with modifiers. However, this is a natural language problem and is not suitable for considering a unified conversion rule to an artificial language. Therefore, all transitive verbs will continue to correspond to the 4th and 5th sentence patterns. </ sub> </ sub>

2-2. Practical big problem

If the problem in Section 2-1 is sought, it will develop into a big problem in which the accessors are switched 180 degrees. That is, a getter becomes a setter, and a setter becomes a getter. For example Field ram And its accessors getRam(int address), setRam(int address, byte value) Class with OS Prepare and its instance computer Let's say you made.

OS class and computer instance


public class OS
{
    private byte[] ram;
    public byte getRam(int address){return this.ram[address];}
    public void setRam(int address, byte value){this.ram[address]=value;}
    public OS(int ram_size){this.ram = new byte[ram_size];}
}

class Main{ public static void main(String...args)
{
    OS computer = new OS(64);//Creating a computer with 64 bytes of RAM
    int i=0;
    for(char each_char : "hello world".toCharArray())
        computer.setRam(i++, (byte)each_char);
        //0~Write "hello world" at address 10

    for(int j=0; j<11; j++)
    	System.out.print((char)computer.getRam(j));
        //0~Address 10(As char)display
        //→ "hello world" is displayed on the console.
}}

In this way, you can define your own OS (which is useless), create a virtual computer (which is useless) with that OS, and display it as hello world.

here,

oop


    int i=0;
    for(char each_char : "hello world".toCharArray())
        computer.setRam(i++, (byte)each_char);

Is writing "hello world" to RAM. Who is the subject of set Ram? So who is writing (setting) the RAM? ** Here the code is a big lie **. The code answers this question, perhaps computer. ** computer is the object to be written **, isn't it? .. ..

If the subject of setRam is really computer, The computer is writing (setting) RAM to the outside world. And that is nothing but the function of getRam," returning the contents of RAM to the caller ".

In other words, if you interpret the ** code obediently, the setter becomes a getter. ** ** If you are an advanced person who does not understand this claim, please think back to the beginning. When "computer" "writes" "ram" (to "caller") Is ram written to computer? Or is it written to the "caller"? Let's think about it. I want you to notice there. I was used to the ridiculous world and my senses were paralyzed.

** Similarly, getters also become setters **. A straightforward interpretation of computer.getRAM (~); This is nothing but the definition of setter because "'computer'" gets "" ram "".

3. Cause of the problem

This problem arose because there are too few engineers who can (respect) distinguish between transitive verbs and intransitive verbs. At least Oracle has confused transitive verbs with intransitive verbs to explain object-orientation. I have to say that it is correct to not understand object-oriented programming anymore. The following is quoted and translated from https://docs.oracle.com/javase/tutorial/java/concepts/object.html.

(Quote)

Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes).

(Japanese translation)

Real-world objects have two characteristics. "State" and "action". Dogs have "name", "color", "breed", "hunger" as states, It has "barking", "adding", and "shaking its tail" as actions. If it is a bicycle, it has "current gear", "current pedaling speed", and "current speed" as states. Actions include "gear change", "change pedaling speed", and "brake operation".

In this text, there is a decisive (no, fatal) difference between dog behavior and bicycle behavior. The behavior of a dog is the "action that a dog takes by himself", while the behavior of a bicycle is the "action that something other than himself takes". Dog behaviors "barking," "adding," and "shaking the tail" are all ** intransitive verbs with the dog as the subject **, Bicycle behavior "Gear change," "changing the speed at which you pedal," and "activating the brakes" are ** transitive verbs that do not use bicycle as the subject **.

An example given as a bicycle behavior is actually "** operation ** applicable to a bicycle".

Even if you confuse the "behavior" of a dog with the "operation" of a bicycle and say, "This is an example of object-oriented programming," it would be incomprehensible to a beginner's honest sense. ** The current situation is that you can understand object-oriented programming only when you try hard to forget the difference between "behavior" and "operation" and get the wrong direction for get and set . In this case, I have to say, " It's better not to understand **" (Understanding the subject.predicate (object) as a lie) ** Understanding object-orientation can even be said to be a kind of brainwashing **.

4. Solution

4-1. Theory

By changing the transitive verb method to the passive voice or moving the instance to the first argument, the object-oriented code can be read correctly as English. Chapter 4 will explain this.

As shown in Chapter 3, the culture that does not distinguish between transitive verbs and intransitive verbs has led to the confusion between the subject and the object. If you dig deeper, there is a culture in object-oriented programming that considers the object of a transitive verb as the subject. ・ ・ ・ ① Originally, the subject of intransitive verbs is "active actor" (eg barking dog) </ sub> </ sub>. The subject of transitive verbs is "operator" (eg, a person who changes the gear of a bicycle) </ sub> </ sub>. The object of the transitive verb is "passive actor" (eg, a bicycle that can change gears **) </ sub> </ sub>. ・ ・ ・ ② To summarize (1) and (2), "Object-oriented programming has the problem of reversing the passive noun and the subject noun when a transitive verb appears."

Conversely, when a transitive verb appears, the subject and object can be reversed again by rewriting it into the passive voice, so the correct English sentence is completed. For example bicycle.change_gear_to(5); Is bicycle.is_changed_gear_to(5); Or bicycle.gear_is_changed_to(5); If so, it can be read as an English sentence that can be translated into Japanese as "a bicycle is changed to a gear of 5," so it is also correct in terms of meaning. ・ ・ ・ Passive method

Or even in the code, it brings the instance to the position of the argument. change_gear(of bicycle,to 5); In this case, it can be read correctly as an English sentence lacking the subject (JVM for java), that is, an instruction sentence. ・ ・ ・ Purpose wording

4-2. Practice of passive method

For an example of an OS class and computer instance:

OS class and computer instance


public class OS
{
    private byte[] ram;
    public byte is_got_ram(int address){return this.ram[address];}
    public void is_set_ram(int address, byte value){this.ram[address]=value;}

    public OS(int ram_size){this.ram = new byte[ram_size];}
}

class Main{ public static void main(String...args)
{
    OS computer = new OS(64);//Creating a computer with 64 bytes of RAM
    int i=0;
    for(char each_char : "hello world".toCharArray())
        computer.is_set_ram(i++, (byte)each_char);//by jvm
        //0~Write "hello world" at address 10

    for(int j=0; j<11; j++)
    	System.out.print((char)computer.is_got_ram(j));//by jvm
        //0~Address 10(As char)display
        //→ "hello world" is displayed on the console.
}}

Certainly, although it can be read correctly as English This time, there are many _ in the method name, which makes it difficult to understand. (In particular, it is a problem that methods starting with is conflict with the rule of returning boolean type)

This problem does not occur in the object usage (sections 4-3).

4-3. Practice of target grammar

For an example of an OS class and computer instance:

String code = "

OS class and computer instance


public class OS
{
    private byte[] ram;
    public byte getRam(int address){return this.ram[address];}
    public void setRam(int address, byte value){this.ram[address]=value;}

    public OS(int ram_size){this.ram = new byte[ram_size];}
}

class Main{ public static void main(String...args)
{
    OS computer = new OS(64);//Creating a computer with 64 bytes of RAM
    int i=0;
    for(char each_char : "hello world".toCharArray())
        SetRam(of computer,i++, (byte)each_char);
        //0~Write "hello world" at address 10

    for(int j=0; j<11; j++)
    	System.out.print((char)GetRam(of computer,j));
        //0~Address 10(As char)display
        //→ "hello world" is displayed on the console.
}}

"; code=new P().parse(code);

This is easy to read, For example SetRam(of computer, i++, (byte)each_char) To computer.setRam(i++, (byte)each_char) You need to build a parser P to replace it with. (Regular expressions do not accept parentheses language, so power is insufficient)

(5. Postscript Mediopassive voice)

I was interested in it while not studying. In the comment section of this article, he introduced the idea that "object-oriented is mediopassive". ʻA.B (C); is commanded by the main method and other callers, It means that "ʻA has no choice but to doB (C)". This is essentially the same as "ʻA yo B (C) do" (only the subject is placed differently). boy.say (stone.place);` can be read as "Boy, tell me where the stones are!" Or "Boys can't help but say where the stones are".

To interpret in the latter You can rewrite it as boy.be_made_to_say (stone.place);

Recommended Posts

The reason why haze remains when trying to regard object orientation as "English"
Fighting notes when trying to do the same thing as Java's XOR Mode in C #
How to use the getter / setter method (in object orientation)