I’m studying programming, and I don’t agree woth my teacher. She basically said that if we use break (and continue too maybe) our test is an instant fail. She’s reasoning is that it makes the code harder to read, and breaks the flow of it or something. (I didn’t get her yapping tbh)

I can’t understand why break would do anything of the sorts. I asked around and noone agreed with the teacher. So I came here. Is there a benefit to not using breaks or continues? And if you think she’s wrong, please explain why, briefly even. We do enough down talking on almost all teachers she doesn’t need more online.

  • samus7070
    link
    fedilink
    arrow-up
    10
    arrow-down
    1
    ·
    1 year ago

    There is a school of thought that break and continue are just goto in disguise. It helps that these two are more limited in scope than goto and can be considered less evil. If you read the book Clean Code by Robert Martin (it should be required reading for all developers), you’ll see that he doesn’t like functions to be very long. I think his rule is no more than 4 lines. I try to keep mine around 10 or less with a hard stop at 20 unless it can’t be avoided because I’m switching over a large enum or something. If you put your loops into functions then you can just use return instead of break.

    I did have a discussion with a teacher once about my use of early returns. This was when I had returned to school after many years as a professional programmer. I pointed out that my code has far less indentation than theirs and was simpler because of it and that it is common in the world outside of education. I got all of my points back he has deducted.

    You’re going to hear some good and bad advice from your teachers. Once you have a job check out what the good developers are doing and just follow them.

    • RustySharp
      link
      fedilink
      arrow-up
      15
      arrow-down
      1
      ·
      1 year ago

      break and continue are just goto in disguise … use return instead of break

      An if statement is goto in disguise. So is a return.

      Some would argue having 10x 4-line functions are worse for readability and debugging than a single 40-liner, because to actually understand the code you have to jump around all over the page (another disguised goto - for your eyes!)

    • Dave.@aussie.zone
      link
      fedilink
      arrow-up
      4
      ·
      edit-2
      1 year ago

      you’ll see that he doesn’t like functions to be very long. I think his rule is no more than 4 lines.

      Four line functions? Sounds like a codebase adhering to that rule would end up as a nice thick function soup. It feels like… I dunno, those database programmers that like normalising databases to the Nth degree.

      If you put your loops into functions then you can just use return instead of break.

      And that just sounds like abusing the concept of functions to replace standard flow control that your language provides.

      I mean, sure, if I find repetitive chunks of code popping up I’ll break them out into functions, but - generally speaking - I do functions that translate into discrete real-world or UI tasks. I’m opening and parsing a text file into internal structures, I’m doing the reverse to go back to a data file, I’m cycling through the data to update UI components, etc etc.

      But hey, I use C and on the rare occasion I sneak a goto in there, so I’m not qualified to pass too much judgement.

      • samus7070
        link
        fedilink
        arrow-up
        2
        ·
        1 year ago

        Yeah, it’s a bit on the extreme side for me. 10-20 is what I prefer. I find that if I follow that rule the code is easy to come back to later because the things a function does are more clearly defined. I can look at a higher level function and it’s filled with function calls like readX, createY and doThis. I don’t have to look at as many blocks of code and try to remember what the intent was.

    • arisunz@lemmy.blahaj.zone
      link
      fedilink
      arrow-up
      2
      arrow-down
      1
      ·
      1 year ago

      I wish people stopped praising a book that has been repeatedly debunked as dogmatic, outdated, nonsensical garbage.

      No, do not bother with Clean Code. Been there thinking it was some bastion of truth that just went over my head. It’s not. Fuck uncle bob.

          • UnRelatedBurner@sh.itjust.worksOP
            link
            fedilink
            arrow-up
            1
            ·
            1 year ago

            Saved me even more time! I won’t be reading that. I don’t know java at all, and the review says that the advice is dated, bad or both. Thanks

        • samus7070
          link
          fedilink
          arrow-up
          3
          ·
          1 year ago

          It’s a highly opinionated book but it is full of good advice that in my opinion goes too far. Using a metaphor here, I think he wanted to get people to the moon but knew that he needed to give guidance to get to mars because people would look at whatever he wrote and think it’s too much.

          The book has several chapters discussing the SOLID design principles and showing how to apply them. You’ll be a better programmer for reading it. “Uncle Bob” the person can be a bit problematic so I don’t particularly like telling people to give him money. Try getting the book from the library or a second hand store. There are also videos out there of him speaking at conferences that may give a good taste of the material. He has a blog too.

    • UnRelatedBurner@sh.itjust.worksOP
      link
      fedilink
      arrow-up
      1
      ·
      1 year ago

      If you put your loops into functions then you can just use return instead of break.

      I wonder if I can bypass her rule with this, thanks.