function getCountryCode($countryName) {
$countries = array(
'Afghanistan' => 'AF',
'Åland Islands' => 'AX',
'Albania' => 'AL',
'Algeria' => 'DZ',
// and so on
);
return isset($countries[$countryName]) ? $countries[$countryName] : null;
}
When I asked it if there was a better way to do it after this output, it recommended using a database to store the country codes instead of hardcoding an array, as well as recommending a specific php library that also provides this functionality.
Certainly a little better, at least O(1) look up time. I am still surprised it's not recommending moving these to an external file especially where something like localization is actually important in country names so they appear appropriately to the audience consuming them. I still don't think I'd stick this in a database :)
I don’t understand, there’s 164 countries in the world with corresponding codes. Whatever the prompt was it’s setting a bad example by starting this way. Also chatGPT isn’t even supporting the same countries as the original post. However I guess if you asked ChatGPT to indent the code, not improve the efficiency and support only a subset of the countries it’s doing wonderfully.
The problem isn't the indents, the problem is that 164 if else statements is entirely unmaintainable. Store that as a json document or something and parse that, so that 1 messed up copy paste doesn't break your country code getter and so you can easily add support for other languages
Yeah, the only way is to write an entire json parser. There's no other way to achieve what I suggested, unfortunately, so you better read up on the json spec, because no one else is gonna do it for you.
And unfortunately you'll have to write a json formatter and schema validator too, since you have to worry about how maintainable your json source is. Not the source code, of course, just the data that you're storing in the json file (and it has to be json).
But I think once you've written those tools, you'll realize the genius of hundreds of if/elses, and wish you had just stuck with one single file full of copy pasta.
If you're the one who wrote it, then yes. If not, then anyone who's had to witness that code has already gone through an eternity of hell so they get a free pass to heaven no matter what
It's fine, just comment a vim macro above that handles the closing side of the pyramid, and comment each line with a little bit of control data for those vim macros to work with and a quick description that overall equals the length of the code on that line, but is spaced to where it makes a huge X instead of a pyramid, because having a huge X in your code would rule.
They never start the if body with opening curly braces, so no. This is just single line statements, so a formatter would put all if expressions on the same indentation.
52
u/[deleted] Jan 27 '23
ChatGPT answer:
}
So technically, this developer can actually be replaced by ChatGPT
66
u/QueenTMK Jan 27 '23
You know the code is deathly bad when it's starting to look like the stairway to heaven
12
u/robin_888 Jan 28 '23
Wait... why are the
else ifs indented in the first place..?10
u/writetehcodez Jan 28 '23
I’ll take “I don’t know what a data structure is” for $500, Alex.
15
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jan 27 '23 edited Jan 28 '23
Image Transcription: Code (1/2)
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
7
u/[deleted] Jan 27 '23
It's fine, just comment a vim macro above that handles the closing side of the pyramid, and comment each line with a little bit of control data for those vim macros to work with and a quick description that overall equals the length of the code on that line, but is spaced to where it makes a huge X instead of a pyramid, because having a huge X in your code would rule.
2
u/Brnit9999 Jan 28 '23
Can we take a moment and wonder why on earth New Zealand is Neusseland in German, I suppose?
1
u/TariqSendi Jan 28 '23
I normally have a JSON file that I call and decode into an array then I look whatever is missing. Is there any other way?
1
u/nerokae1001 Jan 28 '23
This is not pyramid but a well implemented waterfall model! The devs understand clearly how the project manager want things to work on their terms.
1
u/MineKemot Jan 28 '23
Couldn't you use some kind of dictionary? I don't know PHP that well, if there's a feature like that it would be the best option.
1
u/dandigangi Jan 28 '23
At least the indentations are there 🤣
1
u/[deleted] Jan 28 '23
Is it generated code? Is it made up or real world code? The problem with this sub is that most post can be made up.
1
u/Grubzer Jan 28 '23
Hi huys! Good news, we are going international! So could you add 2 more languages to our ui, please?
1
u/Kuroodo Jan 28 '23
Whoever wrote this clearly misunderstood the meaning behind the phrase "programming is art"
1
u/Enjoi_the-Dev_Nerd Jan 29 '23
It’s…it’s beautiful..
1
u/AshPoppet Jan 29 '23
Help
1
u/FTWGaming0 Jan 30 '23
Dictionaries: Me personally, I would never take this level of disrespect.
1
u/[deleted] Feb 08 '23
Paid by the line
1
u/nicpro85 Feb 10 '23
fake or autogenerated
1
u/zgeon Feb 26 '23
thats stairs
1
u/[deleted] Mar 04 '23
This is the programming equivalent of that infomercial where the woman opens the cupboard and everything falls out on her.
“There must be a better way”
1
u/fnieidhfenmbvfekh Mar 24 '23
This is why I don’t indent my code.
0
u/[deleted] Jan 28 '23
[deleted] — view deleted comment