Skip to content
DevMeme
5679 of 7435
Duck Typing Crosses the Road
Languages Post #6230, on Sep 7, 2024 in TG

Duck Typing Crosses the Road

Why is this Languages meme funny?

Level 1: The Picky Gate

Imagine a crossing guard who says, "Only chickens may cross," and stops a duck before it enters the road. Then imagine another crossing guard who lets chickens, trucks, cats, and random people all cross because nobody made a rule. The meme is funny because one programming language is shown as very picky, while JavaScript is shown as letting almost anything through.

Level 2: Compiler Says No

A compiler error happens when a language tool refuses to build or run code because it breaks a rule. In C#, if a function says it needs a Chicken, the compiler checks whether the value passed to it is actually allowed to be a Chicken. That is why the upper panel blocks new Duck().

In JavaScript, a function parameter does not normally declare a type. In function ChickenCrossRoad(Chicken), the word Chicken is just the name of the parameter, like calling it x or thing. The function can be called with almost anything. If the code later assumes the value has a method or property that is missing, the error happens while the program runs.

Duck typing is the idea that what an object can do matters more than what class name it has. For example, if a function only needs something with a walk() method, it may not care whether the object is a chicken, a robot, or a mock object in a test. The meme exaggerates this into JavaScript accepting completely unrelated things because nobody checked them.

Level 3: The Type Safety Crosswalk

Experienced developers laugh because both panels are unfair in recognizable ways. The C# side is drawn as overprotective: one new Duck() approaches a Chicken function and the compiler slams the gate. The JavaScript side is drawn as underprotective: everyone enters, and the runtime shrugs. Neither caricature is complete, but both match the daily emotional texture of LanguageComparison debates.

With TypeSafety, the trade-off is not "strict good, loose bad." Static types catch many mistakes early, document intent, enable refactoring, and let tools reason about code. They also require you to model the domain carefully. If ChickenCrossRoad should accept any object that can cross a road, then hard-coding the parameter to Chicken may be too narrow. The better C# design might involve an interface such as IRoadCrossingParticipant, assuming the team can name it without turning the codebase into a filing cabinet.

JavaScript's DynamicTyping is useful because functions can be generic in practice. A function can operate on any value with the properties it needs. That flexibility helped the web grow quickly, where data comes from forms, JSON, APIs, DOM nodes, and questionable browser histories. But when the expected shape is only implied, errors move later. Instead of a compiler error, you may get undefined is not a function in a browser console, ideally during development, traditionally during a customer demo.

The image uses logos as personalities. The C# logo stands like a traffic officer enforcing declared types. The JS logo presides over a chaotic crossing where constructor names are treated as suggestions. The funny part is that both languages have escape hatches and discipline tools: C# can use interfaces and generics; JavaScript projects can use TypeScript, runtime validators, tests, and linting. The meme ignores that nuance because the road is already full and somebody invited new ElonMusk().

Level 4: Nominal Roadblocks

The meme compresses a real type-system distinction into a road-crossing gag. In the upper C# panel, the function signature is visibly shaped like:

void ChickenCrossRoad(Chicken chicken)

The panel complains:

a duck??? in the chicken function?? i must give a compiler error!!!

That is static typing doing its job before runtime. In a nominally typed language like C#, a value's declared type matters. If a function asks for Chicken, then passing new Duck() is invalid unless Duck is related to Chicken through inheritance, an interface, an explicit conversion, or some other type-system-approved route. The compiler rejects the program because the contract is violated before the road-crossing logic ever runs.

The lower JavaScript panel uses:

function ChickenCrossRoad(Chicken)

and then lets new Chicken(), new Truck(), new Cat(), and new ElonMusk() crowd the road while saying:

i guess the road is bussy

The technical joke is that the JavaScript parameter name Chicken is not a type annotation. It is just a local variable name. Plain JavaScript does not enforce that callers pass a specific class instance at function entry, so the function can receive anything unless the code performs runtime checks. The parser sees a parameter, not a promise about poultry, vehicles, celebrities, or common sense.

