4 ms·
I haven't slept well lastnight (only 4 hours) so please bear with me... but what is the joke on this snippet? "I Don’t Know".ToJson(); public string ToJso
by pmelendez 13y ago
I haven't slept well lastnight (only 4 hours) so please bear with me... but what is the joke on this snippet?
"I Don’t Know".ToJson();
public string ToJson()
{
var s = new StringBuilder("{");
for (var i = 0; i < CustomField.CustomFieldOption.Count; i++)
{
var item = CustomField.CustomFieldOption[i];
s.Append("\"" + item.CustomFieldOptionId + "\"");
s.Append(":\"" + item.OptionName + "\"");
if (i < CustomField.CustomFieldOption.Count - 1)
{
s.Append(",");
}
}
s.Append("}");
return s.ToString();
}
- mephi5t0 13y agoI assume item itself is missing, so nothing to convert... My guess
- deleted 13y ago[deleted]
- pmelendez 13y agoI guess you are talking about: s.ToString(); Just note that s is a StringBuilder and not a string.
- potatolicious 13y agoBeen a while but: var item = CustomField.CustomFieldOption[i]; Shouldn't that be the item to convert? This looks like something stuck onto a class to serialize to JSON. I might be missing the joke also :(
- acron0 13y agoThere aren't any obvious structural errors but two things occur to me. Firstly, JSON serialization is something every platform, including .NET, already has a multitude of libraries available for so this is 'reinventing the wheel' a bit. Secondly, looking at what it's actually doing, it's plausible the author doesn't even need JSON, as it's simply serializing a list of `id:name` fields. Thirdly, there are plenty of more 'inline' ways of achieving this using `string.Join`. All in all, just very 'novice' code but nothing _incorrect_ about it. IMO.
- pmelendez 13y agoYeah, that's what I thought. I didn't see anything horrible that were obvious, and by horrible I mean laughable just as the other pieces of code in the website.
- iron_ball 13y agoI'm assuming they could have just used a well-known and well-tested library.
- jtheory 13y agoThere are easier ways to "JSONize" data, maybe? They aren't escaping the keys/values going in, which could be bad, but if it's a known set of strings that may not actually be a problem. I notice that the two pieces of data actually going into the JSON are "CustomFieldOptionId" and "OptionName" -- which seem somewhat useless/obscure, but there could be a legitimate reason for that. So yeah, I don't quite get it either. Most of the others are more obviously silly.
- philliphaydon 13y agoNope, 100% real. Copy pasted as is from a project I work on. Legacy code from 5? Years ago... No idea who the original author is. But it worked. It now uses newtonsoft.json library now. About 6 years ago I wrote something similar, but that was before newtonsoft.json was super popular.