Jump to content
View in the app

A better way to browse. Learn more.

Tuts 4 You

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Good Coding Practice?

Featured Replies

Posted

I just have a quick question. Is it considered bad coding practice to break out of a loop using a return statement? I have been told from previous professors that using "break" in a loop is a bad idea but not sure about using a return statement. I'm writing the code in Java. Thanks for any feedback.

Edited by Jkimble

In my programming course they told me not to put an extra condition into a for loop header, ie. just the counter, max value and increment.

Sometimes breaking out of/returning from a loop makes things a lot less messy because otherwise you would often have to add another variable, check against failure etc.

I'm guessing returning from a loop is considered better programming practice than just breaking out of it because it clearly states 'I am done' whereas break could mean just about anything and you would have to check for code after the loop.

my advice: Don't think about something stupid, it really doesn't matter how you terminate a loop. "Break" is perfectly fine. At least it is as bad as "return" and as bad as "manipulate the counter so the loop will stop". It is really not worth thinking about it.

There are much more important things to consider. E.g. in Java you should think about how you create a class, a lot of people just don't know how to create a good class model, so this is "bad coding".

  • Author

Thanks for the feedback. I just want to make sure I write good code so I don't look like an idiot when I get a job. I do struggle with a good class setup sometimes so I should focus on more important things. Right now I am writing hashing algorithms for my class and I'm constantly looking at my code trying to make it appear better, even though the program works already. I tried writing it in C and got jacked up on the pointers but that is with only 5 weeks of C experience. Anyways thanks for the replys.

as paid java developer you should definitely read and follow the official java code style guidelines:


/>http://www.oracle.com/technetwork/java/codeconv-138413.html
/>http://www.oracle.com/technetwork/java/seccodeguide-139067.html

what would you guys say is better coding style in c++?

void function()
{
if(c1)
return;if(c2)
return;if(c3)
return...
}

or

void function()
{if(!c1)
{
if(!c2)
{
if(!c3)
{
...
}
}
}
return;
}

The latter. Given you've got resources to free and you're not using RAII, it is way easier to write since you got to write the cleanup code only once - less error-prone, fewer lines, though more indentations.

Breaks are in the language to do exactly what they are telling you not to do lol. It's a matter of choice/preference. Using additional parameters / checks in the statement itself leads to the same overall result, there is going to be some check in place that will execute each iteration.

For example:

#include <Windows.h>
#include <iostream>int main( int, char* [] )
{
bool bExit = false;
for( int x = 0; x < 10; x++ )
{
if( bExit )
break;
if( x == 5 )
bExit = true; std::cout << x << std::endl;
} return 0;
}

Could also be written as:

#include <Windows.h>
#include <iostream>int main( int, char* [] )
{
for( int x = 0, bExit = 0; x < 10, !bExit; x++ )
{
if( x == 5 )
bExit = 1; std::cout << x << std::endl;
} return 0;
}

Either way, there is going to be a check and a break in the code. Which is going to turn into a cmp / jmp of some sort. (Or something similar.) Overall which ever you feel more comfortable with and is has better readability to you is typically the better choice. Not everything comes down to a speed race or less opcodes used etc. unless you specifically want to code that way.

Exiting a loop with a return or a break though comes down to what else needs to be done before you leave the scope that is using the loop. Do you need to cleanup handles or memory? Do you need to call something else before returning?

@deepzero:

If the checks are static like that (as in going to be in a row to check/validate things) it would probably be better for you to combine them into a single statement:

#include <Windows.h>
#include <iostream>int main( int, char* [] )
{
if( !c1 && !c2 && !c3 )
{
// ...
} return 0;
}

Edited by atom0s

Create an account or sign in to comment

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.