Maximum Method Name Length
Asked Answered
A

8

44

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?
Alfonsoalfonzo answered 8/1, 2009 at 21:15 Comment(5)
I would love to know why you need the answer to thisArvillaarvin
Lol, curiosity mostly. A colleague and I were writing some unit test methods and one was particularly verbose (well under any limit), but we became intrigued as to what the maximum length allowed was.Alfonsoalfonzo
OK - just checking - I was figured it was something like that. The only time I ran into anything like this was the stupid warning messages in VC6 for debug builds that had STL - the symbol names were longer than 256 chars or something so they would be truncated.Arvillaarvin
@Tim: I must suffer through tens of thousands of those warnings every day...Punkie
I have one project where I have seen error messages with mangled symbol names long enough to flush a 5000 line scroll back buffer. But that is Uber-template meta stuff.Rhetorical
C
37

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.

Christman answered 8/1, 2009 at 21:20 Comment(2)
Your answer seems to contradict Kirill Osenkov's and DLeh's answers of 511. It would nice if you had a reference for your C# claim as I believe you are wrong here...Henryson
@SerjSagan: I've just tried it and it's larger than 511, although there is a limit. I'd say it's still larger than the bounds of readability and sensible machine-generated names though. I'll edit with the precise limit at this point in time...Christman
K
20

PHP seems to be limited only by the script's memory limit.

With 128Mb I was able to create a class (and method) with 4 million characters.

<?php
ini_set('memory_limit', '128M');
$i = 1024 * 1024;

while ($i < 10000000)
{
    $className = str_repeat('i', $i);
    eval("class $className { public function $className() { echo '$i<br>'; } }");
    new $className();
    $i *= 2;
}

?>
Keese answered 8/1, 2009 at 21:33 Comment(0)
S
18

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;
Sottish answered 28/3, 2011 at 19:10 Comment(0)
D
13

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.

Daw answered 15/4, 2010 at 23:35 Comment(1)
Your answer seems to contradict John Skeet's answer. It would nice if you had a reference for your C# claim of 511Henryson
D
4

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.

Decided answered 8/1, 2009 at 21:43 Comment(0)
L
2

in Progress (OpenEdge) the limit is 32 char.

Lase answered 28/3, 2012 at 15:19 Comment(0)
R
0

In D I don't know this to be the case but I suspect that it is something insane like >100MB. It might be an out-of-memory thing. This is based on knowing that I and other people have run into object file format limitation of about 11kB for symbol names and that this has been fixed.

Rhetorical answered 8/1, 2009 at 21:55 Comment(0)
P
0

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.

Plead answered 30/1, 2009 at 18:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.