应用程序中所有语言的名称大小写约定

Name casing convention across all languages in an Application

我正在使用 Javascript(在浏览器上)、C# 和 Sql 服务器构建一个网络应用程序。在所有三种语言中采用一致的命名约定是个好主意吗?
对于 Javscript,最好的做法似乎是使用 camelCase,但在 C# 和 Sql Server 中它是 TitleCase。
但问题是,当 Javascript 使用数据并将数据发送到 C# / Sql 服务器时,命名约定不一致。所以我必须编写代码来映射它们。

例如

In Sql I have a table called 'Foo' with columns 'Name', 'EmailAddress'

In C# I have an POCO object called 'Foo' that maps to this table with properties 'Name', 'EmailAddress' and exposes an api endpoint to GET this is json e.g {"Name": "Joe Bloggs", "EmailAddres":"joe@test.org"}

My Javascripts makes an ajax call to GET this JSON and maps it to its own JSON object e.g {"name": data.Name, "emailAddress": data.EmailAddress}

column/property 命名约定之间的这种映射对我来说似乎很愚蠢,如果所有语言都同意大小写约定,则不需要。有更好的方法吗?

在我看来,您应该继续使用每种语言的标准约定。

好处:

  • 每个软件的一致性。
  • 对于您的开发伙伴来说更容易,因为他们不需要学习自制命名约定。
  • 在 SO 之类的地方,或者如果您决定实施在 SO 上找到的解决方案,与世界其他地方交流代码会更容易。

不要为了使语言保持一致而添加额外的约定。您的语言最终会以不相关的无意义规则结束,这会让将来使用您的代码的任何人感到困惑。

为了玩恶魔代言人,这里有一个不同意我的观点的观点,从 here 中窃取:

This is where you find the big issue with coding standards based on style - if your team doesn't write the entire codebase, then you're going to find you have mismatches with the other code's standard.

So, my advice is not to sweat it. As long as your code is clear, it really doesn't matter whether you use camel case, pascal case, or underscore style. Its more important that the code is readable.

You should never alter the style of the 3rd parties as that will make comparison with new versions impossible, so you have to stick with them. If you have 2 different-style libraries, you have no choice but to follow a standard that ignores code style. Its not that bad, if you're a good coder you can read any code style. If you're not, having a solitary style won't help you at all.

我将其包括在内是因为我认为它们提出了一个很好的观点,并且因为您是在团队环境之外编写自己的代码。但是,在业余项目中养成良好的习惯(或者更确切地说,避免不良习惯)总是好的,因为它们可能会贯彻到您编写 with/for 其他人的代码中,因此学习如何处理命名约定将是有益的如果您认为将来在团队中会 运行 遇到该问题,则在大型代码库上。

将它添加到您的应用程序启动中,它应该会自动将您的 GET 返回的对象转换为驼峰命名法。

无需纠结于任何映射或偏离每个环境的惯例。

var formatters = GlobalConfiguration.Configuration.Formatters;
var jsonFormatter = formatters.JsonFormatter;
var settings = jsonFormatter.SerializerSettings;
settings.Formatting = Formatting.Indented;
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();

详情请看这里:http://odetocode.com/blogs/scott/archive/2013/03/25/asp-net-webapi-tip-3-camelcasing-json.aspx