冷算法:自动生成代码标识符(类名、方法名、变量名)

竟然有小伙伴喜欢在编写代码时使用随机字符当作类名、方法名、变量名,例如这一篇博客里的代码:使用 Resharper 特性 - 林德熙。既然随机,那也随机得像一些啊!于是我改进了标识符的随机算法,使得生成的标识符更像真实单词的组合。


看看标识符的生成效果吧!0、2、4……行是 PascalCase,即首字母大写的;1、3、5……行是 camelCase 即首字母小写的。

自动生成的标识符
▲ 是不是感觉甚至能读出来?

嗯嗯,因为生成规则中考虑到了辅音和元音的组合,而且……嗯……还考虑到了部件出现的概率。

比如一个单词中的音节数,单音节概率 44%,双音节概率 31%,三音节概率 19%,四音节概率 6%。而这样的概率是通过一个幂函数来实现的。具体来说,是下面这个函数:

音节数

好吧,把我的源码放出来:

public class RandomIdentifier
{
    private readonly Random _random = new Random();

    public string Generate(bool pascal)
    {
        var builder = new StringBuilder();
        var wordCount = _random.Next(2, 4);
        for (var i = 0; i < wordCount; i++)
        {
            var syllableCount = 4 - (int) Math.Sqrt(_random.Next(0, 16));
            for (var j = 0; j < syllableCount; j++)
            {
                var consonant = Consonants[_random.Next(Consonants.Count)];
                var vowel = Vowels[_random.Next(Vowels.Count)];
                if ((pascal || i != 0) && j == 0)
                {
                    consonant = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(consonant);
                }

                builder.Append(consonant);
                builder.Append(vowel);
            }
        }

        return builder.ToString();
    }

    private static readonly List<string> Consonants = new List<string>
    {
        "q","w","r","t","y","p","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m",
        "w","r","t","p","s","d","f","g","h","j","k","l","c","b","n","m",
        "r","t","p","s","d","h","j","k","l","c","b","n","m",
        "r","t","s","j","c","n","m",
        "tr","dr","ch","wh","st",
        "s","s"
    };

    private static readonly List<string> Vowels = new List<string>
    {
        "a","e","i","o","u",
        "a","e","i","o","u",
        "a","e","i",
        "a","e",
        "e",
        "ar","as","ai","air","ay","al","all","aw",
        "ee","ea","ear","em","er","el","ere",
        "is","ir",
        "ou","or","oo","ou","ow",
        "ur"
    };
}

而使用方法,则是简单的一个调用:

var identifier = new RandomIdentifier();
var pascal = _identifier.Generate(true);
var camel = _identifier.Generate(false);

传入 true 生成首字母大写的版本,传入 false 生成首字母小写的版本。

本文会经常更新,请阅读原文: https://walterlv.github.io/post/algorithm-of-generating-random-identifiers.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

知识共享许可协议 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 吕毅 (包含链接: https://walterlv.github.io ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系