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?
Allan Bowe
  • 12,306
  • 19
  • 75
  • 124
Josh
  • 44,706
  • 7
  • 102
  • 124
  • 8
    I would love to know why you need the answer to this – Tim Jan 08 '09 at 21:21
  • 8
    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. – Josh Jan 08 '09 at 21:23
  • 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. – Tim Jan 08 '09 at 21:31
  • @Tim: I must suffer through tens of thousands of those warnings every day... – rmeador Jan 08 '09 at 21:50
  • 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. – BCS Jan 08 '09 at 21:58

8 Answers8

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 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... – Serj Sagan Jun 26 '17 at 16:27
  • 1
    @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... – Jon Skeet Jun 26 '17 at 18:13
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;
}

?>
Greg
  • 316,276
  • 54
  • 369
  • 333
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;
DLeh
  • 23,806
  • 16
  • 84
  • 128
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.

Kirill Osenkov
  • 8,786
  • 2
  • 33
  • 37
  • Your answer seems to contradict John Skeet's answer. It would nice if you had a reference for your C# claim of 511 – Serj Sagan Jun 26 '17 at 16:27
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.

Doug Currie
  • 40,708
  • 1
  • 95
  • 119
2

in Progress (OpenEdge) the limit is 32 char.

vince
  • 21
  • 1
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.

BCS
  • 75,627
  • 68
  • 187
  • 294
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.

Bill
  • 231
  • 1
  • 3