Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

About naming #27

Open
bubnenkoff opened this issue May 8, 2016 · 10 comments
Open

About naming #27

bubnenkoff opened this issue May 8, 2016 · 10 comments

Comments

@bubnenkoff
Copy link

For me naming looks very strange. For example toJSON Converts the given JSON document(s) to its string representation, but by it's name I expect that it should convert string to json.

@wilzbach
Copy link
Member

toJSON is a rather common name. That's the name in JSON's origin language JavaScript uses it too, but I agree it's rather confusing.

Imho ideally it would just extend std.conv, s.t. we can use ``myjson.to!stringandmystr.to!json`.

@s-ludwig
Copy link
Collaborator

The problem is that since JSONValue forwards all methods/operations to the contained value, this would create an ambiguity (i.e. do you want the string value of the contained value, or the JSON string representation of it?). toJSONString would be unambiguous, but also quite long.

The other alternative would be to not forward operations to the contained value, which makes working with JSONValue quite a bit more convoluted.

@zoujiaqing
Copy link

change toJSON() to toString()

@zoujiaqing
Copy link

change JSONValue to JSONObject ?

@s-ludwig
Copy link
Collaborator

The problem is that many of these functions are highly ambiguous. Does toString() convert the represented value to string, or the whole thing to a JSON string? toJSONString would be the clearest choice, the trade-off is its length. But JSONObject would be the wrong term, because a JSONValue can also contain numbers, strings, arrays and Booleans in addition to objects.

@bubnenkoff
Copy link
Author

bubnenkoff commented Oct 11, 2016

@s-ludwig length is not so much problem, it's really better to add several letters, than get problem when people do not understand what function do by it's name.

What do you understand by whole thing to a JSON string ?

@zoujiaqing
Copy link

can use toJsonString() and toJsonValue() names.

@s-ludwig
Copy link
Collaborator

The problem with "Json" is that this needs to adhere to http://dlang.org/dstyle.html, which specifies that acronyms must be written all-uppercase. But I agree that it reads a lot mode lightweight. For that reason I've used another rule for vibe.d, where acronyms that are pronounced as a single word are still written in camel case, but that can't be applied here.

length is not so much problem, it's really better to add several letters, than get problem when people do not understand what function do by it's name.

I agree, but there have been strong opposing voices in the review process. It was argued to drop "JSON" from all names, which would result in code such as Value val = parse(str); that is completely opaque. IMO it doesn't make sense to try to safe a few letters for code that is relatively rare, at the expense of requiring the reader to scan for imports to even get what it does.

What do you understand by whole thing to a JSON string ?

toJSONValue("\"foo\"").toString() could yield either "foo" or "\"foo\"". Since JSONValue is a transparent algebraic data type at its core, it's not all that clear what should happen. The same goes for to!T integration. The problem is that the kind of algebraic data type that forwards operations to the stored type can be very convenient, but it comes at the cost of introducing some ambiguity/the need for more explicit names.

@bubnenkoff
Copy link
Author

bubnenkoff commented Oct 11, 2016

@s-ludwig I can't remember situations where additional quotes like "\"foo\"" was helpful. In most cases they only add problems and different kind of errors.

Maybe it's better to make generation of "foo" by default?

Sometimes it's very hard to understand where I should to use std.conv and where tail toString method

@s-ludwig
Copy link
Collaborator

Well, "\"foo\"" would be valid JSON syntax, whereas "foo" would just be the string contents itself. The former is what toJSON does. toJSONString would make that really clear, but for toString you can't really tell up front which of the two will be returned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants