ShinyLemmy
  • Communities
  • Create Post
  • heart
    Support Lemmy
  • search
    Search
  • Login
  • Sign Up
orhtej2@eviltoast.org to Programmer Humor@programming.devEnglish · 22 hours ago

Peak code reuse

eviltoast.org

message-square
29
link
fedilink
329

Peak code reuse

eviltoast.org

orhtej2@eviltoast.org to Programmer Humor@programming.devEnglish · 22 hours ago
message-square
29
link
fedilink
alert-triangle
You must log in or # to comment.
  • FiskFisk33@startrek.website
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    1 hour ago
    isEven(0) ->
        true;
    isEven(Num) ->
        isOdd(Num-1).
    isOdd(0) ->
        false;
    isOdd(Num) ->
        isEven(Num-1).
    
  • FiskFisk33@startrek.website
    link
    fedilink
    arrow-up
    1
    ·
    1 hour ago

    https://www.npmjs.com/package/is-even

    don’t look at the weekly downloads if you are faint of heart.

    • ThanksForAllTheFish@sh.itjust.works
      link
      fedilink
      arrow-up
      1
      ·
      58 seconds ago

      https://10xengineersqualityprogramming.github.io/ https://www.npmjs.com/package/@falsejs/falsejs This is hilarious, has 262 of the best useless dependencies. In all seriousness though how does anyone ever audit a non package, it’s dependency hell!

    • Hawk@lemmy.dbzer0.com
      link
      fedilink
      English
      arrow-up
      1
      ·
      7 minutes ago

      If you really want to see some horror, follow the dependencies

  • guthib_net@programming.devBanned
    link
    fedilink
    arrow-up
    1
    arrow-down
    2
    ·
    edit-2
    2 hours ago

    Removed by mod

  • guthib_net@programming.devBanned
    link
    fedilink
    arrow-up
    1
    arrow-down
    1
    ·
    edit-2
    2 hours ago

    Removed by mod

  • cogman@lemmy.world
    link
    fedilink
    arrow-up
    3
    ·
    edit-2
    10 hours ago

    Fixed

    boolean isOdd(int num) {
      if(num == 1)
        return true;
      if(num > 0)
        return isEven(num - 1);
      else
        return isEven(num + 1);
    }
    
    boolean isEven(int num) {
      if(num > 0)
        return isOdd(num - 1);
      else
        return isOdd(num + 1);
    }
    
    • Rednax@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      9 hours ago

      Why the complicated if statements to check the sign? Just let the number overflow. Would be functionaly the same, and result in much prettier code.

      • cogman@lemmy.world
        link
        fedilink
        arrow-up
        2
        ·
        9 hours ago

        That’s a platform dependent change. Overflow is undefined behavior. I’d rather have my code portable so it can run on my Univac 1101.

    • affiliate@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      9 hours ago

      the downside with this approach is that it will eventually terminate. the version in the original post has the advantage of giving me plenty of time to contemplate life’s many mysteries.

      • cogman@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        9 hours ago

        What can I say, I’m a performance nerd.

  • meme_historian@lemmy.dbzer0.com
    link
    fedilink
    English
    arrow-up
    37
    ·
    18 hours ago

    stupid comic style drawing of a snake with a derpy face eating its own tail

  • katy ✨@piefed.blahaj.zone
    link
    fedilink
    English
    arrow-up
    40
    ·
    edit-2
    18 hours ago
    bool isOdd(int num) {  
    	const oddNumbers = [];  
    	for (let i = 1; i <= 10000000; i += 2) {  
      		oddNumbers.push(i);  
    	}  
    	if (oddNumbers.includes(num) {  
    		return true;  
    	}  
    }  
    
    • Malix@sopuli.xyz
      link
      fedilink
      arrow-up
      12
      ·
      19 hours ago
      isOdd(-1);
      
      • dev_null@lemmy.ml
        link
        fedilink
        arrow-up
        1
        ·
        18 hours ago

        deleted by creator

    • dependencyinjection@discuss.tchncs.de
      link
      fedilink
      arrow-up
      6
      ·
      17 hours ago

      isOdd(10000001);

      • katy ✨@piefed.blahaj.zone
        link
        fedilink
        English
        arrow-up
        11
        ·
        16 hours ago

        this incident has been reported

    • moseschrute@piefed.social
      link
      fedilink
      English
      arrow-up
      10
      ·
      edit-2
      19 hours ago

      Maybe memo just to be safe, but LGTM!

    • marcos@lemmy.world
      link
      fedilink
      arrow-up
      3
      ·
      17 hours ago

      You should make it oddNumbers.includes(num%10000000)…

    • schema@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      17 hours ago

      And if not, unicorns!

  • OpenStars@piefed.social
    link
    fedilink
    English
    arrow-up
    19
    ·
    18 hours ago

    Boss: don’t spend any time on it, just vibe code a solution.

    You: sure, I enjoy receiving a salary, what could go wrong?

    • Aneb@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      7 hours ago

      Mood…

  • a14o@feddit.org
    link
    fedilink
    arrow-up
    45
    ·
    21 hours ago

    To fix this, add if(num == 255) return true; before line 10.

    • CannonFodder@lemmy.world
      link
      fedilink
      arrow-up
      16
      ·
      20 hours ago

      Peak efficiency there.

      But use 2147483647 to be safe.

  • HappyFrog@lemmy.blahaj.zone
    link
    fedilink
    arrow-up
    42
    ·
    21 hours ago

    Will this ever return? Won’t it just overflow the stack?

    • Sjmarf@sh.itjust.works
      link
      fedilink
      English
      arrow-up
      70
      ·
      21 hours ago

      Yep, this will cause a stack overflow.

      • rovingnothing29@lemmy.world
        link
        fedilink
        arrow-up
        27
        ·
        19 hours ago

        A mod will appear in my office and claim my problem is a duplicate when it’s not?

      • Mad_Punda@feddit.org
        link
        fedilink
        arrow-up
        24
        ·
        edit-2
        21 hours ago

        Might very well be an endless loop because tail recursion can be optimized to reuse the stack frame. Depends on a lot of things of course.

      • ozymandias117@lemmy.world
        link
        fedilink
        English
        arrow-up
        1
        ·
        16 hours ago

        deleted by creator

      • MonkderVierte@lemmy.zip
        link
        fedilink
        arrow-up
        5
        arrow-down
        2
        ·
        edit-2
        20 hours ago

        Hm, stack overflow is basically a forkbomb in programming? ok, bullshit.

        • orhtej2@eviltoast.orgOP
          link
          fedilink
          English
          arrow-up
          21
          ·
          20 hours ago

          Forkbomb kills the entire system so not really.

          With the stack overflow the runtime will gracefully terminate the program.

        • calcopiritus@lemmy.world
          link
          fedilink
          arrow-up
          12
          ·
          edit-2
          20 hours ago

          No.

          A stack overflow is a symptom, not the illness. A fork bomb is an illness.

          Software coming from the mathematical point of view, assummes it has infinite resources. However, a real computer has many resources that are finite.

          CPU time is finite. Memory amount is finite. There is a finite number of network ports. And so on.

          A stack overflow just means: “you have run out of this resource called ‘the stack’”. The stack is a region of the memory. Each thread of each process has 1 stack, and it is not infinite in size. This program will cause a stack overflow because it is infinitely recursive, and each function call will consume a bit of the stack.

          A forkbomb is not the end of a finite resource. A fork bomb is a program that uses “forking” to rapidly consume system resources. A fork bomb might cause a stack overflow. Or an out of memory issue. Slow the computer a lot. Or if the OS has a hard limit for process amount, it might reach that limit.

          • davidgro@lemmy.world
            link
            fedilink
            arrow-up
            1
            ·
            10 hours ago

            A program such as the one in this post is a loop designed (intentionally or not) to run out of stack regardless of how much there is. I’d call that an illness rather than a symptom.

Programmer Humor@programming.dev

programmer_humor@programming.dev

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: !programmer_humor@programming.dev

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

  • Keep content in english
  • No advertisements
  • Posts must be related to programming or programmer topics
Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 1.01K users / day
  • 4.41K users / week
  • 8.91K users / month
  • 20.1K users / 6 months
  • 1 local subscriber
  • 26.3K subscribers
  • 1.7K Posts
  • 60.1K Comments
  • Modlog
  • mods:
  • Feyter@programming.dev
  • adr1an@programming.dev
  • BurningTurtle@programming.dev
  • Pierre-Yves Lapersonne@programming.dev
  • BE: 0.19.12
  • Modlog
  • Legal
  • Instances
  • Docs
  • Code
  • join-lemmy.org