News for hackers. Stuff that matters.
Re:Ask HN: Anyone know any funny programming jokes? by gumby on Wednesday January 20, 2021 @11:47PM (#25855517)
This is an oldie (I first heard it in the 80s) but is one of my all time favorites. While it can be told about any two classes of people it really applies to a lot of code I encounter:

  A physicist is showing a thermos to her friend, a programmer.

  "It's amazing", she said.  "You put a cold drink inside and regardless of how hot it is outside the drink stays cold".

  The programmer is suitably impressed.

  "But that's not all", she continued.  "You can put a *hot* drink inside and no matter how cold it is outside the drink stays hot".

  Now the programmer is perplexed.

  Plaintively he asks, "But how does it know?"

I think of this whenever I read code that contains a gratuitous state variable that explains the type or content of some data structure rather than make the data structure self-explaining. Even more annoying when it's a class.

Having to coordinate two variables is a recipe for bugs down the road. Seems like it should be a beginner's mistake but I see it all the time in "non beginner" code.

Re:Ask HN: Anyone know any funny programming jokes? by randcraw on Thursday January 21, 2021 @11:18AM (#25860484)
I first heard this joke at an AI conference...

"An engineer, a physicist, a mathematician, and an AI researcher were asked to name the greatest invention of all time.

The engineer chose fire, which gave humanity power over matter. The physicist chose the wheel, which gave humanity the power over space. The mathematician chose the alphabet, which gave humanity power over symbols. The AI researcher chose the thermos bottle.

"Why a thermos bottle?" the others asked. "Because the thermos keeps hot liquids hot in winter and cold liquids cold in summer.", said the AI researcher. "Yes - so what?" "Think about it.", intoned the researcher reverently. "That little bottle - how does it know?"

Re:Ask HN: Anyone know any funny programming jokes? by JoeAltmaier on Saturday January 23, 2021 @08:39AM (#25882107)
Similar physics mystery: You're a pool of water in the bottom of a bucket, looking out at the stars. Somebody spins the bucket, making the stars spin. So you (the water) arrange your molecules in a parabolic shape, thicker at the sides of the bucket and thin in the middle.

How do you (the water know)? How do you know that the stars aren't holes in a paper sheet with light shining through? How do you know the universe isn't spinning, and you're standing still?

Re:Ask HN: Anyone know any funny programming jokes? by knodi123 on Thursday January 21, 2021 @01:28AM (#25856129)
But How Do It Know? Basic Principles of Computers for Everyone

https://smile.amazon.com/But-How-Know-Principles-Computers/d...

Re:Ask HN: Anyone know any funny programming jokes? by hedberg10 on Thursday January 21, 2021 @08:26AM (#25858550)
Thank you, that was where I remembered that sentence from.. Great book!
Re:Ask HN: Anyone know any funny programming jokes? by kilnr on Thursday January 21, 2021 @02:52AM (#25856542)
Love the joke, but to me it points to something else than gratuitous state variables. I think of it as "if all you have is a hammer, everything looks like a nail", with the hammer being "procedurally solving problems".
Re:Ask HN: Anyone know any funny programming jokes? by m463 on Thursday January 21, 2021 @08:50AM (#25858726)
Just the same, I wish make were a little more procedural.
Re:Ask HN: Anyone know any funny programming jokes? by amingilani on Thursday January 21, 2021 @12:39AM (#25855836)
Could you share an example of this? I could be misinterpreting this but say you're building a survey system and you need to keep track of answer types (e.g. text, date, etc) and the answer itself, how can you collapse that into one field?
Re:Ask HN: Anyone know any funny programming jokes? by gumby on Thursday January 21, 2021 @02:06AM (#25856325)
Your thermos does not have a switch which you must set to "hot" or "cold" before inserting a liquid, handles solids as well as liquids, and doesn't require you to even think where the theshold might lie between the "hot" and "cold" settings. Instead it just does its best to prevent an energy exchange in either direction without having to even know the variables involved. And the "same subroutine" is basically used for different sized thermoses.

In code I see people not understand this all the time. Here's an example: let's say you present the user with their previous orders, and give them the option to filter those orders by some criterion.

The shitty way to do this is to have two variables:

    Filter* orderFilter = NULL;
    bool filterSpecified = false;

    void setOrderFilter (filter* newFilter) {
      orderFilter = newFilter;
      filterSpecified = true;
    }
So instead of just checking if a filter has been assigned, you have a separate boolean. What happens if the boolean is true but the filter is null? What would the vice versa case even mean?

The global codebase is riddled with dumb errors like this.

Re:Ask HN: Anyone know any funny programming jokes? by zbuf on Thursday January 21, 2021 @04:30AM (#25857081)
Even better, let's just have the default orderFilter be the equivalent of '*'.

If there's always a filter, then there's no longer a branch at the point it's used -- that needs to be tested and maintained.

I used to work with a programmer who I found difficult because their code was an ever-increasing number of "if" statements as each new case came along. Conversely he would say coding is relatively easy and that I was overcomplicating a problem by thinking about it; all I needed to do is "add an if statement here".

Re:Ask HN: Anyone know any funny programming jokes? by nicoburns on Thursday January 21, 2021 @03:07AM (#25856622)
Yep! On of the things I point out most frequently in code reviews are things like this. However, null is an imperfect system for capturing such state. It's not self-documenting, and it breaks down when you have more than 2 states to represent.

Languages with Sum Types represent this much more elegantly with arbitrary numbers of variants and force you to check which one you have before accessing the more specific data (e.g. the filter) inside.

Re:Ask HN: Anyone know any funny programming jokes? by wruza on Thursday January 21, 2021 @09:51AM (#25859373)
I deeply disagree!

Our code is riddled with heavily overloaded meanings in a single value/domain, and if you have a variety of filters, you end up with this atrocity:

  Foo *f1; // hey, just test for NULL
  double f2; // hey, just use isnan()

  int f3; // aaaaah, crap
  bool f3_specified; // god why

  std::string f4;
  // requires heavy drugs to solve existential catastrophes
Compare that to

  template <typename T>
  struct Filter {
    bool is_specified;
    T value;
  };

  Filter<Foo *> f1;
  Filter<double> f2;
  Filter<int> f3;
  Filter<std::string> f4;
And the variety of code that has to work with either of these examples.

What happens if the boolean is true but the filter is null?

An assertion fails miserably.

What would the vice versa case even mean?

That an assertion failed miserably.

What happens if the filter is one? Minus one? Equals to PC(IP), BP? If NULL filter has to search for NULL values in a dataset? If we are looking for NAN values in a corrupted one (this one is even more tricky)?

Deeply!

Re:Ask HN: Anyone know any funny programming jokes? by gugagore on Thursday January 21, 2021 @11:41AM (#25860803)
I'm not sure they were suggesting using type-specific sentinel values (like null and nan), but to always use null or point to a value.

I like wrapping the information in a data structure exactly as you suggested, and if you never mutate the dereferenced pointer, it's equivalent to what they proposed. Big if, though.

The best is to do this generically with https://en.m.wikipedia.org/wiki/Option_type , since this pattern comes up regularly, not just with this one domain concept of "Filter"

Re:Ask HN: Anyone know any funny programming jokes? by foota on Thursday January 21, 2021 @05:34AM (#25857479)
Another way of expressing this concept is to "make invalid states unrepresentable"
Re:Ask HN: Anyone know any funny programming jokes? by User23 on Thursday January 21, 2021 @09:42AM (#25859282)
Dijkstra’s EWDs are full of examples of thinking about problems mathematically instead of operationally and then deriving elegant procedures.
Re:Ask HN: Anyone know any funny programming jokes? by alanbernstein on Thursday January 21, 2021 @01:14PM (#25862054)
Do you have any examples to point to?
Re:Ask HN: Anyone know any funny programming jokes? by User23 on Monday January 25, 2021 @08:50PM (#25911466)
Re:Ask HN: Anyone know any funny programming jokes? by yongjik on Thursday January 21, 2021 @04:34PM (#25864598)
100% agree - it's a quite common trap for inexperienced developers to write down every cases and conditions separately, when they shouldn't. As a similar example, instead of writing "req.source = host + port", people would write:

    if (host == "load-balancer-1.internal.dns") {
        req.source = HOSTS::LOAD_BALANCER_1;
        req.source += PORT_MAPPING[HOSTS::LOAD_BALANCER_1];
    }
    else if (host == "load-balancer-2.internal.dns") {
        // repeat
    }
After a few quarters of services, layers, and people being added and removed, it becomes a 500-line monstrosity and sits in every critical path. And because now it's a 500-line class (half of which is defunct, but good luck figuring out which half), nobody has time to read through it and figure out that it should have been a single assignment statement.
Re:Ask HN: Anyone know any funny programming jokes? by elwell on Thursday January 21, 2021 @09:05AM (#25858883)
I think this is one of the areas that functional programming naturally steers the programmer in the right direction.
Re:Ask HN: Anyone know any funny programming jokes? by roelschroeven on Thursday January 21, 2021 @05:04AM (#25857278)
Having the two separated gives the ability to disable/re-enable the filter without losing it. That's often very useful. In other cases you're very right.
Re:Ask HN: Anyone know any funny programming jokes? by diarrhea on Thursday January 21, 2021 @05:16AM (#25857372)
Then you'd go with `filter_enabled` or similar, which is an entirely different, separate flag. It is not coupled to what the filter itself looks like.
Re:Ask HN: Anyone know any funny programming jokes? by gfody on Thursday January 21, 2021 @05:07AM (#25857303)
this is the essence of Linus Torvald’s doctrine of good taste
Re:Ask HN: Anyone know any funny programming jokes? by novembermike on Thursday January 21, 2021 @01:07AM (#25856005)
In something like typescript you could represent this as a union type. If I'm expecting a location I could take a string ("Baghdad"), lat long pairs, an enum, etc. With a union type I can specify that it could be any of these but they fill the same purpose. In Java I'd either do method overloading or expect to receive an object that implements a method that would give the location in a common format. The wrong way to do it would be to have a function that has both a lat/long input and a string input and just say that they're nullable and we only expect to get one of them.
Re:Ask HN: Anyone know any funny programming jokes? by numlocked on Thursday January 21, 2021 @01:26AM (#25856115)
I think they are referring to this sort of thing (the reader will have to use their imagination and assume there is additional functionality in this class):

  class ReadingMaterial:
    is_magazine = False

    def is_periodical():
      return is_magazine

Using a base class (e.g. a Magazine and a Book class, or something) and inheritance, is much clearer than monkeying around with state.
Re:Ask HN: Anyone know any funny programming jokes? by layoutIfNeeded on Thursday January 21, 2021 @05:21AM (#25857405)
In graphics programming the typical example of this pattern would be when someone does bespoke computations on coordinates with a bunch of if-elses instead of deriving the proper matrix equation that "just works" due to math.
Re:Ask HN: Anyone know any funny programming jokes? by winrid on Thursday January 21, 2021 @12:58AM (#25855963)
You have an Answer object that probably contains a type enum.

However, you don't need to name all variables "answerObject".

Re:Ask HN: Anyone know any funny programming jokes? by dmingod666 on Thursday January 21, 2021 @05:31AM (#25857466)
0 if the bottle is empty.

1 if its hot.

-1 when its cold :D

Re:Ask HN: Anyone know any funny programming jokes? by the_gipsy on Thursday January 21, 2021 @06:21AM (#25857760)
Ah yes the triple-boolean
Re:Ask HN: Anyone know any funny programming jokes? by sethammons on Thursday January 21, 2021 @08:13AM (#25858455)
Reminds me of our five value boolean we used to have: is_deleted. Woe to person who expected that to be 1 or 0. Five distinct and overloaded values were possible, though I now forget them. Probably something like pending, fully deleted, being restored, not deleted, and restored.
Re:Ask HN: Anyone know any funny programming jokes? by dmingod666 on Thursday January 21, 2021 @10:14AM (#25859626)
like linux app return codes.
Re:Ask HN: Anyone know any funny programming jokes? by rzzzt on Thursday January 21, 2021 @08:41AM (#25858666)
A ternary logic computer to go with it: https://en.wikipedia.org/wiki/Setun
Re:Ask HN: Anyone know any funny programming jokes? by ljm on Thursday January 21, 2021 @11:15AM (#25860454)
FileNotFound if it's none of those
Re:Ask HN: Anyone know any funny programming jokes? by mrob on Thursday January 21, 2021 @01:34PM (#25862254)
As reported in the famous Daily WTF post:

https://thedailywtf.com/articles/What_Is_Truth_0x3f_