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

  • Guest, it's time once again for the massively important and exciting FoH Asshat Tournament!



    Go here and give us your nominations!
    Who's been the biggest Asshat in the last year? Give us your worst ones!

Obtenor_sl

shitlord
483
0
attr_accessor :name

Done in Ruby in one line lol.

I coded Java for so long that when I learned Ruby I just became so enamoured by the way things just work and are set in Ruby vs Java. Mind you, I don't want to start a language war (I'm still fluent in Java) but Ruby is so simple and powerful it just blows my mind.
 

Asshat wormie

2023 Asshat Award Winner
<Gold Donor>
16,820
30,968
attr_accessor :name

Done in Ruby in one line lol.

I coded Java for so long that when I learned Ruby I just became so enamoured by the way things just work and are set in Ruby vs Java. Mind you, I don't want to start a language war (I'm still fluent in Java) but Ruby is so simple and powerful it just blows my mind.
Well that just declares your instance variables, setters and getters so Tuco's issue is still there. Also add me to the list of people enamored with Ruby as well (as of recently)
 

Tuco

I got Tuco'd!
<Gold Donor>
47,914
82,545
I think lendarios found a good solution. I'll have to try it out next time I'm in a clean code base to see how I feel. I think with some coding standards you do them for so long, seeing the light on minor shit like m_ isn't worth it because the light burns! Besides I have some 20 opinions of the author I disagree with so I want to move on. Hopefully you guys have the 20th debate about programming tests out of your system! (kidding).

Let's get this issue out of the way, before I even got the book I saw a handful of comments about one contentious opinion of the author: That functions should be very small. ex:
Bob_sl said:
The first rule of functions is that they should be small. The second rule of functions is that they should be smaller than that.
...
Every function in this program was just two, or three, or four lines long. Each was transparently obvious. Each told a story. And each led you to the next in a compelling order. That's how short your functions should be!
The author then goes on to list some abomination code. I really like that the author uses real world code, especially code in well known and respectable products, ex Apache Commons. This code is larger than the typical samples I want to show, but it's the primary piece he uses and I think you need enough code to illustrate the idea. Plus I think this is the biggest problem I will have with the book:

The author describes many reasons why it's bad (repeated code etc), and then at the end of the chapter rewrites it to be how he thinks it should be with the information expressed in the chapter on functions.


I knew it'd be a challenge for me to get on board with the short functions, and after reading the chapter on functions that's pretty reinforced.

My primary contention with it is that when you use the Extract Method to take code out of a larger function and put it into a smaller function you add five new ways code can break: 1. declaration, 2. definition, 3. parameters, 4. return, 5. calling. In many cases the added simplicity of the calling function by extracting the details out are worth it. Often I'll drastically improve a nasty function by simply dicing it up into several new functions. However, when this is done in the extreme you get an explosion of new code and new points of failure.

My secondary contention is that when you pursue this your class explodes in the number of functions it has. You'll take a class with several functions and that can be quickly understood by looking at their declarations and change it into a class with dozens of functions and is now heavier. You may argue that you only need to look at public functions of a class to understand what it does, but when I need to work with a class I often need to know the private functions as well. You could argue that you should continue to use the extract method and have additional classes so you have a limited number of functions within each class. The problem with this is now you're cluttering your library with classes that expose details you didn't want to know about. This shit rolls uphill until you have a massively bloated library with an order of magnitude more classes than one would expect.

We actually had to fire someone several years ago who pursued this method. They spent a month on a week long project creating 34 classes where there should have been 4. Their code was completely unusable and thrown out with them. It had the same miniscule function content wrapped up into weak ass classes that all worked together like tiny ants wandering around carrying a popped kernel of corn in the grass. Trying to navigate your way through the code to figure out what ants did what and how they all worked was a disaster.

So following the author's example, here's my version.

There's a few things I could do to this, primary being to put it into a class like the author did. I wouldn't be against it, but it'd just exchange having to pass around the wikiPage/buffer for having to declare, construct and destruct an object. To me the added complexity isn't worth it for one function. If I had another html bullshit function I'd go that route. In a later section the author later admonishes the usage of function parameters like I did, but I'll whine about that later. There's probably other ways to improve it and it's not the perfect code, but IMO it's much easier to understand and fix than the bullshit he pumped out. It might also be wrong, so if you find a bug in it, get laid.

Now that I've gone through the exercise of unfucking this code, I regret selecting such a large sample (66 lines original, 86 of his refactored code, 46 lines of my refactored code), and I doubt many people will bother to read one version, let alone three. I also don't think it's necessary to have 50+ lines of code to discuss whether 3 lines of code is ideal for function content.

A quick note since I measured lines of code. Lines of code isn't a good metric for quality, efficiency or complexity. I didn't note the lines as some kind of evidence that my code is the best. 5 lines of garbage code are much more complex, error prone and worse than 20 lines of clear code even if they are functionally equivalent. However, in general I find that a lot of code can be improved by reducing the amount of code. Programmers, including me, often piss out lines of code that have copied code, spurious functions, circular logic (Ex: value = constant * k * parameter / k) and other stuff that bloats the code. An important part of the refactoring process is eliminating everything but what is needed to execute some functionality in a clear and concise way. When I see a huge list of functions to shit out some HTML, I feel like taking out a hatchet and cutting.
 

moontayle

Golden Squire
4,302
165
I'm a big fan of not doing more work than necessary. Hard for me at the moment since I'm still learning a lot but at the same time my current redesign project has taken some huge steps over the current design in production in cutting down the clutter. After all, why have 12 dialogs when you only need 2?

