Counter-terror police make further arrest of man, 37, in rural Devon after spate of attacks on Jewish synagogues
CodeSOD: The JSON Template
We rip on PHP a lot, but I am willing to admit that the language and ecosystem have evolved over the years. What started as an ugly templating language is now just an ugly regular language.
But what happens when you still really want to do things with templates? Allison has inherited a Python-based, WSGI application which rejects any sort of formal routing or basic web development best practices. Their way of routing requests is simply long chains of "if condition then invokeA elif otherCondition then invokeB". Sometimes, those conditions will directly set the MIME type on the HTTP response.
They do use a templating library called Mako for generating their responses. They use it for their HTML responses, obviously. They also use it for their JSON responses, generating code like this:
{ "success": true, "items": { %for item in items_available.keys(): "${item}": ${items_available[item]}${',' if not loop.last else ''} %endfor } }The %for and matching %endfor mark the Python code off, which generates JSON via string-munging, complete with the check to make sure we're not on the last iteration of the loop.
Like so much bad code, this offers a degree of fractal wrongness. Instead of iterating over the keys and fetching the items inside the loop, you could iterate for key,value in items_available.items()- and according to the Mako docs, that for is just a regular Python for loop. That we're just outputting the contents of the dictionary is itself potentially a problem- sure, if we know the types of the dictionary, we'll know that whatever it is can be output in the body of a JSON document, but do we really think this code is using type annotations? I don't. And for a RESTful web service, I'm always going to feel weird about using a success field when ideally the HTTP status code could convey most of that information (and yes, I know there are reasons to still put status in the body, I just hate it).
Of course, the real issue is just: Python's built in JSON serialization is actually pretty advanced. And performant! You don't need any of this, you could just do something like:
return json.dumps({"success": true, "items": items_available})No templates. No formatting. No worries about how the data gets represented. Well, still worries, because JSON serialier will throw exceptions if it doesn't know what to do with a type. But then at least you get that exception on the server side and aren't sending the client a malformed document.
In any case, this is a good demonstration that you can write bad PHP in any language.
Sydney Sweeney's wedding ends in bloodshed as fans blast ANOTHER 'cringe' BDSM scene with topless Hunter Schafer in shock new Euphoria episode
After a week on the campaign trail, GUY ADAMS finds Labour's 27-year grip on Wales is doomed - and guess who is to blame...
Adam Peaty joins wife Holly and father-in-law Gordon to show his support for the Ramsays at London Marathon as his family feud rumbles on
Karoline Leavitt shares dramatic behind the scenes photos in the Oval Office after gunman opened fire at White House Correspondents' Dinner
Residents 'outraged' after 8ft-tall parcel locker is randomly 'plonked' on quiet cul-de-sac
My old school taught us Girl Power. Now it's the latest victim of Labour's VINDICTIVE CLASS WAR
Public told 'plan ahead' as GP surgeries close over May bank holidays
The Morning Poll: Are you nervous about the King's safety in America?
Greens accused of picking candidates who call for more 'compassion' despite brazenly promoting Jewish slurs
Jennifer Lopez, 56, shows off her sculpted abs in the gym: 'Rise and grind!'
Who Wants To Be A Millionaire: All 15 questions answered correctly by show's seventh winner - but how many can YOU get right?
Man living with daily 'burning pain' following 'unnecessary' amputation
Lindsey Vonn spotted in wheelchair after opening up on mental health battle following sickening Winter Olympics crash
Paranormal activity, debunked: Scientists claim ghost sightings can be explained by vibrations in old PIPES
Dad turns 'lockdown dream' into busy burger bar named after his son
Kris Jenner, 70, shows off her VERY taut visage during trip to an LA spa after $100k facelift
Pope Leo XIV's openly gay designer reveals what the pontiff's clothes secretly say about him
Right-to-Repair Laws Gain Political Momentum Across America
Read more of this story at Slashdot.