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.

  • BrianTheeBiscuiteer@lemmy.world
    link
    fedilink
    arrow-up
    32
    ·
    1 year ago

    When it comes to programming you’ll find there’s rarely a right and wrong, it’s usually a question of “meets requirements” or not. She’s your customer and she says not to use breaks or it doesn’t meet the spec (be thankful that customers in the business world are rarely this specific).

  • mcforest@kbin.social
    link
    fedilink
    arrow-up
    19
    ·
    1 year ago

    I want to add that you should consider that your teacher is teaching you fundamentals of programming. break and continue are often options to use, but you shouldn’t try to solve everything with them. You are probably writing pretty basic functions in class right now where it doesn’t really matter, but with more complex problems break and continue constructions might easily get pretty messy.

    Also your teacher has a plan what to teach you over the whole semester. If recursion is part of this plan it’s valid she wants you to understand the basics of calling functions first. This might be overkill for easy problems but it could help you to be a better programmer in the future.

  • Admiral Patrick@dubvee.org
    link
    fedilink
    English
    arrow-up
    15
    arrow-down
    1
    ·
    1 year ago

    She’s reasoning is that it makes the code harder [for her] to read, and breaks the flow of it or something. (I didn’t get her yapping tbh)

    Fixed it for you. She’s imposing her preferences on the class. Unfortunately, it’s easiest to just suck it up and appease her than to fight it. Just my two cents.

    • xmunk@sh.itjust.works
      link
      fedilink
      arrow-up
      11
      ·
      1 year ago

      Oh yea, definitely just follow the arbitrary preference. It’s actually pretty good practice for your professional life to learn to follow the occasional arbitrary bullshit… you’ll need to obey a depressing amount of it in most large companies (especially a shitshow like Google).

        • ilinamorato@lemmy.world
          link
          fedilink
          arrow-up
          13
          ·
          1 year ago

          You either learn to work within a system or you build your own system. Guess where the money is.

          At the end of the day, following your own coding preferences isn’t the fun part of the job. The fun part of the job is looking back and saying “hey, I did that.”

        • xmunk@sh.itjust.works
          link
          fedilink
          arrow-up
          8
          ·
          1 year ago

          Oh, don’t worry, every other job has to deal with the same bullshit or worse and we get to have fun solving problems.

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

      You might be right, problem is that I’m already appeasing her by not writing in a language that I know, but in one that she knows. However I’m enlightened by your correction, might explain things.

      • CookieOfFortune@lemmy.world
        link
        fedilink
        arrow-up
        9
        ·
        edit-2
        1 year ago

        It’s very rare in the industry for you to start something and have the flexibility to pick the language. You’re much more likely to be working on an existing system or expected to work with whatever the company uses.

        That said, there’s plenty of opportunity to make your own decisions. Having to follow arbitrary style rules is hardly a consideration (actually you’ll find it’s hard enough just to have consistent arbitrary rules within a company).

        There may also be opportunities for you to change these rules, but it’s also wise to pick your battles.

  • Redkey@programming.dev
    link
    fedilink
    arrow-up
    12
    ·
    1 year ago

    Assuming C/C++, dare we even ask what this teacher uses instead of switch statements? Or are her switch statements unreadable rat’s nests of extra conditions?

    This is a good life lesson. We’re all idiots about certain things. Your teacher, me, and even you. It’s even possible to be a recognized expert in a field yet still be an idiot about some particular thing in that field.

    Just because some people use a screwdriver as a hammer and risk injuring themselves and damaging their work, that’s not a good reason to insist that no-one should ever use a screwdriver under any circumstances, is it?

    Use break statements when they’re appropriate. Don’t use them when they’re not. Learn the difference from code that many other people recommend, like popular open-source libraries and tutorials. If there’s a preponderance of break statements in your code, you may be using a suboptimal approach.

    But unfortunately, for this course, your best bet is to nod, smile, and not use any break statements. Look at it as a personal learning experience; by forcing yourself sit down and reason out how you can do something without using break statements, you might find some situations where they weren’t actually the best solution. And when you can honestly look back and say that the solution with break statements is objectively better, you’ll be able to use that approach with greater confidence in the future.

    • gazter@aussie.zone
      link
      fedilink
      arrow-up
      8
      ·
      1 year ago

      It’s a very good lesson- to the point where I wouldn’t be surprised if the teacher is deliberately putting an arbitrary restriction on the assignment.

      If you want to have a career, the people that pay you are going to make you do things that you consider to be ridiculous. That’s work, that’s life. You’ve got three options- Just smile and nod and do it their way, get huffy and tell them that you don’t like their yapping and you’ll do their project your own way, or politely suggest there may be an alternative way, and ask if they are willing to be flexible with some requirements.

  • samus7070@programming.dev
    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@programming.dev
      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@programming.dev
        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@programming.dev
          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.

  • Treczoks@kbin.social
    link
    fedilink
    arrow-up
    7
    ·
    edit-2
    1 year ago

    No, there is no benefit. Actually avoiding continue or break like statements makes code overly complicated.

    Maybe she made a mental short circuit with constructs like set_jump and long_jump (which are evil).

    I’ve 30+ years of C in my portfolio, with >1000 programs small and big, with millions of LOC, and I’d say her stand on break and continue is utterly stupid.

    • xmunk@sh.itjust.works
      link
      fedilink
      arrow-up
      4
      ·
      1 year ago

      It might just be an insufficient amount of whitespace, but your second example seems much harder to parse. As someone who regularly writes extremely dense functional paradigm code - you can always use more newlines.

        • olsonexi@lemmy.wtf
          link
          fedilink
          arrow-up
          2
          ·
          1 year ago

          idk, maybe C# just doesn’t have great syntax for the way you’re doing it or something, but I think the simplest solution is the most readable in this case:

          for (int i = 1; i < JUST_THE_WORST_NUMBER; i += 2) {
              Console.WriteLine(i);
          }
          
  • litchralee@sh.itjust.works
    link
    fedilink
    English
    arrow-up
    8
    arrow-down
    1
    ·
    1 year ago

    There will always be some instructors that are more dogmatic than pragmatic. All the same, there will be instructors that have pearls of wisdom to offer. Regarding the “break” and “continue” keywords, this lays somewhere in the middle.

    One of the purposes of higher-level programming language is to remove from the low-level, machine-specific language of assembly, by offering other, more descriptive constructs, like “while”, “for”, and “switch”. In the C language, “break” is almost mandatory in a “switch” statement but only occasionally shows up in a “for” loop, excepting drivers. In Python, “break” only exists in loops, but there are lots of loops which can be replaced more efficiently with comprehensions, so “break” can be a sign of poorly organized logic.

    If you can specify which programming language you’re learning, it would help to understand what your instructor might have meant to teach.

  • xmunk@sh.itjust.works
    link
    fedilink
    arrow-up
    5
    ·
    1 year ago

    Okay, I want to clarify two different but highly similar syntaxes: break; and break 3;. The latter syntax of break #number is fucking awful and you should never use it. In most languages it’ll only count loops and not conditionals so it can be extremely hard to tell how many levels of indentation you’re unrolling and it’s probably a code smell about having an overly complex function anyway.

    However, good old single level break and continue are awesome and useful for making it really clear what preconditions exist in complex situations when looping. It’s much easier to read a series of elementary if break statements than one single gigantic if statement with half a dozen conditions.

    Most tools in our toolbox are useful… oh, except goto (unless you’re programming in assembly language). Fuck goto.

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

      haha I personally like both gotos and multi layered breaks, I know it’s bad practice, and I avoid them, but it’s fun.

      That aside, you summed up what we are thinking pretty accurately.

  • blargerer@kbin.social
    link
    fedilink
    arrow-up
    5
    ·
    1 year ago

    Using breaks is completely standard in some situations. Using breaks and continues can be very useful and still end in clearly understood code in some other situations. It is however, very easy to end up with nonsense code using both, and if its an introductory course just telling you to ignore them isn’t that crazy an idea.

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

      I seriously think that if you’re using a continue, at best it’s just a bit less clear way to structure something, and at worst the codes a kludged together nightmare. There’s definitely good uses for break, but if you think you need a continue you should take a step back and reevaluate what you’re writing.

  • bizdelnick@lemmy.ml
    link
    fedilink
    arrow-up
    5
    ·
    1 year ago

    Well, sometimes it is possible to write a loop with break or without it, and in such cases solution without break is better readable. But if you don’t see a simple way to avoid using break, use it. It is very common, as well as having multiple return statements in function. Even goto can be a good solution sometimes if it points to label located below and not very far.

    However you should avoid some antipatterns. If you write an infinite loop that is interrupted only by break, it is highly likely that you are doing something wrong. Nested loops with multiple breaks or gotos are very hard to read and debug. Such code usually can and should be rewritten for better readability and to avoid possible errors (occasional hangs, for instance).

  • zib@kbin.social
    link
    fedilink
    arrow-up
    5
    ·
    1 year ago

    I can understand telling you not to use break and continue if the point is to teach you to think about different ways to solve problems, but saying it’s because “it makes the code harder to read” is bullshit. Readable code flow is important, but if using those makes your code too hard to read, your problem is most likely that you’ve just written shitty code.

    To get really into the technical weeds, what break and continue boil down to in the compiled machine code is a non-conditional branch instruction. This is just going to move the execution pointer to a different location in memory. Other keywords, such as if, elif, and else, will compile down to conditional branch instructions. Basically the same thing, but they have the added cost of having to evaluate some data to see if the branch should happen at all. You can achieve the same things with both, but the high level code might need to look different.

    For instance, if you’re in a loop, continue will let you skip the rest of the code in the loop to get to the next iteration. Not a huge deal to instead make the entire code block conditional to skip it. However, the break keyword will let you exit the loop at any point, which is more complicated to deal with. You would have to conditionalize your code block and force the looping condition to something that would stop it on the next iteration. If you ask me, that has the potential to be much more complicated than necessary.

    Also, good luck using switch without any breaks, but I’m guessing that’s not quite what your teacher had in mind.

    In short, just go with it for now. Be creative and find a way to make it work to your teacher’s liking, but always try to be aware of different ways you can accomplish a task. Also, I don’t know what language you’re using, but if you’re in C/C++ or C# and you feel like getting really cheeky, it doesn’t sound like she disallowed the use of goto. It’s kinda like break with fewer safeguards, so it’s super easy to write broken code with it.

  • Excrubulent@slrpnk.net
    link
    fedilink
    English
    arrow-up
    5
    arrow-down
    1
    ·
    edit-2
    1 year ago

    A blanket rule against certain keywords sounds pretty silly to me. Break and continue are useful tools in the right situation. Sounds like it’s her preference that she’s decided to impose on the rest of you.

    You could ask her what she expects as an alternative. You could show her code that uses it and ask how she expects you to rewrite it to satisfy her standards. Ask nicely because unfortunately just being right isn’t enough for some teachers, they have to like you too.

    If she has a good answer, then do it that way as long as you’re in her class. If not well… sorry that’s just a terrible teacher.

  • Kissaki@feddit.de
    link
    fedilink
    English
    arrow-up
    3
    ·
    edit-2
    1 year ago

    This is too unspecific, not a focused question. Yes, break and continue break directed program flow. Like early returns do too. That doesn’t make them bad tools though. Use them where they make sense.