Strictly speaking, this is adjacent to duck typing, not a perfect example of it. Duck typing means code accepts an object if it has the methods or properties needed for the operation: if it can crossRoad(), maybe it is good enough. The meme's JavaScript panel is even looser: it accepts things regardless of behavior because no check is shown at all. That exaggeration is the point. Dynamic languages give you flexibility, and flexibility without validation is how new Truck() ends up in the chicken lane.

Description

A two-panel black-and-white line drawing shows a road scene annotated with code-like labels about chickens crossing a road. In the top panel, text says "a duck??? in the chicken function?? i must give a compiler error!!!" while a C# logo stands near labels such as "void ChickenCrossRoad(Chicken ...chicken)", "new Chicken()", and "new Duck()". In the bottom panel, the same scene says "i guess the road is bussy" with a JavaScript logo allowing "new Chicken()", "new Truck()", "new Cat()", and "new ElonMusk()" into `function ChickenCrossRoad(Chicken)`. The meme contrasts nominal/static type checking and compiler enforcement in C# with JavaScript's looser runtime behavior and duck-typing-adjacent permissiveness.

Comments

58
Anonymous ★ Top Pick JavaScript saw a parameter name and treated it as stakeholder guidance, not a contract.
  1. Anonymous ★ Top Pick

    JavaScript saw a parameter name and treated it as stakeholder guidance, not a contract.

  2. @GLXBX 1y

    Bussy🤯☠☠☠ (Stands for boy-pussy)

    1. @jaaaaded 1y

      damn i was thinking the same

    2. @paranoidPhantom 1y

      Typical dev meme user

  3. @Araalith 1y

    What compile error C# is talking about? And it's a void method, not a function.

    1. @noi01 1y

      Method is just a function, bounded to a class. So it is a function

      1. @callofvoid0 1y

        the thing is it is void how can it return anything ?

        1. @noi01 1y

          Appropriate username

          1. @callofvoid0 1y

            :-)

        2. @noi01 1y

          Void function changes state of a program, it is basically a return value, but indirectly

          1. @callofvoid0 1y

            yes, if it does something on the argument or the object it is called on

      2. @Araalith 1y

        Function returns result, otherwise it's a procedure. But anyway, C# has Func as a Delegate (with result)

        1. @noi01 1y

          Procedure, function, method, it's all different names in different languages to the same thing, if we break down to a pure math - we have just functions

  4. @desrevereman 1y

    bussy

  5. @vladyslav_google 1y

    What's the problem to check in JS case, if the "chicken" argument is instance of Chicken, and not something else?

    1. @Araalith 1y

      TS exists for 11 years, but people are still blaming pure JS while using BS languages like Python or Lua

      1. @GLXBX 1y

        BS - BadaSs?

        1. @Araalith 1y

          Yes. "I need your memory, CPU and all the time you have"...

          1. @L2CacheGay 1y

            it’s a matter of picking the right tool for the right job

            1. @L2CacheGay 1y

              lua, for instance, is amazing at being a plugin language

              1. @L2CacheGay 1y

                easy to embed, fast enough (especially if you’re using luajit), and pretty watertight in terms of sandboxing

              2. @Araalith 1y

                Nope

                1. @GLXBX 1y

                  Name a better tool

                  1. @Araalith 1y

                    Even JS is a better tool then Lua. No really, Lua is a disaster in every way, but when it comes to debugging it's beyond terrible

                    1. @GLXBX 1y

                      Nothing beats js in the ability to bring suffering into one's life

                    2. @anilakar 1y

                      Lua is an excellent, lightweight embeddable interpreter with a dogshit language strapped on. It's a lazy cop-out way for a dev to create extendable and scriptable programs. I meant, four dogshit languages strapped on. Every minor* version breaks things both in the syntax and standard library (yes, even string.gsub behaviour changes between releases) so you need to support all of them separately. A good custom wrapper for the standard library can alleviate a ton of issues with the language, but even in the exceptional case of devs adding in one, you gotta learn it from the beginning because it's not, well, standard. *) When you point this out, the main defense they have is that Lua is not using semver.

                      1. @Araalith 1y

                        From my perspective, Lua is annoying because: - It implements its own syntax for basic operations that is incompatible with any other language (like string concatenation) - It doesn't limit global access to variables (the "S" in Lua supposedly stands for "Safety" and "Stability") - Debugging is worse than even debugging pure C - Every time I need something, it's either not implemented in Lua or implemented in the most bizarre way possible

                        1. @anilakar 1y

                          Like I said, you have to write your own wrappers on top of the abysmally quirky standard lib. 🤣 When you google how to do something in Lua and end up in lua-users.org it's most probably a benchmark with fifteen different ways to do a simple thing. You need to manually create local temporary variables for speed, which is ridiculous. Or not use, say pairs() but next, foo to get more perf.

          2. @GLXBX 1y

            Sounds better than wasting your whole life on investigating some bullshit error in js

            1. @vladyslav_google 1y

              Easily getting fixed by try...catch block and logging the stack of the error

              1. @GLXBX 1y

                Sometimes it's something stupid to the point when you start to question common sense Like, all the languages dump maps into json as objects, but js, the origin of json, dumps them into ... fucking nothing I ain't no expert, but I have never ever faced as much bullshitery, as I face it every time when coding in js, and I switch stacks oh so often

            2. @Araalith 1y

              There are known issues in all the programming languages and frameworks. I can remember dozens for C#, especially when it comes to async.

  6. @Dymidrol 1y

    Eventloop is busy figuring out your life that's why

  7. @GLXBX 1y

    Yeah another crazy shit Only two languages that I truly hate: C# and JS Never tried TS tho, maybe I should give it a try

    1. @GLXBX 1y

      *Transsexual, of course, not js's offspring

    2. @Araalith 1y

      Oh, I can name even more for Perl. Classic C is a just one huge trap, you can shot your leg with a simple string operation

      1. @GLXBX 1y

        Yo, but C's so smol, you learn it in a week or two in a sandbox and never really shoot your leg on production I love this tiny thing

        1. @Araalith 1y

          Implicit type conversion rules in JS can be learned in 5 minutes, they are simple AF. But ppl crying for decades because of them.

          1. @GLXBX 1y

            Because you offer them to eat shit, claiming: "it's only for five minutes, and then you'll get used to it"

            1. @Araalith 1y

              "I dreamed for all my life how I add a number to an object and compare the result with an array, JS ruined my dreams"...

              1. @GLXBX 1y

                I miss static typing😔

                1. @Araalith 1y

                  TS

                  1. @GLXBX 1y

                    Why would I do TS instead of a real girl Kotlin?

                    1. @Araalith 1y

                      I'm not familiar with Kotlin, but isn't it related to Java?

                      1. @GLXBX 1y

                        It is, as a base version, but it can be compiled into JS And Kotlin Multiplatform is a thing

  8. @graduated_vernier 1y

    If it honks like a duck, has shitty hair plugs like a duck, it clearly must be a duck.

  9. @GLXBX 1y

    Or whatever else not connected to js

  10. @zvyozdniymichal 1y

    very unfortunate typo

  11. @zvyozdniymichal 1y

    also why the fuck

  12. @zvyozdniymichal 1y

    are people going on a full internet debate

    1. dev_meme 1y

      Return times / Normalize long-form debates in Internet!

      1. dev_meme 1y

        We have it and it was perfect!

    2. @dsmagikswsa 1y

      This is also a meme

  13. @zvyozdniymichal 1y

    it's a meme

  14. @fll_fll_fll 1y

    Bla bla bla… the first time I got into programming was with LUA (one of the few good things that Rio de Janeiro has to offer).

    1. dev_meme 1y

      I got into programming by large degree thanks to lua in cheat engine

      1. @leandrofriedrich 1y

        i got into the whole it stuff due to clabretro

  15. @fll_fll_fll 1y

    I used LUA to learn how to work with a private Tibia server. Best time of my life 🥺

Use J and K for navigation