If it helps, I’m from the other side of networking and infra. You could swap the labels and make the meme accurate for me.
In the mid 1980s I learn the BASIC programming command GOTO and fell in love for a lifetime. If you look any anything I used to write in Ruby script or today in Python you’ll see my love affair with “if” “elfi” and “else” continues. My universe is nested “if” statements all the way down.
This seems geared more towards programming languages like C++, whereas in scripting such as BASH it seems to me like it absolutely would get to be a mess to have so many functions, particularly those called only once (either total for the execution of the entire program or within the context of being inside another function, so once per function).
I am curious now what would Linus Torvalds say about that?
Early returns seems good, but personally I find code more confusing when things get arbitrarily separated out into functions, because to read it you have to keep scrolling back and forth between different parts of the page and either remembering where the other part is or typing a lot with ctrl-f, there’s more variables to hold results, and if the section of code to be separated into a function is picked just because of nesting depth reasons then it’s likely the function name isn’t going to describe what it does very clearly, leading to more scrolling and more having to keep track of things in your head.
I’ve had this too. It was cured by being forced to work with Java and enhance a PHP-framework. Dear god, the php-framework.
But it literally increased my mental “stack size”. My shell and python scripts also got better.
Ah, and also keep the “do one thing and do it good” tenet also to functions.
How? I’m using vscodium. There are right click menus that bring you to function definitions and uses, but that isn’t necessarily faster than scrolling around and is still way more friction and more stuff you can’t see to hold in your short term memory.
IDEs also have collapse/expand features which make nested or longer functions easier to navigate.
In a decent IDE you can CTRL+click (or similar) on a function name to go to its definition. This is absolutely faster than scrolling, especially if you’ve got more than a couple dozen lines in the current file.
When splitting functionality into smaller functions you would ideally not have to keep implementation details of other functions in short term memory.
You only need to understand/remember the contents of the current function. Does it do what the name suggests? Cool, you’re done checking it.
Of course, the “2 hard problems in computer science” meme exists for a reason - it’s not always straight forward to come up with good names. But in my experience, even when coming up with a fitting name is difficult, reducing nesting usually helps a lot with comprehension, mainly because it reduces scope and thereby the amount of information you need to hold in your head.
The larger a function gets, the harder it is to check that it does what it’s supposed to.
There is of course a balance to strike - trying to force every single function to be at most 3 lines is likely to make the code more difficult to understand and is, in my eyes, almost pathological. But a function that covers more than a screen is the other extreme that should be avoided when practical.
I remember one of the hardest transitions for me from scripting in Ruby to Python was the whitespace indent was larger by default in Python. With Python, I ran out of monitor width and would have to be constantly scrolling right and left when visually parsing the code because the nested loops were so deep.
It was fashionable at a time to avoid ifs and other imperative structures, but I tend to like them. Functional is nice and all but I like my code to be smart in the way it works, not in the way it’s written. Nested ifs are readable, collapsible, and easy to make sense of at a glance, there’s often nothing wrong with using them.
Same here! I can put together a half-descent network in just a few hours, but even programming simple stuff on microcontrollers is a major endeavor. And the documentation is usually terrible or nonexistent. And the tutorials are usually barely able to cover basic questions. At least in the space of networking, there’s enough context clues in the UI that I can piece together the solution I need.
I’d argue programming for micros is an order of magnitude more involved than say a simple linux or windows terminal app. The former requires actual understanding of the architecture, chipset, features, instructions, interfaces, on and on… The latter has such advanced tool chains readily available you dont need to think about any of that.
If it helps, I’m from the other side of networking and infra. You could swap the labels and make the meme accurate for me.
In the mid 1980s I learn the BASIC programming command GOTO and fell in love for a lifetime. If you look any anything I used to write in Ruby script or today in Python you’ll see my love affair with “if” “elfi” and “else” continues. My universe is nested “if” statements all the way down.
As a network engineer myself… python if/elif, and nested for loops are all you need
/s
Ahh, I found you, Toby Fox!
I’m sure he’s fine, Sans/Susie.
Please avoid nested if statements. Instead use early returns when possible.
See video explanation.
This seems geared more towards programming languages like C++, whereas in scripting such as BASH it seems to me like it absolutely would get to be a mess to have so many functions, particularly those called only once (either total for the execution of the entire program or within the context of being inside another function, so once per function).
I am curious now what would Linus Torvalds say about that?
Early returns seems good, but personally I find code more confusing when things get arbitrarily separated out into functions, because to read it you have to keep scrolling back and forth between different parts of the page and either remembering where the other part is or typing a lot with ctrl-f, there’s more variables to hold results, and if the section of code to be separated into a function is picked just because of nesting depth reasons then it’s likely the function name isn’t going to describe what it does very clearly, leading to more scrolling and more having to keep track of things in your head.
I’ve had this too. It was cured by being forced to work with Java and enhance a PHP-framework. Dear god, the php-framework.
But it literally increased my mental “stack size”. My shell and python scripts also got better.
Ah, and also keep the “do one thing and do it good” tenet also to functions.
Getting good at reading bad code does seem like a worthwhile skill
A good IDE makes it unnecessary to scroll back and forth like that, though.
How? I’m using vscodium. There are right click menus that bring you to function definitions and uses, but that isn’t necessarily faster than scrolling around and is still way more friction and more stuff you can’t see to hold in your short term memory.
IDEs also have collapse/expand features which make nested or longer functions easier to navigate.
In a decent IDE you can CTRL+click (or similar) on a function name to go to its definition. This is absolutely faster than scrolling, especially if you’ve got more than a couple dozen lines in the current file.
When splitting functionality into smaller functions you would ideally not have to keep implementation details of other functions in short term memory.
You only need to understand/remember the contents of the current function. Does it do what the name suggests? Cool, you’re done checking it.
Of course, the “2 hard problems in computer science” meme exists for a reason - it’s not always straight forward to come up with good names. But in my experience, even when coming up with a fitting name is difficult, reducing nesting usually helps a lot with comprehension, mainly because it reduces scope and thereby the amount of information you need to hold in your head.
The larger a function gets, the harder it is to check that it does what it’s supposed to.
There is of course a balance to strike - trying to force every single function to be at most 3 lines is likely to make the code more difficult to understand and is, in my eyes, almost pathological. But a function that covers more than a screen is the other extreme that should be avoided when practical.
Oh hey it works, thanks.
If you like
ifstatements, you’ll love the pythoncasestatements for pattern matchingOhhh nice! I could see replacing dozens of nested
ifstatements with dozens of nestedcasestatements!Also don’t nest case statements. Please.
I remember one of the hardest transitions for me from scripting in Ruby to Python was the whitespace indent was larger by default in Python. With Python, I ran out of monitor width and would have to be constantly scrolling right and left when visually parsing the code because the nested loops were so deep.
Yes, but also, it makes for twistsd code.
It was fashionable at a time to avoid ifs and other imperative structures, but I tend to like them. Functional is nice and all but I like my code to be smart in the way it works, not in the way it’s written. Nested ifs are readable, collapsible, and easy to make sense of at a glance, there’s often nothing wrong with using them.
Same here! I can put together a half-descent network in just a few hours, but even programming simple stuff on microcontrollers is a major endeavor. And the documentation is usually terrible or nonexistent. And the tutorials are usually barely able to cover basic questions. At least in the space of networking, there’s enough context clues in the UI that I can piece together the solution I need.
I’d argue programming for micros is an order of magnitude more involved than say a simple linux or windows terminal app. The former requires actual understanding of the architecture, chipset, features, instructions, interfaces, on and on… The latter has such advanced tool chains readily available you dont need to think about any of that.
Also known as AI 🤣