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.

Treczoks
link
fedilink
7
edit-2
1Y

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.

amio
link
fedilink
0
edit-2
1Y

Ignore it (edit: obviously not for the purposes of the course, which someone helpfully jumped down my throat presuming). A lot of people somehow in charge of teaching coding couldn’t code their way out of a wet paper bag.

They are in a ton of languages in all kinds of different families for a reason: they are a logical and fairly consistent expression of two fairly consistently logical ways to deal with control flow in the specific case of a loop. Also there’s the switch-case case in C-style languages.

Now, there are legitimate arguments for avoiding tons of of exotic control flow shenanigans, but if someone doesn’t understand break/continue, then the problem is 100% theirs and nobody should take their advice on anything much, let alone relating to programming.

m_r_butts
link
fedilink
41Y

deleted by creator

amio
link
fedilink
2
edit-2
1Y

Yeah, no shit. I sort of assumed the question was about the actual concepts as they relate to actual programming, outside of their current idiot teacher’s course.

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).

mcforest
link
fedilink
191Y

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.

@Kissaki@feddit.de
link
fedilink
English
3
edit-2
1Y

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.

LalSalaamComrade
link
fedilink
1
edit-2
1Y

Can you at least share a code snippet so we have any idea about what we are looking into?

Your teacher is wrong here, at least from what I’m getting the vibe. Those two keywords don’t make the program unreadable, they’re there to serve a purpose.

there is no code snippet, it’s in general. break = fail. Maybe there are some cases where this isn’t true, but I have very little experience with this so far.

Well if you can’t break out of anything then I guess you will just have to return instead. I’m sure this will result in code that is much easier to read.

Not a bad idea, thx

The only time I’ve used break outside of switch statements is to break out of for each loops early, when I’m checking to see if at least one element in a collection meets a condition. I really don’t think there’s ever a good time to use continue, to be honest. And if you use a labeled break statement, that’s a problem.

@yoevli@lemmy.world
link
fedilink
English
31Y

continue is useful as a loop analog to early return in a function context, which helps keep indentation/nested conditionals under control and in turn improves code readability.

zib
link
fedilink
51Y

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.

thanks for the breakdown, I don’t know if my grade would be too high of a sacrifice to try goto, but I’ll have it in mind. thanks.

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

The teacher, probably: “You must always put a switch in its own function! Then use return at the end of each case.”

@zib @UnRelatedBurner @programming

zib
link
fedilink
31Y

Good point, that is a valid way to do it sometimes, but it’s extremely situational and trying to do that for everything would be absolute nonsense.

Oh absolutely. I can think of several situations where that wouldn’t work well or at all, for example, a switch statement that sets up variables to be used in the rest of the function.

@zib @UnRelatedBurner @programming

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.

last part gave me something of a mindset change, for the better. I’ll try being a little less of an idiot then c: Thanks!

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.

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.

It’s not arbitrary. When you start out on a profession, the first thing a good instructor does is make you unlearn the things you already think you know before teaching you the things you need to know. Think of it this way: When you pick up a golf club and start hitting the ball, you’ll drive it left and right. First thing you’ll be taught is to only hit straight. Even if you think you should try to drive a curve ball, a good teacher will not allow you. Only when you have mastered the basics will he teach you to drive curved balls. So ignore your teachers advice at your own peril, but it will most likely set you up for an expert beginners career.

As a closer, I can tell you this much: I received the same advice almost 20 years ago and now, after being a professional developer for two decades, I can not recall more than five times when a break statement actually made more sense than to rethink the algorithm.

You guys have so much faith it’s contagious. Very motivational words, Thank you.

I’ve never considered that she might just be very smart. You’re very optimistic. Filled me with hope out of nothing. Thanks!

sour
link
fedilink
2
edit-2
1Y

you can use break in while true loop when condition is met

@Kissaki@feddit.de
link
fedilink
English
41Y

When I need a break I make use of a break

heh, nice one

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.

This rule will apply for C# only I think (I hope). I also like your optimistic points, thanks.

m_r_butts
link
fedilink
71Y

deleted by creator

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.

m_r_butts
link
fedilink
51Y

deleted by creator

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);
}
m_r_butts
link
fedilink
11Y

deleted by creator

Yeah, thanks. I think I will do that. Thanks for the example.

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.

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.

(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.

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?

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.

oh, okay, I hear you. Thanks

Good luck

Create a post

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person’s post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you’re posting long videos try to add in some form of tldr for those who don’t want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



  • 1 user online
  • 1 user / day
  • 1 user / week
  • 1 user / month
  • 1 user / 6 months
  • 1 subscriber
  • 1.21K Posts
  • 17.8K Comments
  • Modlog