Small functions can be difficult in Android. If you need to access a database the boilerplate code alone is 5 lines. Doing anything with a button? An onClickListener is 3-4 lines before you get to what the button does. About the only time I would agree with him is the need to keep the onCreate method as small as you can manage in an Android Activity. That said, it's going to be as big as it needs to be in order to do what I need it to do. Arbitrarily fostering things off into a chain of functions just for the sake of keeping things small is asinine.

That's my school + 4 months of work 2c.
 

Lendarios

Trump's Staff
<Gold Donor>
19,360
-17,424
Having a function with just one line, is counter productive. You lose more coding real state by declaring it, than just using it on the first place.
Look at this. example
You are not gaining anything by declaring this function. You are not checking parameters validation, you are not doing anything to the parameter, you are not modifying them, nor doing anything algorithmic wise. You are just duplicating and bloating the code.
"You should make things simple, but not simpler".
If you really wanted to make it more readable you can use consts for the string and just pass those around.
here is my first version.

I want to point out something that i do, or rather avoid and the reason behind it. The chaining of functions. I avoid the chaining of functions because it makes debugging easier. When going step by step, I can see exactly the parameters that are going into the function, and without going into the subfunction.
For example.
if there is an exception thrown on bug on this line "appendTearDownPath(buffer, PageCrawlerImpl.getInheritedPage(SuiteResponder.SU ITE_TEARDOWN_NAME, wikiPage ));" Object is null. You literally have no idea where is it, since the line covers two completely separated functions, and then if you place a break point on that line, you sort of have to manually exit the first one, and then go into the second one.

In other words, having the output of the function in a variable by itself, makes the debugging so must easier as all the values are in your watch or local window,
 

Noodleface

A Mod Real Quick
38,360
16,251
Pfft you peasants with your breakpoints.

I put a hardware emulator directly on the Intel chip and do source level assembly code debugging while the chip is running.

I'm of the thought that function length doesn't matter as long as it does what it needs to do. I've heard people say that a function shouldn't be longer than a sheet of paper, but in the real world that may not be always realistic. We have a lot of #ifdefs, switch statements, and in general a lot of settings that need to be set/get/etc. PCIe is pretty crazy in general.


Just a side question, do you guys work on any cool projects at home? Open source or otherwise?

I always have these grand ideas to pick up a new language and start creating, but with a new kid I find I don't even have time for that. Most of my tinkering is on my raspberry pi, but nowadays it is just a cool emulation station hooked up to my tv to play all the old games.

Wondering if I could use my tools to work on the open source core bios stuff.
 

Tenks

Bronze Knight of the Realm
14,163
607
Why hasn't Java implemented auto properties yet?
Groovy did it but honestly I don't see the big deal with auto properties. I just declare all my private fields in the class then have Intellij auto-generate all my getters and setters. With a modern IDE I don't really see the big fuss. If you really want the ease of dot notation such as MyClass.myProperty = "Some Value" the big reason Java doesn't do it is because the ambiguation between if you are trying to access the item through an accessor or directly. And since Java loves its backwards compatibility if it injects an accessor behind the scenes during compile time you may get unintended side effects if you compile code written in a previous Java version with a newer one.

Groovy literally does inject into the class a default accessor/mutator so if you do MyClass.myProperty = "Some Value" it actually does turn into MyClass.setMyProperty("Some Value"). So even if you declare a custom implementation of setMyProperty you can still access that method using MyClass.myProperty = "Some Value". I guess its clean but again I don't see any actual value added other than syntax sugar.
 

Lendarios

Trump's Staff
<Gold Donor>
19,360
-17,424
Pfft you peasants with your breakpoints.

I put a hardware emulator directly on the Intel chip and do source level assembly code debugging while the chip is running.

I'm of the thought that function length doesn't matter as long as it does what it needs to do. I've heard people say that a function shouldn't be longer than a sheet of paper, but in the real world that may not be always realistic. We have a lot of #ifdefs, switch statements, and in general a lot of settings that need to be set/get/etc. PCIe is pretty crazy in general.


Just a side question, do you guys work on any cool projects at home? Open source or otherwise?

I always have these grand ideas to pick up a new language and start creating, but with a new kid I find I don't even have time for that. Most of my tinkering is on my raspberry pi, but nowadays it is just a cool emulation station hooked up to my tv to play all the old games.

Wondering if I could use my tools to work on the open source core bios stuff.
I'm not talking to you until you post pictures or linked in of the cheerleader.
 

Noodleface

A Mod Real Quick
38,360
16,251
No no no, I have never posted her name or a picture. Someone else did and it hit way too close to home so I try not to comment on it anymore.

I brought it on myself this time. It entered creepy levels before..
 

Tenks

Bronze Knight of the Realm
14,163
607
Why do you care so much? God just mentioning a female on the internet brings out the strangest behavior in people. Are you really that hard up to see a girl? I can point you to plenty of websites that feature them.
 

Lendarios

Trump's Staff
<Gold Donor>
19,360
-17,424
Easy Tenks. This is a judgement free zone. There are plenty of sites that can help you relax, i can point you to them.
 

Noodleface

A Mod Real Quick
38,360
16,251
Probably was in the marriage thread and it wasn't me that posted it, so I can't remember. In fact if I find it now that I'm amod I'll probably remove it.

It got weird.. too weird for me.
 

Lendarios

Trump's Staff
<Gold Donor>
19,360
-17,424
Last time I ventured into the marriage thread I almost walked out with an divorce. So now I tend to avoid it.