Anti-If Programming vs. Smelse

  Рет қаралды 3,680

Mark Jivko

Mark Jivko

2 ай бұрын

Grab your free Smelse Diploma here: markjivko.com/smelse/
"Trust me, I'm a Doctor" works for the Janitor in Scrubs. Really, such an awesome line!
Not really your best argument for polymorphism, though.

Пікірлер: 62
@deoxyde
@deoxyde 2 ай бұрын
I thought this was going to be something interesting like branchless programming, which is a legitimate use case for execution hot paths but it turned out to be just another guy pushing his horrible courses lol
@markjivko
@markjivko 2 ай бұрын
Me too! I was soo excited to discuss these things with the author himself on LinkedIn directly - only to be met with a very harsh appeal to Authority Bias. That was disappointing, indeed.
@xybersurfer
@xybersurfer 2 ай бұрын
yep that's also what i was expecting
@mzg147
@mzg147 2 ай бұрын
oh god I'm so glad Rust allows to break from any named code block, so you can use the Smelse methodology blazingly fast 🚀 (and without fake loops)
@markjivko
@markjivko 2 ай бұрын
I love Rust so much! Thank you for the idea, maybe I'll do a video on that soon.
@gr.4380
@gr.4380 2 ай бұрын
can't wait to hop on the smelse train like it's a new JS framework
@markjivko
@markjivko 2 ай бұрын
Don't forget your diploma: markjivko.com/smelse/
@StefanReich
@StefanReich Ай бұрын
Smelse like a scam to me honestly. I'll stick with Anti-If
@birthdayzrock1426
@birthdayzrock1426 Ай бұрын
do-while-false is just goto in disguise :D
@markjivko
@markjivko Ай бұрын
Don't say bad things about my precious! 😂 I'm kidding. Do-while-false is just a flavor of the Guard Clause, but using block breaks instead of return statements. I prefer it to the guard clause because it allows me to have only 1 return statement per function. So that way I can pretend that functions are SiSo - single input, single output. It's a pattern I like personally, not one that everyone should use.
@gr.4380
@gr.4380 2 ай бұрын
This was surprisingly good for how few subs you have, I can definitely see your channel taking off
@markjivko
@markjivko 2 ай бұрын
I appreciate that!
@tropictiger2387
@tropictiger2387 Ай бұрын
If either Kent Beck or Robert Martin has their name attached to something, you can safely assume its useless and move on.
@nick1752
@nick1752 2 ай бұрын
Genius. Just took my diploma, now gonna tell my university bros about Smelse thing. Thank you, please make more courses!
@hackvlix
@hackvlix 2 ай бұрын
Perhaps we could repackage plain old programming as the "DON'T GO TO" paradigm?
@AloisMahdal
@AloisMahdal Ай бұрын
i like smelse, it seems it's a better version of an adage what I used to say, "the best kind of conditional is return". Usually I would also add "and the best kind of comment is a function name". At some point I would realize that most ugly if/else branches could be broken by moving whatever is causing the complexity into a function (which often doubles as a documentation strategy) and just returning/throwing on all unhappy conditions. This can sometimes have a nice effect that the very last line in the function is the happiest one, and also the most representative of what the function is about. If you do it consistently you can often understand the gist of the function by reading the signature and the last line.
@markjivko
@markjivko Ай бұрын
Do while false is just a flavor of the Guard Clause. I prefer it because it allows me to have only 1 return statement per function, following your "happy function" definition of a final, easy to follow return. I like siso functions. That is, single input, single output. One set of arguments, and only one return statement. Do while false is a great tool for that.
@PatPatych
@PatPatych 2 ай бұрын
Avoiding branching makes sense in shader programming due to a bunch of cores executing the same instruction in a batch and having to do it again for a different branch if there are any, slowing everything down. That's about it. Nit surprising tbh, linkedin posts are generally pretty bad.
@maksymiliank5135
@maksymiliank5135 2 ай бұрын
The original post was probably not about branchless programming optimizations. I doubt that the course author cared about performance but rather what looks "nice" (by his standards) in a codebase
@nick-brooking
@nick-brooking 2 ай бұрын
Hey nice vid, keep it up! I recently heard about Gleam, the language with no if. I thought I was about to get another video on this and was worried this is a trend. If/Else is such a huge thing to sacrifice for some higher purpose. Also well done on implementing another goto, just what php needs.
@markjivko
@markjivko 2 ай бұрын
Thank you! do {} while(false) is not as evil as go-to, but they are definitely cousins, that's for sure.
@Skeluz
@Skeluz Ай бұрын
I'm probably pro-if, for a lack of a better term. Then again... I did a bunch of the Advent of Code 2023-challenges using "no if"-approach, on a dare. When I took a step back and went through the code it was really easy to understand everything. Any modification to the code went really fast. The code felt "clean", separated and isolated. Lots of common design patterns and data structures. My conclusion is that a "no if"-approach forces the developer to not take short cuts to solve problems. It is possible to do anyway but it is so easy to pull out the "if"-keyword to solve the problem immediately instead of producing architecture that supports your code. Side note: As a developer who focuses on code that is easy to understand vs super performant code, I really dislike "Pattern matching" yet it is hailed as a savior. All I see with pattern matching are verbose lines of regular expressions. The amount of PRs I've rejected because developers feels like they can pattern match the project to oblivion is surprisingly high. Don't even get me started on ternary if-statements...! Sorry for serious comments on a funny video. :)
@markjivko
@markjivko Ай бұрын
I like your insights! Indeed, the ternary operator is yucky. On the other hand, I don't mind it that much in the world of React components. Regular expressions are useful too - sometimes. Other times, you're better off writing a tokenization algo yourself. There's this thing called "creative limitation", where constraints seem to have the interesting effect of fostering creativity. So if you're given a programming task but you're told not to use "if" statements (or avoid them), that constraint forces you to look for alternative patterns - such as polymorphism and higher order conditionals. However - as always in SWE -, more creativity is not a desideratum. I would even go as far as saying a code that tries to be "very clever" should be rewritten from scratch. Attempting to avoid "if" statements just for fun is an interesting thought experiment. Using that game in production code, on the other hand, is just plain wrong as it leads to "over-engineering".
@user-ge2hp3qw3j
@user-ge2hp3qw3j Ай бұрын
I dislike using too many ifs, but being anti-if like this guy seems like a bad idea. Basically any problem can be solved if you throw an infinite amount of ifs at it, and I see many programmers write code this way and it irks me. "There's a bug in the code? Just put an if to check for the bug condition!" "Who needs a switch when I can if 20 times in a row??" I like my ifs to be as meaningful as possible. I like them as an actual structural fork in the algorithm rather than a one size fits all solution to everything with ifs all over the place.
@TheEVEInspiration
@TheEVEInspiration 2 ай бұрын
First time I hear of it, but branchless programming I do know and that is not an empty promise. The thing is, the typical programmer never has to deal with it in any detail. Just write for better predictable execution paths and memory-access. As in the end, that is the source of gaining performance.
@markjivko
@markjivko 2 ай бұрын
My takeaway is this: if you truly want to optimize performance, focus on O(n); logical lines of code and number of "if" statements are not important. If you want to optimize loading times and memory performance, polymorphism is probably worse than simple if statements. If you want to sell a course on SOLID principles and you need a catchy motto, say something weird. Our lizard brains have a tendency to identify and remember weird.
@TheEVEInspiration
@TheEVEInspiration 2 ай бұрын
​@@markjivko The large discrepancy in speed between memory and processors means everything that needs best performance, needs to take that into account. This means favoring arrays, group data together that gets accessed together, high information density data-structures, avoid unpredictable branches in inner loops. So less per-object thinking and more per-set/array thinking. Because in reality we hardly ever need to act on just one thing and nearly always need to do it for a large set of things. This works because modern processors deal with the memory/CPU speed discrepancy using caches and various prediction/prefetching hardware. It is easy to predict + fetch the memory for the next/previous element in an array. So linearly accessing the array is more efficient than some fancy structure that in theory requires less steps to do the same actions. As for polymorphism, that is just one way things can become unpredictable for a CPU. If an array contains all objects of the same class (all dog/cats, whatever), it wont really hurt performance. Because the pattern is predictable. If an array however contains a random mixture of both, then performance will suffer or even tank.
@klasus2344
@klasus2344 2 ай бұрын
Subscribed
@markjivko
@markjivko 2 ай бұрын
❤️
@comedyclub333
@comedyclub333 2 ай бұрын
The examples on the anti-if programming website are basically examples by people who never heard of guard clauses. Also, I'm still wondering why they chose accordions to represent their single question FAQ.
@markjivko
@markjivko 2 ай бұрын
Exactly. The "smelse" example is just the Guard Clause implemented within a while block. That lonely FAQ question in an accordion just shows poor UI planning. But if you want to take it one step further, it confirms that the author prefers complexity over simplicity. Polymorphism over simple if statements. Design patterns and expensive OOP over "simple" functional programming. In my opinion simplicity is always a desideratum in Software Engineering. But hey, I didn't "pioneer TDD" or whatever.
@codeline9387
@codeline9387 2 ай бұрын
can i use smelse in not smell programming language? or it's php only approach
@markjivko
@markjivko 2 ай бұрын
The guard clause using do while(false) can be implemented in most programming languages, even the stinky ones.
@moneyfr
@moneyfr 2 ай бұрын
What do you think about fp-ts
@DiSiBijo
@DiSiBijo 2 ай бұрын
switch case for the win
@forasago
@forasago 2 ай бұрын
agreed but does anyone know why the syntax for switch is so god awful disgusting in seemingly every language? is there a language where a switch feels good to write?
@markjivko
@markjivko 2 ай бұрын
I think that's by design, so you use it less 😂
@maksymiliank5135
@maksymiliank5135 2 ай бұрын
​@@forasagorust and zig have a decent syntax. The problem with switches in a language like c is that is uses label syntax to handle each case and you have to break out of the switch after every one of them. That syntax looks very out of place. In languages like zig you can just: switch (x) { 42 => { // Your logic }, // other cases... else => {} }
@chudchadanstud
@chudchadanstud 2 ай бұрын
It really depends. If you have 2 condition it literally doesn't matter. They're both branching. No performance gains.
@chudchadanstud
@chudchadanstud 2 ай бұрын
​​@@maksymiliank5135 pattern matching is not magic. It's just pushing the responsibility of deciding whether to use if-else or switches to the compiler.
@ifstatementifstatement2704
@ifstatementifstatement2704 5 күн бұрын
All these obsessions with not using for loops and if statements, come from some programmers’ desire to prevent people other than them to program
@markjivko
@markjivko 5 күн бұрын
I agree with you. But I'm not sure it's a need to stop others from working rather than a constant need to prove they are smarter. There's a saying, "those who can't do, teach, and those who can't teach, teach sports". I believe academic types have this frustration, a sort of inferiority complex. They need to constantly use big words and complex imagery to convey meaning. Whereas any programmer worth his salt knows you should "keep it simple, stupid", and complexity is the enemy of engineering. Secondly, academic types also need to pay those bills. Beyond tenure there's not much you can do - so you sell your "authority". You write books, you attend conferences, and you create courses and programs that have your signature on them. You constantly remind people how well known you are and how much you contributed to Scrum or whatever and you keep on tapping into people's authority bias to make a quick buck until your name becomes synonymous with charlatan at which point you retire to a villa in southern Italy.
@kuklama0706
@kuklama0706 16 күн бұрын
4:08 Who writes else-if like that? You solved the problem that wasn't there in the first place. if( ) { } else if( ) { } else if( ) { }
@markjivko
@markjivko 16 күн бұрын
I don't like "else if".
@richardcesar5546
@richardcesar5546 2 ай бұрын
Now I am not a PHP guy, more low level, but I don't see why you do not replace the if while false with a function and the breaks with returns. In both cases they should compile down to jumps, but the latter is not going to clutter your stack frame with temporaries.
@markjivko
@markjivko 2 ай бұрын
The "do while false" approach is just a flavor of Guard Clause. I prefer it because it allows me to have only one return statement per function - respecting the "single input, single output" principle. I know the "single return per function" preference is controversial, so I did not mention it in this one. Maybe I'll go over it in some future video. Regarding the jumps in PHP - I have zero clue whether a break is more "optimal" than a return. I would bet that there's no significant difference between the two at any scale. Remember that PHP is a scripting language - code is interpreted, not compiled.
@richardcesar5546
@richardcesar5546 2 ай бұрын
@@markjivko Thanks for the well thought out reply. Earned a sub. I was under the impression most production deployments of php had JIT compilation these days, so it would apply to hotpath code at least. I am absolutely interested in your single return statement per function preference. I've actually never heard that before, and I am not sure if its a matter of code style or maybe something PHP does that you are optimizing around? Because the downside I see of what you did here, assuming the JIT is not smart enough to optimize it out, is introduce a pointless branching conditional at the end. Is it not possible to break out of a block that is not a loop in PHP? I know in like C or zig or rust I can create a named block and break out of it.
@hisyamsk
@hisyamsk 2 ай бұрын
great video! just subbed
@markjivko
@markjivko 2 ай бұрын
Awesome thank you!
@benjaminbras7475
@benjaminbras7475 Ай бұрын
do { } while(false) thats good LOL
@maksymiliank5135
@maksymiliank5135 2 ай бұрын
do ... while(false) looks worse than a goto. I understand the idea behind it and I think it is nice but it can be solved in a much better way in most modern programming languages with if/switch/blocks which can be evaluated as expressions. E.g. in zig: const result1 = if (some condition) value1 else if (some other contition) value2 else value3; // its like a ternary operator with better chaining syntax const result2 = switch (some_enum) { enum_variant1 => value1, enum_variabt2 => value2, else => default_value, }; Breaking out of a labelled block scope with a value const result3 = blk_lbl: { // some common logic if (some_condition) break :blk_lbl value1; break :blk_lbl default_value; };
@markjivko
@markjivko 2 ай бұрын
I have mixed feelings about the ternary operator. Kind of makes sense in react components, not really easy on the eyes otherwise. As always in SWE, "it depends".
@codeChuck
@codeChuck 2 ай бұрын
@@markjivko Ternary is useful in Typescript for `ArkType` codebase :) It is simple and good until it is not :) Agreed on it is controversial! The `prettier` team spent lost of time to format it correctly when it breaks to a new line!
@Ian-zj1bu
@Ian-zj1bu Ай бұрын
Why the "do while false" when you can return early?
@markjivko
@markjivko Ай бұрын
Great question! Returning early is technically called "The Guard Clause". The "do-while-false" structure is just a flavor of the guard clause with breaks instead of returns. I prefer this approach because it allows me to have only 1 return statement per function. This is a personal preference, of course, and not something that should be considered best practice. I like to look at functions as SISO - single input, single output. Functions (usually) have only 1 set of parameters, and that is the "input". Having only 1 return statement at the end of the function just feels right to me. Obviously, "impure" functions have more than 1 input: arguments, use() statement, globals and superglobals etc. ($input) => { $output = 0; do { /* change output */ } while (false); return $output; }; I just prefer the simplicity of SISO.
@Ian-zj1bu
@Ian-zj1bu Ай бұрын
@@markjivko I like it.
@AloisMahdal
@AloisMahdal Ай бұрын
ok, i have the diploma. where do i send my $2200?
@markjivko
@markjivko Ай бұрын
😂
@milanpospisil8024
@milanpospisil8024 17 күн бұрын
This is basicaly copy paste programming, my nemesis :-D
Python's 5 Worst Features
19:44
Indently
Рет қаралды 83 М.
The purest coding style, where bugs are near impossible
10:25
Coderized
Рет қаралды 871 М.
ДЕНЬ РОЖДЕНИЯ БАБУШКИ #shorts
00:19
Паша Осадчий
Рет қаралды 6 МЛН
Black Magic 🪄 by Petkit Pura Max #cat #cats
00:38
Sonyakisa8 TT
Рет қаралды 36 МЛН
КАК СПРЯТАТЬ КОНФЕТЫ
00:59
123 GO! Shorts Russian
Рет қаралды 3,2 МЛН
The Worlds Most Powerfull Batteries !
00:48
Woody & Kleiny
Рет қаралды 25 МЛН
Motivation sucks | Do this instead
7:44
Mark Jivko
Рет қаралды 314
How C++ took a turn for the worse
5:03
Code Persist
Рет қаралды 250 М.
Guillermo Rauch - Vercel, The Front End Cloud
1:05:02
devtools-fm
Рет қаралды 6 М.
The Secret Behind Ollama's Magic: Revealed!
8:27
Matt Williams
Рет қаралды 27 М.
How Senior Programmers ACTUALLY Write Code
13:37
Thriving Technologist
Рет қаралды 1,3 МЛН
SQL is the COBOL of databases
5:58
Mark Jivko
Рет қаралды 358
How to build stuff (like an ex-CTO)
12:02
Mark Jivko
Рет қаралды 178
The case against htmx
6:21
Mark Jivko
Рет қаралды 10 М.
ДЕВУШКА проучила МУЖА изменщика 😱 #shorts
1:00
Лаборатория Разрушителя
Рет қаралды 5 МЛН