News for hackers. Stuff that matters.
Re:Uncle Bob and Silver Bullets (2017) by ajuc on Monday July 04, 2022 @12:09PM (#31978617)
I completely miss the point of the article, but my pet peeve is that operator== is even defined for floats in most programming languages. It really, really shouldn't.

Instead if should return an error "Floating points should be compared using this library function that includes acceptable difference. If you want exact math use BigDecimal or similar. If you know what you're doing use library function with acceptable difference = 0.0"

And yes, Uncle Bob is giving some terrible advice. My least favorite is his advice to split "too long" pure functions into a stateful class with "short enough" methods that later can be called in the wrong order because now there's a right and wrong way to call them.

Re:Uncle Bob and Silver Bullets (2017) by marginalia_nu on Monday July 04, 2022 @12:30PM (#31978877)
A lot of Uncle Bob's advice come from a time and a place where they actually had merit. There is a sort of enterprisey Java code that was very common in the '90s. OOP and Gang of Four was incredibly trendy, but few people were actually all that good at it. So now your code had a AbstractFactoryFacadeDelegatorImplBuilderVisitorVisitor. Methods were often extremely long, hundreds of lines. The code was complex in a lot of ways, lots of static instances being accessed from every which way.

In this context, Uncle Bob's style was compelling, it felt like a breath of fresh air. His opinionated attitude was also compelling. In general, associating a term like 'clean' to your methodology seems to be a good zealot recruitment strategy. 'Pure functions' is similarly brilliant marketing.

That said, a lot of his supposed solutions actually cause problems of their own if you let the pendulum swing too far. It creates its own kind of complexity. Books like these also tend to sort of get their own momentum, if you look back you see a lot of big names praising them. Surely they must be good, right? Well they were. Clean Code made sense when it was published. It's for the most part not good advice today.

Re:Uncle Bob and Silver Bullets (2017) by Apocryphon on Monday July 04, 2022 @01:59PM (#31980004)
Ironically, on mobile at least, applying Uncle Bob's ideals into the VIPER framework creates proliferation of classes, interfaces, and functions that are often as byzantine and names as unwieldy as stereotypical J2EE spaghetti code.
Re:Uncle Bob and Silver Bullets (2017) by heap_perms on Monday July 04, 2022 @01:50PM (#31979897)
I'm curious why you think clean code is for the most part not good advice today. I read the book recently. What is different today compared to in 2008?
Re:Uncle Bob and Silver Bullets (2017) by marginalia_nu on Monday July 04, 2022 @02:22PM (#31980259)
You can easily end up with another sort of complexity, where the code is difficult to reason about because it is so decoupled and the methods are so short (but the call stack very long) and modules so small (but many). You can end up with unnecessary code that's difficult to spot because the logic is so spread out that the big picture is clouded.
Re:Uncle Bob and Silver Bullets (2017) by int0x80 on Monday July 04, 2022 @03:27PM (#31980803)
Because lots of small functions increase the system complexity by creating interdependencies. You also don't have context in each function to understand the big picture and very small functions can be meaningless on its own. You have to jump through many functions just to figure out what is being done.
Re:Uncle Bob and Silver Bullets (2017) by wrnr on Monday July 04, 2022 @03:58PM (#31981137)
Code is a non-linear medium, depending on the path one takes thought the graph and the scale one operates at clean might mean a very different thing. Take a long look at the history of ideas, it might be obvious looking back that thing should be the way they are, but no one just knows them as if they are a prophet. Even if there are sign of something being ugly or a hack this does not give any guidance as what direction to take next.
Re:Uncle Bob and Silver Bullets (2017) by jen20 on Monday July 04, 2022 @10:28PM (#31983688)
The feasibility of functional programming in commercial settings.
Re:Uncle Bob and Silver Bullets (2017) by mypalmike on Monday July 04, 2022 @12:43PM (#31979047)
I haven't seen anything much better than SOLID from Clean *Architecture in terms of organizing large projects into lots of small discrete interfaces. I guess globally declared pure functions are being pushed a lot today, but I find this approach organizationally wanting, particularly in promoting tight coupling (with all its drawbacks).

*Edit: Updated: I had forgotten this was not from "Clean Code".

Re:Uncle Bob and Silver Bullets (2017) by ajuc on Monday July 04, 2022 @01:03PM (#31979280)
> I guess globally declared pure functions

They don't have to (and probably shouldn't) be globally defined.

Re:Uncle Bob and Silver Bullets (2017) by mypalmike on Monday July 04, 2022 @01:23PM (#31979573)
I meant "globally declared" in contrast to instance-oriented (i.e. methods) rather than whether or not a function exists within a namespace, which is what I think you're implying.
Re:Uncle Bob and Silver Bullets (2017) by Jtsummers on Monday July 04, 2022 @01:27PM (#31979627)
A somewhat more common term is "free function". Comes from C++, in particular, where it means non-member functions (that is, not methods).
Re:Uncle Bob and Silver Bullets (2017) by greenmana on Monday July 04, 2022 @03:12PM (#31980684)
I often wonder what would currently be a way to work in a more enterprisey environment and actually make stuff a little bit more fun and not all "abstract factories" etc. I'm talking TypeScript/C#/.NET Core type of stuff, maybe with Vue. What's the "new Clean Code"? Might just learn Go next or something.
Re:Uncle Bob and Silver Bullets (2017) by NautilusWave on Tuesday July 05, 2022 @02:42AM (#31985098)
I don't think it's sensible to liken Pure Functions with "clean code". The former, while no panacea, is strictly defined in a way that the latter could never hope to be.
Re:Uncle Bob and Silver Bullets (2017) by seabrookmx on Monday July 04, 2022 @12:24PM (#31978792)
> can later be called in the wrong order

I'm thankful I'm not the only one with this critique. I see this so often and it makes navigating and/or debugging the code a nightmare, while I hop around these tiny single use methods. A long function I can read top to bottom is a lot easier to grok!

Re:Uncle Bob and Silver Bullets (2017) by BurningFrog on Monday July 04, 2022 @01:08PM (#31979358)
I used to call this the "Poltergeist Pattern".

Tons of tiny functions, each doing almost nothing, frantically calling each other, and somehow useful functionality results.

Each function is very easy to understand, but how they together do useful tasks is near incomprehensible.

Re:Uncle Bob and Silver Bullets (2017) by camgunz on Monday July 04, 2022 @01:21PM (#31979547)
I had a gig where a consultant had just been through (before I started), and totally changed the programming culture of the engineering teams. One of the weird things that happened was my new team no longer tolerated methods > 10 lines, which created exactly what you're talking about: a bunch of tiny single-use methods and helpers scattered across other classes/files/modules/libraries that totally obscured control flow.

It's amazing to me how even smart people can be taken in by Harold Hill types. Can I get a job going around giving advice for $100k a pop? I promise to do better than that guy haha.

Re:Uncle Bob and Silver Bullets (2017) by tziki on Monday July 04, 2022 @01:37PM (#31979755)
That's funny, I wonder how nobody thought to challenge that.
Re:Uncle Bob and Silver Bullets (2017) by ratww on Monday July 04, 2022 @03:20PM (#31980750)
In a team I was part of, we used the default in a popular Ruby linter, Reek. And it wasn't 10, it was 5.

The team treated that as puzzle-solving, and it was part of their culture to tackle those kinds of challenges. There was light hazing of newbies whenever the CI didn't pass because methods were too big. So, that's why at least in this case. There was no technical argument, just inertia.

Code quality was atrocious. Things that could be 10 lines were 30 or 40, and extremely stateful.

Re:Uncle Bob and Silver Bullets (2017) by btschaegg on Tuesday July 05, 2022 @10:06AM (#31988335)
On that topic: I came to like John Ousterhout's notion (in [1]) of "deep" methods/classes/etc. He explains it like this: If you have a reusable piece of code, imagine drawing it as a rectangle. The width corresponds to the complexity of the interface, the depth corresponds to the complexity of the implementation.

Ousterhout is saying that we really want deep classes, i.e. the complexity of the implementation a construct hides should be considerably larger than the complexity it takes to use it. Which, at least to me, basically says "search for good abstractions". And what you're describing strikes me as a good way not to achieve that.

Of course, there's considerable wiggle room in that mental model, and one could still endlessly squabble about how (not) to break up the implementation.

[1]: https://www.amazon.com/Philosophy-Software-Design-John-Ouste...

Re:Uncle Bob and Silver Bullets (2017) by BurningFrog on Tuesday July 05, 2022 @12:29PM (#31990337)
Sound about right!

I think the fallacy of the "Poltergeist Pattern" is to optimize solely on the - in itself valuable - metric of short simple methods, without consider anything else, like the complexity of using or reading the resulting code.

Re:Uncle Bob and Silver Bullets (2017) by forgetfulness on Monday July 04, 2022 @12:31PM (#31978888)
Examples of that were common in 2000s poor Object Oriented Programming introductory material, do people still advise that kind of design?

There were people who abhorred the sight of non-stateful code, there was little opportunity to fiddle with objects in it, not realizing that they just made their and everyone else's job harder with more moving parts, a concept that, despite being very easy to understand and with analogues to physical machines, was lost on many programmers.

Re:Uncle Bob and Silver Bullets (2017) by johannes1234321 on Monday July 04, 2022 @12:58PM (#31979221)
> do people still advise that kind of design?

It is a constant discussion. You can always find examples of long functions with lots of state which is hard to follow and then there are always examples of code where people went too short. And then there are always people taking any of those examples and showing why they were wrong.

I myself like going to the extremes for fun/toy/throwaway projects (not only on these but also with rules like "no `if`" or "no raw `for` loop" etc) to try out all the different alternatives and then resetting my default based on cases where it went well and where it went bad.

No such rule is always right, but most hmstem from being exposed to too much of one extreme.

Re:Uncle Bob and Silver Bullets (2017) by layer8 on Monday July 04, 2022 @03:16PM (#31980715)
The correct way to do this, if it really is a significant simplification, is to factor out the minimal relevant code into a private helper class (or struct) that you instantiate anew for each computation. It then functions like a stack frame you can pass around and share between functions/methods, within private boundaries.
Re:Uncle Bob and Silver Bullets (2017) by aaaaaaaaaaab on Monday July 04, 2022 @01:00PM (#31979253)
Integer floats can be checked for equality perfectly.

Uncle Bob is a fraud by the way. Turning a perfectly readable medium length function into a class with dozens of tiny methods is a cardinal sin in my eyes. Then later someone stumbles upon this class and reuses some of these methods in one way or another, while working on a problem completely unrelated to the original, thus entangling the two implementations…

Re:Uncle Bob and Silver Bullets (2017) by pmcollins on Monday July 04, 2022 @01:12PM (#31979415)
i like the idea that uncle bob subscribes to and teaches a certain style or school of coding. kind of like judo vs karate etc..

you may not be an adherent of judo and you may prefer and better understand karate, but that doesn’t make a judo teacher a fraud.

Re:Uncle Bob and Silver Bullets (2017) by ajuc on Monday July 04, 2022 @01:26PM (#31979605)
Interesting. In martial arts world there were lots of styles that claimed to be "the best" until they did UFC and suddenly people discovered that if you don't know brazilian jiu jitsu you will most likely lose to someone who knows it.

What's the programming analog of UFC/MMA?

Re:Uncle Bob and Silver Bullets (2017) by jgon on Monday July 04, 2022 @02:10PM (#31980133)
This analogy doesn't work because programming in "industry" has been like the MMA since day 1 in the sense that you have always had to "test your skills" and make something that people wanted and compete with other products. In the martial arts world a bunch of different martial arts just completely went without full-contact sparring/competition and instead built up a bunch of different rules and scenarios around how they were "too deadly to be done in practice". This is the bullshit that MMA exposed, and its interesting to note that the two practices in the comment you're responding to, judo and karate, have had a long history of being practiced "for real" in the gym and in competition and thus have spawned a long line of highly successful MMA competitors.

The analogy also doesn't work because BJJ isn't some silver bullet. What people discovered is that the first M in MMA is actually the important part and if all you know is BJJ you're going to get starched by a boxer with a sprawl, or more likely a wrestler with a modicum of submission knowledge, who will never let you get to the floor in the first place, and instead just grind you out.

So just like the question "what is the best martial art" currently has no answer outside of "you need a mix of striking and grappling not just one thing", there is no answer to "What is the best programming style" outside of "think about the problem you have at hand and crib on examples and knowledge from other people who have solved a similar problem". This "unfortunately" points to boring industry standard tools, like Java, C/C++, Javascript, RDBMSs, IDEs, Linux etc, etc. Probably some newer stuff like Rust and React as well. And note that answer isn't one specific technology, like MMA its a bag of different tools you combine.

Re:Uncle Bob and Silver Bullets (2017) by aaaaaaaaaaab on Monday July 04, 2022 @01:45PM (#31979857)
Yeah, Uncle Bob is like aikido then.
Re:Uncle Bob and Silver Bullets (2017) by Gordonjcp on Monday July 04, 2022 @07:36PM (#31982805)
> my pet peeve is that operator== is even defined for floats

I fixed two bugs at once in a softsynth caused by this stupidity. In the envelope generator code, which loosely modelled the ADSR circuit in a synth which charges and discharges a capacitor, there was an integrator implemented like `env = ((target - env) * timeconstant) + env`. The `target` value was set to 1 to allow the "capacitor" to "charge" at a rate set by `timeconstant`, and when it reached the top it would be flipped to 0 and `timeconstant` changed to the decay rate.

It failed about one go in ten, with some fudges to set `timeconstant` in odd values. It turns out that they detected the "capacitor fully charged" state with something like `if (env == 0.999) { dostuff(); }` and for some values of `env` and `timeconstant` it might never hit *exactly* 0.999 if you got it exactly wrong.

It was logically wrong (detect a precise float value) and functionally wrong (envelopes switch over when the cap is about 2/3 charged). Changing the "flip over" condition to something more like `if (env > 0.999)` and changing the maximum value of `target` to 1.5 (mimicking a circuit running off 15V and trying to it a 10V output, like a real analogue synth) cured all its problems.

Well, all its *envelope* problems. There were more, but they get into scary discussions of poles and zeroes and negative frequencies, and you don't want that at this time of night.

Re:Uncle Bob and Silver Bullets (2017) by forgetfulness on Monday July 04, 2022 @12:27PM (#31978825)
Floats can be compared for equality, they're a pattern of bits in memory after all. operator== can be defined for floats.

Using it on the results of floating point arithmetic may be a bad idea, but that's a different matter. That should perhaps be a compiler or linter warning, rather than flat out be declared incorrect.

Re:Uncle Bob and Silver Bullets (2017) by marginalia_nu on Monday July 04, 2022 @12:33PM (#31978915)
Floats can be subnormal, and different bit patterns can represent the same value. It's not as easy as just comparing bits.

Here is some cursed C code to play with:

  #include <stdio.h>
  #include <float.h>
  #include <math.h>

  int main() {
    int a = 0x1000002;
    int b = 0x2000001;
    float* fa = (float*)&a;
    float* fb = (float*)&b;
    printf("value-equals? %d\n", *fa == *fb);
    printf("fabs-equals? %d\n", fabs(*fa-*fb)<FLT_EPSILON);
  }
May print something like

  value-equals? 0
  fabs-equals? 1
Re:Uncle Bob and Silver Bullets (2017) by eesmith on Monday July 04, 2022 @04:15PM (#31981302)
Could you explain why that output is cursed?

I get the same output using the following (I switched to the portable memcpy solution.)

  #include <stdio.h>
  #include <float.h>
  #include <math.h>

  int main() {
    int a = 0x361dc5df; // 2.351e-6f
    int b = 0x361e3e22; // 2.358e-6f;
    float fa, fb;
    memcpy(&fa, &a, 4);
    memcpy(&fb, &b, 4);
    printf("value-equals? %d\n", fa == fb);
    printf("fabs-equals? %d\n", fabs(fa-fb)<FLT_EPSILON);
  }
Since FLT_EPSILON is 1.19209290E-07F , I would expect a lot of small, close numbers to give the same output, even without touching subnormal values.
Re:Uncle Bob and Silver Bullets (2017) by jbay808 on Monday July 04, 2022 @12:48PM (#31979097)
I'm not sure how subnormal numbers are relevant. For binary floats, the only duplicate values are (+0, -0) and the NaNs, and in both of those cases it's really up to you to decide if those are really representing the same value or not.
Re:Uncle Bob and Silver Bullets (2017) by marginalia_nu on Monday July 04, 2022 @12:50PM (#31979122)
I'm arguing against the notion that floats are just bit patterns in memory. If you base your operator== on such a notion, you will get very weird behavior.
Re:Uncle Bob and Silver Bullets (2017) by im_down_w_otp on Monday July 04, 2022 @12:26PM (#31978806)
Is that last paragraph actually a thing? Yikes! That sounds like the kind of advice one would give if they've only ever dealt with toy problems.
Re:Uncle Bob and Silver Bullets (2017) by ajuc on Monday July 04, 2022 @12:41PM (#31979018)
It's one of his solutions for refactoring long methods that operate on more than one variable, so that simple "extract method" refactoring isn't feasible.

He says to turn the function into a class and local variables into private fields. So that you can split the function into small methods that operate on these private fields. I used it a few times in a company that had strict "no Sonar warnings" policy. But I hate the resulting code much more than the initial code.

Re:Uncle Bob and Silver Bullets (2017) by ssrc on Monday July 04, 2022 @01:03PM (#31979282)
The jack-in-the-box (or hellraiser's box) style of OOP. You call a succession of 0 or 1 argument methods that change the object state until suddenly something happens.
Re:Uncle Bob and Silver Bullets (2017) by jacquesm on Monday July 04, 2022 @01:56PM (#31979958)
The 'clean' pattern to me is a complex data structure that is operated on atomically by relatively simple functions, but not so simple that you need to keep a 6 level call stack in your head to follow what is going on. Function decomposition is fine as long as - like everything else, really - you don't overdo it.
Re:Uncle Bob and Silver Bullets (2017) by lgleason on Monday July 04, 2022 @01:36PM (#31979742)
Do you practice TDD? That is usually where the long methods start to become an issue.
Re:Uncle Bob and Silver Bullets (2017) by ajuc on Monday July 04, 2022 @05:06PM (#31981787)
Unit testing pure functions is a pleasure, no matter if they have local variables.

    assertEquals(expectedOutput, f(input));
When you split them in a way that makes the local state external - you now have to handle the state in unit tests which is much more hassle.
Re:Uncle Bob and Silver Bullets (2017) by Joker_vD on Monday July 04, 2022 @12:23PM (#31978783)
Oh, but one generally wants an "acceptable difference" only when the numbers compared are close enough to zero, otherwise the "acceptable relative difference" should be used instead, right?
Re:Uncle Bob and Silver Bullets (2017) by ajuc on Monday July 04, 2022 @12:43PM (#31979045)
Yes, it's more complicated than that, but at least "straightforward" code with x==y won't compile so you'll have to think about it.
Re:Uncle Bob and Silver Bullets (2017) by lzsiga on Monday July 04, 2022 @01:45PM (#31979854)
Off: floating point numbers can be used to store integer values, so equality comparison might be perfectly valid in some cases. For example, if the embedded Sql doesn't support 'int64_t' (only int32_t), itt might still support 'double' which can store 52-bit integers exactly.
Re:Uncle Bob and Silver Bullets (2017) by ajuc on Monday July 04, 2022 @01:59PM (#31980011)
Yes, also there are deep magic gamedev tricks exploiting that (see quake sqrt :) ). There should be a way to do them. Something like bit_compare(x, y). I'd just prefer if rarely useful operations shouldn't use the most commonly used API. Too easy to make a mistake.

BTW I'd also love to have a built in float type that fails when you assign 0 to it.

Anyway, I guess in Java operator == is a lost cause anyway. My favorite example:

    // true
    System.out.println(BigDecimal.valueOf(10) == BigDecimal.valueOf(10));
    // false
    System.out.println(BigDecimal.valueOf(11) == BigDecimal.valueOf(11));
    // true
    System.out.println(BigDecimal.valueOf(11).equals(BigDecimal.valueOf(11)));
Re:Uncle Bob and Silver Bullets (2017) by lelanthran on Monday July 04, 2022 @03:58PM (#31981140)
> Off: floating point numbers can be used to store integer values, so equality comparison might be perfectly valid in some cases.

Yeah, but then you're having to learn all the special cases for when it silently gives wrong answers, and hope to hell that you didn't miss any.

Much better to have consistency and behave the same way all the time, than to optimise for 3 keystrokes and introduce all sorts of special exceptions that the programmer must memorise.

Re:Uncle Bob and Silver Bullets (2017) by Gibbon1 on Monday July 04, 2022 @03:40PM (#31980934)
I think if you want to perform binary operations on floats you should have to cast them to a binary type.
Re:Uncle Bob and Silver Bullets (2017) by zoomablemind on Monday July 04, 2022 @01:25PM (#31979596)
Memory based == is not unreasonable for floats, once in exact representation domain. Of course, if operating in approximated domain, especially involving any of existing math libraries, the float compare should be done with corresp. functions/macro and be epsilon based.
Re:Uncle Bob and Silver Bullets (2017) by layer8 on Monday July 04, 2022 @03:11PM (#31980683)
> my pet peeve is that operator== is even defined for floats

Then people would start writing `signum(a - b) == 0` (or some equivalent) instead of `a == b`, and/or factor that out into a helper function. Not sure that would be an improvement.