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.

  • mcforest@kbin.social
    link
    fedilink
    arrow-up
    2
    ·
    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
    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
      2
      ·
      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).

    • UnRelatedBurner@sh.itjust.worksOP
      link
      fedilink
      arrow-up
      0
      arrow-down
      1
      ·
      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.

  • samus7070@programming.dev
    link
    fedilink
    arrow-up
    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
      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!)

    • arisunz@lemmy.blahaj.zone
      link
      fedilink
      arrow-up
      0
      ·
      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.

        • samus7070@programming.dev
          link
          fedilink
          arrow-up
          1
          ·
          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.

  • blargerer@kbin.social
    link
    fedilink
    arrow-up
    0
    ·
    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
      0
      ·
      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.

  • crashfrog@lemm.ee
    link
    fedilink
    arrow-up
    0
    ·
    1 year ago

    I suspect “you’ll fail the test if you use break” is more of a joke by your teacher than an actual grading rubric, although if you used it more than twice in the same test I wouldn’t award you better than a B.

    Is there a benefit to not using breaks or continues?

    The benefit is that you learn to write non-branching code. That’s important for beginners, who tend to write very complicated and complex code with lots of branching, which they then discover they’re not able to test and debug. Barring you from using break and continue forces you to write more abstract code to achieve the same level of function with less complexity, and that’s how programmers advance in skill - simpler, more abstract code.

    Ultimately it’s an effort to kick a crutch out from under you. Whether you think that’s appropriate for a teacher is up to you, I guess - I’m inclined to think it is, but many students don’t respond well to being challenged.

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

      well, It’s not a joke. But the rest is way to optimistic. You aren’t the first to say that my teacher is just way smarter.then I give credit for but the thing is, noone (in my class) that I tried to share this idea with believed this to be the case. I’ll stay optimistic, and see what’ll happen.

      Thanks for the warning about overly braching code, I’ll keep that in mind. (however, I don’t get why more loops and ifs makes a function harder to test, I’m just going to trust you and that I’ll find out later.

      • crashfrog@lemm.ee
        link
        fedilink
        arrow-up
        1
        ·
        1 year ago

        (however, I don’t get why more loops and ifs makes a function harder to test, I’m just going to trust you and that I’ll find out later.

        Well, it’s fairly easy to explain - each branching statement in your function doubles the number of discrete paths through the code. If there’s one if statement, there’s two paths through the code. (The one where the if predicate is True, and the one where it isn’t.) If there’s two if statements, there’s four paths through the code. If there’s three if statements, there’s eight paths through the code.

        In order to test a function completely, you have to test every possible path through the code. If you used three if statements, that means you have to devise and write eight tests just for the different code paths, plus testing various exceptional cases of the function’s input (“what if all inputs are 0”, “what if all inputs are null”, “what if the integer is a string”, etc.) That’s a lot of tests! You might even have to write tests for exceptional cases combined with different code paths, so now you’re writing eight times the number of tests you otherwise would have had to.

        Whereas if your function doesn’t branch at all, there’s only one path through the code to have to test. That’s a lot fewer tests which means you’ll probably actually write them instead of saying “well, it looks like it works, I won’t spend the time on tests right now.” Which is how bugs make it all the way through to the end of the project.

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

          Thanks, it makes sense. I just don’t yet see how I’d reduce the number of ifs*, but I guess it’s a case by case thing.

          • simplest I can think of is let’s say we need different logic based on a number’s parity. How do I avoid if x % 2 == 0?
          • crashfrog@lemm.ee
            link
            fedilink
            arrow-up
            2
            ·
            1 year ago

            What would be an example where you need different logic based on a number’s parity? Why wouldn’t you write logic that ignores the number’s parity?

            Part of getting better as a programmer is realizing which stuff doesn’t matter, and writing less code, as a result.

  • Redkey@programming.dev
    link
    fedilink
    arrow-up
    0
    ·
    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
      0
      ·
      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.