IT/Software career thread: Invert binary trees for dollars.

Khane

Got something right about marriage
19,827
13,341
I think the first sentence gave away the context. Exceptions are not part of a normal flow of a program. He should have used try parse instead. Exception handling on a try catch is very resource intensive. Protecting yourself against a timeout or a unforeseen situation is recommended, using exceptions as a crutch for something that can be handled without an exception, is not good programming.
It looks like the code you wrote is C#. In C# a try/catch block is only resource intensive if an exception occurs. So if you're expecting the code to catch a lot of exceptions you are absolutely right, but if the exception block will only catch an expected limited amount of errors there is a negligible performance hit, but I agree with idea behind your statement.
 

Obtenor_sl

shitlord
483
0
There's also the concept of checked and unchecked exceptions, at least in Java, but the concept is still good for other languages: Should you catch an exception from a piece of code that's doing something wrong (such as a bug/defect) or something unforeseen? In the case of say network connectivity issues, that's an exception you should catch; an example of one that you shouldn't is something like dividing by zero (or a null pointer, or indexarray, etc), if the value/variable is set to 0 somewhere in your code (because you didn't say sanitize properly) then you should fix that instead of trying to catch that exception.
 

Tuco

I got Tuco'd!
<Gold Donor>
45,418
73,488
What's "bad programming"?

I don't consider myself a mastermind or anything, but the people around me who (apparently) have way more experience are HORRIBLE coders in my opinion. And that's the thing: "In my opinion." -- I'm starting to wonder is it just me? Is it just a "style" people adopt and, to me, it looks like shit? Maybe my stuff looks like shit to them. I'm not sure.
Bad programming is a nebulous term and creating any kind of metric to measure the quality of code would be really cumbersome and silly. People who get really uppity about how things should be coded in very specific ways are probably bad. It's like someone in CStrike demanding everyone use esdf instead of wasd while always being in the bottom 20% of k/d.

Let me give two examples of bad programming that are somewhat contradictory:
1. People who unnecessarily try to condense the size of their program has thinking it will improve the runtime of their code or something stupid. I'm not talking about removing some unecessary {}, I'm talking about reusing variables improperly, or writing some coherent code, testing it, then condensing it as much as the compiler/interpretter allows.

2. People who implement functionality using like 5x times the code as it should take. We had a guy produce 35 classes to implement a sensor interface that shouldn've taken 3-5. The whole codebase was indecipherable. Some people create wrappers upon wrappers of stuff for no purpose. Some people get used to a small set of design patterns and then force every bit of functionality into these design patterns until you have to go on a fucking adventure to find code that does anything for real.

One time I sat with a developer who was really uptight about coding standards/practice and produced really bad code. He was leaving and I was responsible for his mess (we threw all the code out and rewrote it). He was tasked with adding a new variable to some class or something really minor and wanted me to shadow him while he did it. After like 20 minutes of him changing one shitty wrapping class after another he just gave up and asked me to come back the next day.
 
349
1
Recursion, you are not beautiful and elegant like I've been told!

I am working on this one method where no matter what I do, I get the opposite of what I want and when I reverse the logic, I still get some backwards output. Some problems seem natural to do recursively and developing a solution doesn't take long (IE factorials, converting decimals to binary, ect) but others not so much. I shall sit on it for a bit longer because I know I am close. Probably something stupid I am overlooking like every other coding problem I've come across.

Question though. Do you try to make as much of your code recursive if possible or not? I would think not because it seems to use more resources and is less readable. I ask because it is almost preached here at my school that recursive methods are preferred when possible because it is "elegant and perfect."
 

Cad

<Bronze Donator>
24,487
45,378
Recursion, you are not beautiful and elegant like I've been told!

I am working on this one method where no matter what I do, I get the opposite of what I want and when I reverse the logic, I still get some backwards output. Some problems seem natural to do recursively and developing a solution doesn't take long (IE factorials, converting decimals to binary, ect) but others not so much. I shall sit on it for a bit longer because I know I am close. Probably something stupid I am overlooking like every other coding problem I've come across.

Question though. Do you try to make as much of your code recursive if possible or not? I would think not because it seems to use more resources and is less readable. I ask because it is almost preached here at my school that recursive methods are preferred when possible because it is "elegant and perfect."
I don't use recursion unless the method makes sense to do recursively. XML parsers (or other parser of arbitrary input), for example, are great for recursion because you can get N number of the same tags in a row and why write the same handler over and over?

For most practical applications you'd never use recursion.
 

Citz

Silver Squire
180
8
Exactly, unless you have a tree-like structure (or you're really into functional programming), recursion mostly doesn't have its place. Recursion, while great, makes a process flow harder to decipher.
 

Deathwing

<Bronze Donator>
16,386
7,388
You'll use recursion a lot on interviews, often to your detriment because the fucker interviewing you used it as a "gotcha!". Actual programming? Rarely. As Citz says, it makes process flow a lot harder to understand. Easy to read code is underrated.
 

Dumar_sl

shitlord
3,712
4
Recursion, lol. I had forgotten. Just ignore anything/everything in an actual CS curriculum except maybe your database class, and even that, ignore about half.
 

Noodleface

A Mod Real Quick
37,961
14,508
They put such an emphasis on recursion in school and I've used it maybe 1% of the time here. Whenever it is implemented, it seems needless and confusing.
 

Asshat wormie

2023 Asshat Award Winner
<Gold Donor>
16,820
30,963
I think recursion is pretty but thats the math lover in me since its basically just induction. My professor always said that no one uses recursion in the industry until they have to have it and that is when the highly paid consultant gets hired.
 

Tuco

I got Tuco'd!
<Gold Donor>
45,418
73,488
I had the same experience where I found recursion to be overused in school. My theory is that it is used a lot in conservative institutions because Computer Science faculty like solving computer science problems that intersect with discrete math, data structure problems etc. For these problems recursion is the only way to do it concisely so it has that mathematical ellegance that the science field loves in the equations they put on the front of their publications. I know I always get a little bit excited when I have a problem that utilizes recursion. I had a very simple recursive problem I solved yesterday and thought to myself, "Oooh it's technically a recursive problem!'.

But it has its place. And you probably use it more than you think. In C++ did you just make a std::map<string,double>? I think it uses a binary search tree and every time you access it might use a recursive algorithm to find the element (though now that I think of it, iteration is probably just as good...). Does your program use SQL somewhere? You're probably using recursion everytime you access it.

But most programming? Nah. If you're just writing a web handler that accepts some json payload with a known format and operating on that data, it's no surprise that recursion has no place because all the fun CS problems were solved by the libraries you're using. You're just hooking them together and adding context for your specific use case.
 

Tenks

Bronze Knight of the Realm
14,163
606
Recursion is a good teaching tool because it is difficult to understand and grasp and when used properly can be a very elegant solution. But I've used it maybe twice in the real world.
 

Obtenor_sl

shitlord
483
0
You barely use it because recursion is just a pretty flow control structure. You can achieve pretty much the same using while/do
 

Tenks

Bronze Knight of the Realm
14,163
606
Adding for-next style loop syntax in Java was the best QoL change they've ever done
 

Tuco

I got Tuco'd!
<Gold Donor>
45,418
73,488
C++11 added a new for iterator system that is super nice. I wish i could upgrade compilers
frown.png
 

Tuco

I got Tuco'd!
<Gold Donor>
45,418
73,488
You barely use it because recursion is just a pretty flow control structure. You can achieve pretty much the same using while/do
Recursion gives you a contextual call stack to work with that would be annoying to replicate with while/do.
 

Tenks

Bronze Knight of the Realm
14,163
606
By far the language I like the most in terms of syntax and "get shit done fast" is still Groovy. Mainly because modern Java programming is almost all about working with Collections and the framework Groovy has for collection definitions and looping them is super, super nice. Like a 1-10 for loop is expressed like

Lists are defined like

Like I've said before I like Groovy for unit tests and prototype. I just wish the language wasn't so inefficient.
 

Tuco

I got Tuco'd!
<Gold Donor>
45,418
73,488
Fun recursive vs iterative CS question:

I'm building a hex tree where each node has the address of its six neighbors if they exist ex:
I start out with one node and as I need to expand the space it occupies I keep adding nodes (and have to assign the new nodes to its neighbors if they exist!!). Once I'm done I need to deallocate the memory space of all those nodes without leaving behind any nodes or double deleting.

I'm going to write the recursive implementation for delete and would be interested to see the iterative implementation.

Note that I might give up and try a completely different and better approach once I realize how slow searching is and how much better octrees or kdtrees are for my problem.

Software engineering bonus question: Find a current implementation that does all this with better performance and reliability than I can.