r/shittyprogramming • • Mar 28 '15

Real men hard-code their JSON

Post image
372 Upvotes

28 comments sorted by

94

u/Timbo_KZ Mar 28 '15

I don't get it.

Why bother hard-coding JSON, when you can simply send your array to JavaScript using this simple trick:

var array = <? $array; ?>;

When the array is in JavaScript, simply JSON.parse() and alert() it, then use JNA to take a screenshot of the screen and save the popup content into a text file using Java's native String.fromWindowsScreenshot.

All that's left now is just reading text file's content using PHP, just keep in mind you'll have to set a timeout of 2 to 3 days if your user is using IE.

42

u/TheOnlyMrYeah Mar 28 '15

Jokes aside, String.fromWindowsScreenshot would be useful.

28

u/tyteen4a03 Mar 28 '15

OCR built right into Java, fancy!

1

u/RenaKunisaki Mar 28 '15

IIRC Windows API does let you read the text of controls on other windows (unless newer versions changed that), so it'd be possible to at least extract strings from a visible window.

5

u/TheOnlyMrYeah Mar 28 '15

Yeah, they're some useful tools out there which probably use that. But they won't help if someone sends you a screenshot of the error message.

65

u/MrVallentin Mar 28 '15

Why not just use json_encode?

Note to self, don't start writing a comment on /r/shittyprogramming before you've read the whole comment your commenting on... *facepalm*

0

u/[deleted] Mar 28 '15

[deleted]

8

u/Timbo_KZ Mar 28 '15

Nope, I mean what I mean. Additionally, adding an extra = will require your server to spend priceless picoseconds interpreting it what will result in noticeable increase in page loading time.

10

u/McGlockenshire Mar 28 '15

Not to defend this too much, but this was a common pattern before JSON was called JSON and it became trivial to call json_encode or even use Services_JSON from PEAR. On the other hand, if that code is less than five or six years old, there's no excuse. All of the code in our shitty codebase that does this is 10+ years old, and we haven't had enough round tuits to fix it yet. At least it's stupid instead of broken.

15

u/tyteen4a03 Mar 28 '15 edited Mar 28 '15

This codebase is from 2014. My company outsourced this project to another local company because of lack of personnel, and their code was beyond shitty. I only had to touch this because we're rewriting this in Go.

Sweet times.

2

u/RenaKunisaki Mar 28 '15

Looks like they wanted to output those fields and only those fields, and couldn't be arsed to put the field list in an array for some reason.

4

u/tyteen4a03 Mar 28 '15

The array passed into the function contains exactly these fields.

They were shitty coders.

20

u/amdc Mar 28 '15

U avin a giggle ere u cheeky $cnt?

1

u/sugar_free_haribo Mar 29 '15

This looks like a toString method for logging/debugging purposes. Could use json_encode, but this is really not that egregious. It's pretty much how Java developers generally implement a toString.

1

u/tyteen4a03 Mar 29 '15

This is a part of the front-facing API.

1

u/smegul Mar 29 '15

Is there any purpose to the leading and ending

echo '';

1

u/tyteen4a03 Mar 29 '15

Good question. I think they thought that this would give them linebreaks.

1

u/whoisearth Mar 28 '15

:( I have to do this with XML because I'm parsing data from a 3rd party application and without access to their code I have to mirror what they provide. Sometimes you gotta do it :(

4

u/tyteen4a03 Mar 28 '15

Can't you just throw it into SimpleXML?

4

u/whoisearth Mar 28 '15

custom xml that involves multiple nested elements from quite a few tables in a database. I probably could, if given the time, figure it properly using the django rest framework serializers but end of the day it was much easier for me to make a template and just pass the data into there. Ugly as sin, but it works :)

1

u/hak8or Apr 04 '15

Excluding the code contents it self, my god this looks so damn beautiful! What theme/IDE/color scheme is this?

Sublime, VIM, what?

1

u/dhvl2712 Mar 28 '15

Something something butterflies...

1

u/flynnski Mar 29 '15

I've actually had to do something similar, because ColdFusion and (lack of) case sensitivity. I don't want to talk about it.

-7

u/rossisdead Mar 28 '15

Real men make giant screenshots of snippets of code.

8

u/wockets Mar 28 '15

Or use a retina display.

11

u/tyteen4a03 Mar 28 '15

Or use a Retina Macbook Pro given to him by his company.