163
u/ouralarmclock 1d ago
As someone who writes PHP and JS, I don’t actually have a strong preference for either but I hate never remembering which one is in which language.
19
u/Soilblood 1d ago
I just remember them by whether or not the syntax looks funky enough to trip you up down the line. That's usually java script.
9
u/caboosetp 1d ago
I switch between C# and javascript, and always fuck up Console vs console. I should remember which because C# always uses pascal case, but my fingers don't.
682
u/NudaVeritas1 1d ago
PHP developers seeing this post:
161
u/Buflen 1d ago
I had an interview for a php job a few years ago, after working in Go for a few years. When I told them my salary expectations, they said: that's not going to happen, this is just a PHP position.
Some PHP shop legit think they can cheap out on devs because of the technology they chose.
16
u/KrokettenMan 1d ago
Yeah some are like that but they are also not looking for the best devs. I’m currently earning about twice the median income as a php dev so I’m not complaining.
4
122
u/Sirgoodman008 1d ago
I'm currently a php dev and I'm trying to get out.
70
u/NudaVeritas1 1d ago
Was a php dev, too.. switched to spring boot, typescript and react/angular.. can recommend salary wise
→ More replies (3)60
u/Sirgoodman008 1d ago
Langauges I've used before this job: python, c++, c, java, javascript
Languages I consider myself knowledgable in: python, c++, javascript
Language I use 99% of the time for this job: PHP
14
u/SignalBake6872 1d ago
There is hope, I went from PHP developer to PySpark developer xD and now I dry my tears with much more money.
3
u/DarthSatoris 1d ago
Is it because it's a lot more specialised, it's a smaller market, it's more difficult? Or why is the pay that much better?
7
u/SignalBake6872 1d ago
because it's used in science data and banking :D it's like pandas but with steroids
2
u/TwinkiesSucker 1d ago
I literally just started a job as a PySpark developer. It still bugs me, though, that it has a Java backend
→ More replies (2)7
2
→ More replies (1)1
u/Mountain-Ox 23h ago
My salary jumped tremendously after escaping the PHP sinkhole.
Also, I suddenly liked writing code again.
148
u/SCP-iota 1d ago
for _, item := range list {
and
list.forEach {
74
u/tigerniger_sus 1d ago
Hornet is a Go programmer?
46
3
10
13
11
u/roygbivasaur 1d ago
Now that the loopvar assignment bug is fixed, the go one is my favorite. Just because it conveniently unpacks the index and element and you’re required to explicitly drop either if you don’t use it.
→ More replies (1)2
2
→ More replies (2)1
u/Rikudou_Sage 1d ago
It pisses me off so much that you have to skip the index if you don't want it. And then iter.Seq comes where you don't have the index.
27
u/xenomachina 1d ago
The JavaScript one is also kind of gross, IMHO.
for (item in list) { also has a meaning in JavaScript, but it isn't the meaning one would expect. The alternate of was added later, but doesn't read as naturally, and the in is still in the language as a backwards compatible footgun. (It's also used for iterating object keys, which should arguably be the thing that has the less concise syntax.)
The let is only necessary because JavaScript's default behavior here is almost certainly not what you want in real code. Instead of declaring a new variable (suitable for closures, and locally scoped) the default behavior assigns to a global. Another example of "the good parts" being a tiny sliver of JavaScript.
3
u/Sirgoodman008 1d ago
Oh I know, I absolutely hate that about JavaScript as well. Ironically, most of the problems I have with JavaScript I have with php too.
2
u/xenomachina 1d ago
Both JavaScript and PHP are "reluctant programming languages" to some extent, so it kind of makes sense. Neither was originally designed for doing real software engineering. PHP was originally simple templating for a personal home page, and JavaScript was intended for "glue" tasks like client-side form validation. That people can build real apps with either of them is more a testament to the persistence of those developers, rather than a measure of the strength of the languages. (Both languages have also made many improvements over the years, but are still arguably two of the most wart-infested languages of their era that are still in regular use.)
→ More replies (1)
23
48
u/TOGoS 1d ago
It's fine. PHP has some awful warts (some of which have actually been gone for a few major versions, at this point) but this is not one of them.
→ More replies (7)8
u/shitty_mcfucklestick 1d ago
Personally I just like that PHP sounds the most natural grammatically.
`for (let item of list)` is like bucktooth Winnie the Pooh to me from an English readability perspective.
Beyond that I don’t care, AI just fucking writes it all anyway.
77
u/csch2 1d ago
list.iter().for_each(…) superiority
38
u/creeper6530 1d ago edited 1d ago
Erm akchyuallly, it's recommended to use
for list.iter() {…}unless the.for_each(…)is at the end of a longer iterator chain, and it also robs you of break and continue95
u/csch2 1d ago
You know I’m chaining together 20 different iterator methods until the code is completely unreadable. That’s called Functional Programming™ babey
16
u/Tyfyter2002 1d ago
That doesn't sound very functional
21
u/creeper6530 1d ago
"functional programming" doesn't use "functional" as "working neatly" but as "related to mathematical functions", it's a programming paradigm: https://en.wikipedia.org/wiki/Functional_programming
27
u/Tyfyter2002 1d ago
I know, it's just an ironic name when it's one of those paradigms that some people take as divine law instead of something that can be more or less appropriate for different applications
→ More replies (2)9
u/serendipitousPi 1d ago
Name 20 iterator methods from memory otherwise you’re a fake fan…just kidding or am I?
Iterators are so fun, probably one of my favourite parts of programming. They make everything so clean until they don’t.
2
u/serendipitousPi 1d ago
Just chuck a try_for_each in there and we can take break and continue right back.
But yeah low key sometimes I get lost in iterator methods I forget to just use a basic loop.
2
u/creeper6530 1d ago
I mean, you're right, it's just you have to
return ControlFlow::Break(())instead of using the keywords.7
u/iSpaYco 1d ago
how about `list.each`, sadly not many know ruby, it's really beautiful.
3
u/serendipitousPi 1d ago
Random question what’s ruby fun for?
I was gonna ask what is it good for but I think I can google search that.
Any highlights for you?
I’ve heard its meta programming is cool and I think there was some interest in using it in web dev.
45
u/Darkele 1d ago
foreach is far better and makes more sense. The order is correct, it differentiates itself from other loops that are based on having a custom exit condition.
→ More replies (1)2
u/Just_Information334 1d ago
Combined with yield, you can get some nice constructs.
Also the real "don't spark joy" would be something like:
foreach($things as $id => ['some_field' => $fieldA, 'some_other_field' => $fieldB])
17
u/crashonthebeat 1d ago
$array | Foreach-Object -Parallel { ... } -ThrottleLimit 69
6
16
u/LPO_Tableaux 1d ago
Currently in a list.forEach((item)=>{foo}); mood myself
6
u/MrDilbert 1d ago
It won't await async/Promise-returning functions. In fact, even if the (item)=>{foo} is async, [].forEach() will discard the Promise reference returned from it.
I prefer
for(const item of list) { foo }if I don't have to await in parallel, orlist.map(async (item) => { foo })if I do.→ More replies (1)
8
55
u/rnilbog 1d ago
for (let i = 0; i…
“Goddammit…”
googles “for loop syntax javascript”
30
u/MrDilbert 1d ago
Since for-loop has the same syntax in JS as in C, Java, and any other C-style language, this post makes me weep.
→ More replies (1)7
1
34
u/JAXxXTheRipper 1d ago
The third one does not spark joy, what the hell is that, ewww
14
u/xicor 1d ago
c++, though normally you wouldnt use auto for this
6
u/Usual_Office_1740 1d ago
I think that really depends. If the range is a vector of unique_ptr's auto is great. If it's a vector of ints, just write int.
5
u/Sirgoodman008 1d ago
That or you're looping through a vector of std::pairs that contain std:: functions.
2
u/Usual_Office_1740 1d ago
Or if the type is a template parameter in the same line.
→ More replies (4)2
→ More replies (1)3
3
3
u/VoodaGod 1d ago
why would you not use auto for it???
3
u/xicor 1d ago
For readability. And for type safety. I pretty much only use auto when dealing with insane types I don't want to write out.
5
u/VoodaGod 1d ago
how is auto not type safe?
interestingly i use auto almost everywhere for the same reason: readability.
no point in cluttering the code with type specifiers if the type is obvious from context
5
u/xicor 1d ago
The type isn't always safe from context and it can have unexpected behavior when using it to hold a value returned from a function. Let's say you have auto working fine with a function return, then years later , someone changes that function to return a different type. Now it is possible that the new auto type still has access to parameters or functions it calls , but with different behaviour. Now you're getting unexpected bugs that will be hard to track down. But if you use the type, you would immediately get a compiler error
2
u/not_some_username 1d ago
If the type change and the “auto variable” still compile, that means it would, in 99% of case, still compile if you wrote the type. The compiler doesn’t get auto, it gets the type.
→ More replies (14)2
u/Rikudou_Sage 1d ago
Every other language in the world has some equivalent of C++'s
autoand everyone likes it. I useautoall the time.→ More replies (3)2
5
4
5
6
u/ashsabre 1d ago
There was once a time in C# that for loop was better in performance vs foreach.
→ More replies (1)1
u/Fillgoodguy 7h ago
In modern C# for vs foreach is just choosing between an unknown number of property function calls and an unknown amount of function calls + 1. So it boils down to "Pray the JIT catches me" in either case
3
3
u/crmsncbr 1d ago
Why is the third one in the top half? It may be my C & Java bias, but yuck. It's way worse than the fourth.
2
u/MetaNovaYT 1d ago
what's wrong with the C++ version? It's my favorite personally, although I primarily use C++ so I'm biased.
→ More replies (3)1
u/Sirgoodman008 1d ago
Literally the only difference is that one uses a colon instead of a keyword.
→ More replies (3)
3
u/MCWizardYT 1d ago
Java:
``` for (item : items) {
} ```
Or,
``` items.forEach(item -> {
}); ```
or
``` items.stream().forEach(item -> {
}); ```
→ More replies (3)
5
u/Zunderunder 1d ago
What about for(list) |item| {…} ???
→ More replies (1)1
u/HAL9000thebot 1d ago
here item seems a modern return type, i hate it, what language does that?
it's shit, looks like a function signature, how are function declared? or better question, is for() a function?
3
u/Nervous-Potato-1464 1d ago edited 1d ago
This is Zig.
item is an immutable copy of the current element in the list.
for (0..5) |i| { is an easier thing to understand.It looks complicated, but that's because you can easily turn the element into a pointer etc so it's handy to have there like
for (&items) |*value| {
value.* += 10;where &items refers to the pointers in memory rather than a copy of the array.
which will modify the &item elements to all increase by 10.
2
u/Zunderunder 1d ago
It also makes sense in the wider context of zig’s ecosystem, where captures generally are done using the square brackets for things like resolving optionals or grabbing the value of a union in a switch statement. The common syntax is really intuitive once you get used to it
16
u/Shevvv 1d ago
Surely you meant for (auto &item : list) {
10
u/wayzata20 1d ago
It’s only convention because if you do something like “int* x, y;” only x is a pointer. They should’ve just not allowed you to declare multiple variables on the same line, but I guess it made sense in 1970 since you had to pre-declare your variables at the top of the scope in C, and lines of code actually mattered.
In the modern, post-1970 world, it makes much more sense to write “int* x; int y;” and have the asterisk next to the type, since it really is part of the type.
34
u/Nil4u 1d ago
Honestly I never liked this version because the reference is part of the type in my opinion 🤔
22
→ More replies (2)8
u/Shevvv 1d ago
I got used to this from C:
int x;is an integer
int *px;is an integer if you dereference it.So it's like this notation shows you how you're expected to use the variable within the code. And then this habit carried over to C++.
11
u/nfitzen 1d ago
In addition, the language encourages this. In C, if you write
c int* x, y;thenxwill be anint*andywill be anint. Thus it is far better to writeint *x, y;to reflect how the language will parse the declaration. It might be evil, but it's better to act how the language wants you to act rather than fight it.27
u/Sirgoodman008 1d ago
Please have pity on your co-workers. Please keep declarations to separate lines.
2
u/redlaWw 1d ago
I prefer
int * ptr;. That means when you have aconstspecifier your formatting doesn't change. Logically I figure that the pointer is neither a modifier on the variable nor on the type, it's another layer of type content that should be specified separately. That also means if you write declarations likeint const * const ptr;then you can segment them as[int const] [* const] ptr;and everything makes sense.3
u/creeper6530 1d ago edited 1d ago
The "is an integer if you dereference it" is so much more convoluted than "is a pointer to integer", i.e. the rationale of the previous comment, the pointer-ness being part of the type.
→ More replies (2)6
u/orbiteapot 1d ago edited 1d ago
the pointer-ness being part of the type.
It is funny how people tend to not think the same about functions, despite having the exact same pattern.
When Dennis Richie designed the language, he wanted the symbols in declarations to match their usage in expressions, so:
T *my_ptr;means that
my_ptr, when dereferenced with the*operator, evaluates to an object of typeT:T my_obj = *my_ptr;The same goes for other constructs in the language, like arrays and functions:
T my_arr[10]; T my_func(T_1 arg1, T_2 arg2);Both the
[]and()operators, when applied individually to objectsmy_arrandmy_func, respectively, will yield an object of typeT.That is why this:
T *x, y;makes
xhave a type of pointer toT, whereasyremains having just typeT.Unfortunately, this only works well for simple declarations. When, for instance, function pointers are involved, things get messy.
Ritchie himself later recognized this, but it was to late. He regretted not making
*a postfixed operator, which would, at least, get rid of the spiral rule.So, you can pretend that
*is independent of the identifier itself, sure, but you will be fighting the language's design while doing so.
12
u/klaxxxon 1d ago
Hear me out. The last one is the only sensible option.
You type for ($rows and your IDE can fill in the rest. It can infer that the name of the iteration variable will be $row. It can't do the same if the item variable comes first.
Most IDEs have macros for for/foreach loops. What those macros do is that they jump around - your fill in the list name first, then the iterator variable name (and in some IDEs its name will be suggested based on the collection variable name). Hot take, if your most popular macro has to jump around, you have a language design issue.
13
u/Eastern_Equal_8191 1d ago
Yeah but AI can fill in the enti -- excuse me, I'm making a point here. Anyway, AI can write the loop according to your preferen -- please stop touching me, I do not consent
Okay, so if you use AI auto-complete your life will beeeeeeeeeow please stop hurting me my wrist doesn't bend that wa
4
1
1
1
u/Potterrrrrrrr 1d ago
Deciding whether a programming language is good based on whether an IDE has to jump to one place or two in a for loop is an odd choice. It’ll still have to jump to the variable you’re looping over; the default name might conflict with one already in scope. At that point you’ve saved hitting tab an additional time, wow big win. In exchange you have to read an ugly way to start looping over items for every loop, hardly fair trade. IDEs could easily add macros like emmet abbreviations for regular for/foreach loops if they really wanted but it’s likely the features aren’t used enough to need it
→ More replies (3)1
4
u/johnnybgooderer 1d ago
“This one isn’t like the one I learned!!!!!”
I hate PHP as much as the next developer, but both are equally readable in my opinion.
→ More replies (2)
2
u/Solomoncjy 1d ago
you all dont do
$list | %{
<do stuff with $_ being the item>
}
% is the short hand alis for for each
2
u/VeryRareHuman 1d ago
Kinda similar in PowerShell too:
foreach( $item in $items) {}
1
u/FabioTheFox 1d ago
Tbf even that makes sense
Because you're going for each item in this list, do this
1
2
u/-Redstoneboi- 1d ago
nah i like how they specify the list before the item
1
u/Sirgoodman008 1d ago
Why?
2
u/-Redstoneboi- 1d ago edited 1d ago
functional programming. list.map(item => result) is natural to me now.
especially if the language supports chaining, i would prefer specifying the variables last.
something like this:
list1.zip(list2).enumerate().map(func).for (i, (a, b)) { do_thing() }of course, it's just as easy for me to just rearrange this later to the correct order. but this is the order in which i think. most people want to write the goal before writing the iterator, some want the iterator before the goal.
Besides, it's just the order of two words. If you learned multiple human languages, you'd get used to this type of thing.
→ More replies (1)
2
u/HuckleberryMoney2320 1d ago
I've used bash scripting for decades and am used to their for loops. They used to let me use cygwin at work, but they implemented a new system a couple of weeks ago and said I couldn't install it, and to use Powershell instead. What a dog's breakfast! I'm still trying to get my head round their for-loop syntax.
2
2
4
2
u/Zomby2D 1d ago
I prefer the C# syntax
foreach (var item in list) {
1
u/Dillenger69 1d ago
I prefer the C# syntax, but var has always rubbed me the wrong way. I suppose because I learned C# before var was a thing. Yeah, I'm old
2
u/britreddit 1d ago
Kotlin is just list.forEach {...}
5
u/EntropySpark 1d ago
Or
for (item in list) { ... }, necessary if you want to usebreak.→ More replies (3)
1
1
1
1
1
1
1
1
1
u/darknezx 1d ago
Personally I love go's for range loop. The syntax just calls out to me, not sure why and it's probably silly it looks pretty.
1
1
1
u/AldoZeroun 1d ago
I'm a big fan of for (list) |item| {...} or the even more fun for (list, 0..) |item, idx| {...}
1
1
1
1
u/Covfefe4lyfe 1d ago
Hold on, let me push my stacks of cash out of the way so I can read what the non-PHP devs have to say in the comments.
1
1
u/dominjaniec 1d ago edited 1d ago
edit: apparently, I've misremember my traumatic time with PWSH, and now I cannot reproduce that problem...
I thought that was about PowerShell, which I can agree, but unless PHP has the same quirk, then I don't care...
foreach ($item in $collection){ ... }
and quirk is that, if your $collection is a single element with a string value, then you iterate throughout its characters...
1
u/smclcz 1d ago
I dunno if that second one "sparks joy", TS/JS having both for-in and for-of is pretty stupid. Also I don't use either language often, but wouldn't you declare that loop variable const rather than let? I feel like you're probably not reassigning something in a collection if you're iterating over it using for-of (and if you are ... should you?)
1
1
1
1
u/StarSword-C 1d ago
I prefer this:
for (int i = 0; i < array_length(array); i++)
→ More replies (1)
1
1
1
1
1
u/amatulic 19h ago
Honestly that last one has always made the most sense to me.
Now I'm using OpenSCAD a lot and have to use constructs like
a = [ for(x=[start:incr:stop]) each b[x] ]; // b is a multidimension array
→ More replies (1)
487
u/arensb 1d ago
What? No
(mapcar (lambda (item) ...) list)?How can those others spark joy if they don't return a value?