• ilikecats@lemmy.sdf.org
    link
    fedilink
    arrow-up
    10
    ·
    1 day ago

    I have a rule in my NVim init.vim that it gets room for three line number digits. If it goes over, the screen shifts uncomfortably as warning. That works for my current projects.

  • kryptonianCodeMonkey@lemmy.world
    link
    fedilink
    arrow-up
    55
    arrow-down
    1
    ·
    1 day ago

    When I was in my 101 comp sci classes, one of my professors would say, “A function is meant to do one thing. So if your function doesn’t fit on the monitor in it’s entirety, that is a good indication your function is probably too complicated and/or doing too many things. Either simplify it or break it down further.” And that’s a rule I usually try to live by with my professional work too. So, anyway… I want to see the 4000k monitor this guy is using.

    • Pup Biru@aussie.zone
      link
      fedilink
      English
      arrow-up
      9
      ·
      22 hours ago

      also depth of blocks: if i’m more than 2 or 3 indents deep, it’s probably got a bit too much logic to it and those comments should be converted to a function name

    • DarkAri@lemmy.blahaj.zone
      link
      fedilink
      arrow-up
      6
      ·
      1 day ago

      The rule of thumb I use is how likely am I to reuse some part of this code in the future? Also readability. Sometimes I like to just wrap some code in functions to make the code look neater. Other considerations are how often will this function be called? The overhead of calling a function is tiny but if a program is used by millions, everyday for many years, it’s sort of like not littering a bit to make the code a bit more inline. It is kind of nice to be able to mostly see what a piece of code does in a glance other than when it’s just wasteful.

      • kryptonianCodeMonkey@lemmy.world
        link
        fedilink
        arrow-up
        2
        ·
        edit-2
        1 day ago

        Concise readability is usually my goal. Like if I have 12+ lines of code with a loop and some nested conditionals, recursively digging into buried directories, that probably takes a bit of thought to parse. So it is probably is better to move that to a function with a descriptive name like, “size, depth = get_directory_size_and_depth(filepath)”, that way the function name itself serves as it’s own comment/documentation describing what’s being done and, if you don’t really care about how the size and depth is aggregated, it abstracts that process into one line of code in the calling function that the reader can instantly understand without parsing the whole process. That goes doubly so when the function is generic like get_directory_size_and_depth(filepath) would be, and can be put in its own shared utilities file.

    • Gonzako@lemmy.world
      link
      fedilink
      arrow-up
      7
      arrow-down
      1
      ·
      edit-2
      1 day ago

      I personally go by the monocre that if it’s something important, it’d be all on one file as not to lose the train of thought when debugging it.

      As jumping around between files

      tends to break up thought lines

      also, the compiler likes it better when there’s less layers

      • Johanno@feddit.org
        link
        fedilink
        arrow-up
        3
        arrow-down
        5
        ·
        1 day ago

        Are those your arguments?

        Train of thought is subjective

        And the compiler doesn’t care

        • Gonzako@lemmy.world
          link
          fedilink
          arrow-up
          4
          arrow-down
          2
          ·
          1 day ago

          Yeah? If something is important being able to have it all on one file is the best way to ensure you get the behaviour you want.

          Having to file-hop is generally a strain on mental resources and it breaks the linearity of the project, ofuscating initial reads.

          And yes, the compiler does care, there are no 0-cost abstractions.

          • Johanno@feddit.org
            link
            fedilink
            arrow-up
            2
            ·
            23 hours ago

            Yesnt.

            If the alternative to changing into a new file is to search through a 40k lines file that has no structure, then I rather go to a new file. As my train of thoughts is interrupted by the size of the file.

            And a good compiler is capable of a lot these days. However if you are working on low end hardware that has to perform good and fast then you should of course think about the compiler.

            In 99% of software that isn’t the case though

  • TheWonderfool@lemmy.world
    link
    fedilink
    arrow-up
    108
    ·
    2 days ago

    And what’s beautiful is that the one right under has a comment “we don’t use thi…”

    This code looks like so much fun to debug…

      • Taldan@lemmy.world
        link
        fedilink
        arrow-up
        16
        ·
        2 days ago

        we don’t use this without first writing proper unit tests for our changes, integrating them with the regression tests, and thoroughly documenting our changes for the next developer that has to work on this

        I’m an optimist, and it’s technically possible

    • Blackmist@feddit.uk
      link
      fedilink
      English
      arrow-up
      23
      ·
      2 days ago

      There’s one I found today that just reads “this is a fucking abortion”

      I don’t remember doing it, but it was almost certainly me.

    • kryptonianCodeMonkey@lemmy.world
      link
      fedilink
      arrow-up
      18
      ·
      1 day ago
      def is_even(num:int) -> bool:
          if x < 0:
              x = x * (-1)
          if x == 0:
              return True
          elif x == 1:
              return False
          elif x == 2:
              return True
          elif x == 3:
              return False
          elif x == 4:
              return True
          elif x == 5:
              return False
          elif x == 6:
              return True
          elif x == 7:
              return False
      # ...
      
    • Reuben@lemmy.nz
      link
      fedilink
      arrow-up
      31
      ·
      2 days ago

      Here, I optimised it:

      private function is_even(integer): 
          integer = 2;
          return True;
      
  • Winter_Oven@piefed.social
    link
    fedilink
    English
    arrow-up
    24
    ·
    2 days ago

    imagine if the entire function is a single if-else statement which they kept adding onto, but could never refactor lest everything crashes down and burn.

    • _stranger_@lemmy.world
      link
      fedilink
      arrow-up
      5
      ·
      edit-2
      1 day ago

      You’re dredging up some bad memories 😅

      Saw one once called something dangerously innocent like “run_validation” that turned out to be a 5K line hand built deserializer (no libraries used when then absolutely should have) with so many nested levels the editor put “expand this line?(2kb)” warnings at the end of most lines.

      In the original author’s defence the garbage being deserialized was an Eldritch horror. We eventually moved to JSON.

    • Daedskin@lemmy.zip
      link
      fedilink
      arrow-up
      14
      ·
      2 days ago

      I once worked on a project doing firmware that a lot of people have likely used (printer firmware for a big-name company). One of the more trafficked functions was something like 1500 lines of “if-elseif-elseif…” with something like 13 or 14 different cases.
      It did end up getting refactored to something polymorphic and based on a configuration file to cut out any unnecessary steps, but it was like that for a while before I got there, and was like that for most of the year that I was part of the project.

      • thebestaquaman@lemmy.world
        link
        fedilink
        arrow-up
        5
        ·
        1 day ago

        To be fair, I think massive “switchboards” can be relatively clean. The key is that each step contains little enough logic. 1500 lines of 13-14 cases means the average block was > 100 lines, and at that point I definitely agree that it should probably have been refactored a long time ago. Just splitting that into 13-14 smaller functions, and using the switchboard as a delegator could be completely fine.

      • Thorry@feddit.org
        link
        fedilink
        arrow-up
        17
        ·
        edit-2
        23 hours ago

        // - - - - - - - - - - - - - - - - - -

        // Note added by employee 871 - S. Sandler - 10-31-2003

        // Chapter 1

        // Dear reader, let me tell you all about this here function.

        // You see it was a dark and rainy night, like the nights often were in the place I once called my home.

        // My grandma used to tell stories about those nights, the nights where the cold creeps in and the fog rises up from all around.

        // She used to tell us those nights were haunted, evil things happened which could not stand the light of day.

        // I never put much stock into those stories, but when the rain beats against the window and the wind would rustle the trees, I figure she might have been onto something.

        // So you see it was one of these typical nights where I learned evil does indeed come out. But not from outside as one might expect, no… The evil comes from within

        // Chapter 2

        // Just like every other Friday, I got stuck with night duty. I wanted to go drinking with friends, but my manager told me I needed the extra shift since quarterly reviews were coming up.

        // I was in a foul mood and cursed my manager, but like my grandma used to say curses can be a dangerous thing. They have a tendency to backfire when not used in moderation.

        // Frankly I’m not sure my grandma knew what moderation meant, otherwise she would have cut down on the amount of beans she ate.

        // Let me tell you; old folk and beans aren’t a great combination. But I digress.

        // Getting stuck with night duty wasn’t bad, it didn’t pay much, but you could work from home and usually nothing much happened, especially on a Friday.

        // So I sat around watching some old X-Files episodes, keeping half an eye on my mailbox to see if any tickets came in. I do say that Scully chick is one hot mamajama, I kinda forgot about the story.

        // It was at this moment I heard loud thunder rolling by and the familiar wee-woo sound of my email client getting a new mail.

        // As I read the ticket number: EI-WA-98215-6-66 the hair on the back of my neck stood up.

        // Chapter 3

        // I wondered who could be submitting tickets at this hour, but it must be important so with one eye on Scully I checked out the ticket.

        // For some reason the user name wasn’t in the ticket, just the user number. I had complained about this to IT and they said they would “get to it” whatever that means.

        // I swear to the gods if you think developers have their own code to make fun of users, IT guys are way worse. They probably just sit around crimping their cables or whatever they do.

        // Anyway this ticket was really weird, the user said when they opened up the admin portal the menu would freak out.

        // They said, I kid you not: “The menu is haunted and we need an exorcism”

        // Must be some kind of joker who also got stuck with the night shift and doesn’t have anything to do, so lets mess with the poor devs right?

        // Chapter 4

        // When I opened up the admin portal I didn’t see anything weird at first, this dude was messing with me for sure.

        // I got hungry so I wanted to make myself a quick snack, I found a buldak ramen packet in the kitchen.

        // Perhaps I shouldn’t have judged my grandma for eating beans, as I know these spicy ramen will be much worse.

        // While waiting for the water to boil, I saw movement from the corner of my eyes, it was like there was someone in the room with me.

        // When I walked over to investigate it was suddenly gone, but I saw on my screen the menu was freaking out, opening and closing like crazy.

        // I turned to walk back to the kitchen and get my ramen going, but as I did suddenly I hear a loud bang in the apartment.

        // Chapter 5

        // As I investigated the noise I found a door had slammed shut due to the wind, I was pretty sure I had closed that door earlier…

        // With my snack in hand I went in to investigate the code running the menu.

        // After thorough investigation, I found no fault in the menu. The code hadn’t been touched in months and nobody had ever complained before.

        // But I had seen the menu misbehave myself, surely something must be wrong with it.

        // Suddenly it was like my hands had a mind of their own, they started writing code like I had never seen before.

        // With horror I saw what they were doing and I gasped. The only brief pause I got was when my hands turned to my ramen.

        // I felt sick by the code they had written, but they forced the spicy buldak into my mouth and when I refused to swallow, poured the lukewarm Mt. Dew in my mouth.

        // Chapter 6

        // Even though I have no explanation for the code that now lays before you, in order to clear my conscience I will briefly try to explain what it does.

        // My understanding isn’t complete, but what I’ve been able to glean from beyond the veil is the following:

        // Lines 8 - 26 deal with getting the user details, they appear to use the regular API for this but also some calls I’m not familiar with.

        // Lines 43 - 78 seem to get the users actual GPS coordinates, I have no idea how?

        // Around line 156 the weather.com API is used to get a current report at the users location.

        // The rest of the code seems to deal with various time and weather related checks. As far as I can tell it checks if it’s dark and stormy out.

        // In the last part of the function there is some simple logic that cancels the menu animation if it triggers too often.

        // Chapter 7

        // It was with a heavy heart I pushed commit 54D3AD into production with the simple message “Fixes ticket EI-WA-98215-6-66”

        // I went to close the ticket, but somehow I couldn’t find it in the ticket system anymore. Probably those IT guys messing up again.

        // The mail was gone as well, but with Microsoft Outlook that was pretty much par for the course, so I didn’t think anything of it.

        // Wanting to put this horrible experience behind me, I resumed watching X-Files, such a classic show.

        // A few minutes later my VLC crashed and kept looping on Mulder saying “Thank you”, but it was slowed down a bunch and distorted.

        // In the few seconds I needed to get my hand free from my sweatpants, all my power went out. We really should invest more into infrastructure…

        // - - - - - - - - - - - - - - - -

        // Note added by employee 2548 - Martin (intern) - 07-02-2009

        // New Weather.com API premium key added, no idea why we need this, but without it the menu breaks.

        // Could not find contact info for employee 871 - presumably left the company.

  • SailorFuzz@lemmy.world
    link
    fedilink
    arrow-up
    14
    ·
    2 days ago

    oh look, it’s the old codebase at my job

    Legit, the company banned people from using #region in their scripts because too many people were using it to hide massive amounts of slop code. Just giant fucking IF trees. Even though using it is hugely beneficial to keep things organized, especially if you use a lot of interfaces.