java - 关于中文写类名及方法名的疑问?
天蓬老师
天蓬老师 2017-04-17 16:32:42
0
12
1122

众所周知,java是支持中文的方法名和类名的,如下面代码

public class 谁的年龄大{
    //------------这就叫代码的可读性---------
    public static void main(String[] args) {
        int 张三的年龄 = 20;
        int 李四的年龄 = 21;
        boolean 张三的年龄是不是比李四的年龄要大 = 判断甲的年龄大还是乙大(张三的年龄, 李四的年龄);
        if (张三的年龄是不是比李四的年龄要大 == 对) {
            在控制台输出这么一句话并换行("张三的年龄大");
        } else if (张三的年龄是不是比李四的年龄要大 == 大错特错) {
            在控制台输出这么一句话并换行("李四的年龄大");
        }
    }
    
    //--------------定义自然语言(把你的注释删掉,改成方法名)--------------
    static boolean 对 = true;
    static boolean 大错特错 = false;
    
    /**
     * 你确定我应该写注释?
     */
    public static boolean 判断甲的年龄大还是乙大(int 甲的年龄,int 乙的年龄){
        return 甲的年龄 > 乙的年龄;
    }
    
    public static void 在控制台输出这么一句话并换行(String 要在控制台输出的话){
        System.out.println(要在控制台输出的话);
    }
}

输出 -> 李四的年龄大

中文化的好处在于,代码的可读性非常之强

上面这段代码,我拿给一个不懂编程的高中生看,也能非常清晰的看懂代码及里面的逻辑

而在团队开发中,成员素质良莠不齐,我个人觉得如果统一使用这一的命名规范的话,开发效率至少能提升50%以上!

中文化的缺点

  1. 不够国际化

  2. 在某些系统中,会出现乱码情况

能否用中英文混杂命名的方式起方法名

所以我最后的思考是,能否用中英文混杂的命名方法命名和编写查看代码,在编译运行的时候,统一自动把方法名类名中的中文去掉只留下英文,这样的方式是否可行呢?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(12)
黄舟
System.out.println("这就是为什么很多招聘的时候要求英语过四级");

Although Chinese characters are great, the capacity of Chinese fonts is a cancer in the programming world. .

update - 2016/1/4 14:18:10update - 2016/1/4 14:18:10

若如楼主所言,那么为啥全英文写的java源码里有比源码还要长

If it’s like what the original poster said, then why does the java source code written entirely in English have the comment is longer than the source code? I’m thinking deeply....

Eg.

/**
     * Prints a string followed by a newline. The string is converted to an array of bytes using
     * the encoding chosen during the construction of this stream. The bytes are
     * then written to the target stream with {@code write(int)}.
     *
     * <p>If an I/O error occurs, this stream's error state is set to {@code true}.
     *
     * @param str
     *            the string to print to the target stream.
     * @see #write(int)
     */
    public synchronized void println(String str) {
        print(str);
        newline();
    }

The key point of comments is to describe the content, import and export of your code segment in detail and clearly. As for which language to implement, this is not the point. 🎜
洪涛

Personally, I feel that not understanding the method name has a very small impact on efficiency. First of all, the code is commented. Although some people with mysophobia will say that the best code is self-commented, but the domain-specific business logic is still It is better to have comments. Comments can be written in Chinese. Secondly, even if code with poor logic is written in Chinese, college students may not be able to understand it. Therefore, algorithms, thinking, and familiarity with commonly used code logic have a greater impact on efficiency. I often find myself puzzled when looking at segmentfault.

So the uneven quality of members is the root cause of your team's inefficiency, brother. The right way is to do more training, write more exercises, and do more pair programming.

黄舟

Don’t you think a method name and variable name are too long? I didn't want to read the name until I was halfway through it. Also, I need to use an input method, which is really not as convenient as abc. . .

左手右手慢动作

Let me say something first:

Chinese does not have upper and lower case letters, so you cannot enjoy the benefits of CamelCase nomenclature. Chinese is composed of characters. Readers need to distinguish which characters form a word based on semantic meaning. This is very time-consuming and may lead to ambiguity and misinterpretation. In English, this can be distinguished by capitalizing the first letter of a word.

In addition, I think that as long as a person has learned junior high school English, he can basically cope with the English needed for programming. So, does Chinese programming serve people who have not graduated from primary school?

迷茫

Do you know how disgusting it is to switch input methods during programming

左手右手慢动作

A long time ago, it was said that someone wanted to code in Arabic, but finally found that it was better to use English...

Language and text are just tools, if they are inappropriate, they are inappropriate.

PHPzhong

First: It will increase the size of the code, which is inevitable
Second: Determine whether A is older or B (Zhang San’s age, Li Si’s age). It took me a long time to realize that this sentence is a method call
Third: English reading ability should not be a stumbling block to programming. If you are exposed to this kind of code all year round, are you sure you can still understand the class library? Can it also ensure that technology is in line with international standards?

The rest is just personal preference. To be honest, there is nothing wrong with writing it like this

Ty80

That’s a lot of experience. There are really weird things everywhere. Which company dares to name the person in question? Let's hide!

黄舟

I just translated the names. These names should be understandable even if they are written in English.

Ty80

Personally speaking, at least typing English code is faster than Chinese.
You can’t tell anything with a little bit of code. If a project is all in Chinese, you can imagine how much time is wasted

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template