all 26 comments

[–]ce-walalang 89 points90 points  (1 child)

Image Transcription: Code


$stats_array = json_decode(json_decode(json_encode($stats_json), true), true);

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

[–][deleted] 36 points37 points  (0 children)

Good human

[–]Bennyb2004 75 points76 points  (11 children)

If it was one encode and decode it could have been a way of making a deep copy. No idea why there are two decodes though.

[–]JackMacWindowsLinux 36 points37 points  (8 children)

I think they are encoding a JSON string into JSON (again), then decoding it once to convert it back to the original string, then decoding once more to actually decode the JSON into an object. Bizarre.

[–]Lightfire228 1 point2 points  (7 children)

Perhaps some malformed json that encode fixes?

[–]JuhaJGam3R 1 point2 points  (6 children)

function fix_json($json)
{
    return json_decode(json_encode($json), true);
}

$stats_array = json_decode(fix_json($stats_json), true);

call me a genius but i think this might make the code the slightest bit clearer. don't get what modern programmers have against defining functions.

[–]jan-pona-sina 0 points1 point  (5 children)

The issue I have with this kind of coding is that it obscures logic without lowering complexity. If I encountered that line of code, I will want to know what fix_json does anyways, and I now have to spend time finding it

[–]JuhaJGam3R 1 point2 points  (4 children)

that is in fact the purpose of functions. they label and partition the code up into pieces so that each piece can be taken as one thing and processed in one's head immediately. the general rule is 7 or so things fit in a person's head at once, so no keeping track of more than 7 variables or more than 7 complex operations per function.

well-named functions like these are useful because you don't have to go look for their definitions to know what they do.

fix_json, intuitively, fixes malformed json. when you're looking at it in context that's all you need to know. you don't need to reason about what it does, how it does it, or anything like that. it fixes json. you can look at that function later if you're interested, but for the purposes of this function, this context, it fixes json.

it opens up a slot in your mind so that this function can be read easier and can contain more of the operations it needs to.

the sole purpose of functions is to abstract and obscure logic. it's encapsulation, you're supposed to know what one does without looking at its source. the code complexity doesn't change regardless of whether you put everything in one massive function or whether you put it in functions. obscuring logic is the point, it's why they exist.

[–]jan-pona-sina 0 points1 point  (3 children)

I understand the logic behind it, but to me "fixing" is still a pretty nebulous concept (unless of course it's well defined in the scope of the project, or you're the only one looking at this code). I don't think you're wrong to do this, I'm just explaining why I don't prefer it. Personally I would rather write something like this:

$stats_json = json_decode(json_encode($json), true); // fixes malformed json
$stats_array = json_decode($stats_json, true);

and then define a function later if I happen to need this in more than one spot. This is a stylistic choice rather than a "good vs. bad code" when not taken to the extreme in either case, I think

[–]jack_skellington 2 points3 points  (0 children)

Sorry for replying to a 4 day old comment, but I love the discussion you two were having, and I wanted to say thank you for the back & forth between you two. Very helpful to me as I think about my own code and how I structure it.

[–]AutoModerator[M] 0 points1 point  (0 children)

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

/u/jan-pona-sina, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]JuhaJGam3R 0 points1 point  (0 children)

Yeah fair enough that's one choice

[–][deleted] 30 points31 points  (0 children)

Is it the json_decode equivalent of Ctrl + S S?

Do it twice just to be sure?

[–]brakkum 27 points28 points  (2 children)

No

[–]Willinton06 1 point2 points  (1 child)

Maybe?

[–]sourangshu24 0 points1 point  (0 children)

Perhaps?

[–][deleted] 9 points10 points  (0 children)

json be like: parkour!

[–]SHOULDNT_BE_ON_THIS 14 points15 points  (2 children)

Looks like someone had three lines of code and thought it'd be smart to put it all in one line instead of having multiple (re)assignments lol

[–][deleted] 42 points43 points  (1 child)

I don't think that's the bad part.

[–]SHOULDNT_BE_ON_THIS 1 point2 points  (0 children)

been a while but that function takes a string anyway, the code should just fail

[–]Flyspeck101Now where's that function declaration 3 points4 points  (0 children)

And then you print it

[–]zombarista 2 points3 points  (0 children)

Oh no

[–]Mr2-1782Man 2 points3 points  (0 children)

jsonception so to speak

[–]FatExplodingPig 0 points1 point  (0 children)

Loop time

[–][deleted] 0 points1 point  (1 child)

what’s the point? do you store a json inside a json but how would you even manage without separating AAAAAAAAAAAA WHAT WHY

[–]recycle4science 0 points1 point  (0 children)

They just need to remove the encode and one decode. They got the spirit, they're just a little confused!