Maximum Method Name Length

2019-01-08 19:37发布

Does anyone happen to know what the maximum length of a method name is in your programming language of choice? I was going to make this a C# specific question, but I think it would be nice to know across the spectrum.

What are the factors involved as well:

  • Does the language specification limit this?
  • What does the compiler limit it to?
    • Is it different on 32bit vs 64bit machines?

10条回答
We Are One
2楼-- · 2019-01-08 20:18

Interesting! In Java as others said there is no limit (I never thought about the length!) but I think you want to avoid having a long method name as this could effect readability and even mental mapping (any other dev looking at the code may forget or want to forget the method name!).

I believe "clean code" in a nutshell talks about the use of descriptive pronounceable names. You could also argue a long descriptive name is better than a short one, I guess a sound balance. tis my humble view.

查看更多
做个烂人
3楼-- · 2019-01-08 20:19

Common Lisp symbols's names are strings; strings have a length limit of array-dimension-limit

The value of array-dimension-limit is a positive integer that is the upper exclusive bound on each individual dimension of an array. This bound depends on the implementation but will not be smaller than 1024. (Implementors are encouraged to make this limit as large as practicable without sacrificing performance.)

In practice this can be quite large

Welcome to Clozure Common Lisp Version 1.3-dev-r11583M-trunk  (DarwinX8664)!
? array-dimension-limit
72057594037927936
? 

Welcome to Clozure Common Lisp Version 1.3-dev-r11583M-trunk  (DarwinX8632)!
? array-dimension-limit
16777216
? 

This answer ignores the method name's package name; this could double the lengh.

查看更多
【Aperson】
4楼-- · 2019-01-08 20:20

In Java, I believe a length limit is not given. See this from the online Sun Java Tutorial:

Variable names are case-sensitive. A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter...

Like others above, I would guess the length is dependent upon the available memory.

查看更多
迷人小祖宗
5楼-- · 2019-01-08 20:23

For C# I don't believe there's a specified hard limit. (Section 2.4.2 of the C# 5 spec doesn't give a limit, for example.) Roslyn v2.2.0.61624 seems to have a limit of 1024 characters; this is way beyond the bounds of readability and even a sensible machine-generated name.

For Java, section 3.8 of the spec states:

An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter.

查看更多
不美不萌又怎样
6楼-- · 2019-01-08 20:24

Microsoft's C# implementation is 511, VB.NET implementation is 1023.

Visual Studio will only colorize the first 511 (1023 for VB) characters of the identifier and keep the rest black.

查看更多
男人必须洒脱
7楼-- · 2019-01-08 20:25

I just did a test in C# Visual Studio 2010 (x64): made an identifier:

int a123456789a123...;

And repeated. At 512 characters, VS gives me the error "Identifier too long." 511 is fine though. (Checked character count in Word.)

Another example:

int whyintheworldwouldyoueverhaveanidenfifierthislongitsreallyjustquiteridiculousimeancmonyoucouldatleasthavethecommoncourtesyofmakingitcamelcasesoitsnotsohardtoreadcmonjuststopnowyourereallyreachingtomakethisaslongaspossiblearentyou123412341234alrightwellthatsenoughnowisntitwelliguessnotbecauseimstillgoingthisisofficallytheworstidentifiereverಠ_ಠokaynowthatithasunicodeitsofficialbutseriouslythisthingissolongthatihadtogetupinthemiddleofittotakeabreakbeforesittingdowntofinishtoppingitofftothemaxcharlimitof___511;
查看更多
登录 后发表回答