-2

I know many people have suggested to me to not use code such as

using namespace std;

or

system ("pause");
system ("cls");
system ("color f0");//and other system commands

or

goto TitleScreen;
goto DeathScreen;

or

break;

and I'm just wondering why these things are looked down upon and what C++ commands can replace the bad habits of mine?

Andrew Tew
  • 47
  • 1
  • 8
  • 2
    So you really don't know why `goto` is looked down upon? Ever hear of spaghetti code? – PaulMcKenzie Nov 19 '15 at 01:05
  • 1
    Most of those are looked down on, and usually for good reasons, but what makes you think that `break` is? It is not. – Remy Lebeau Nov 19 '15 at 01:06
  • I am not sure who said `break` but it is definitely used in a lot of cases, such as `switch`. For `goto`, in almost most cases, you will not need to use it, if you design your code properly. To me `goto` decreases the readability of the code. – macroland Nov 19 '15 at 01:07
  • https://xkcd.com/292/ :) – Keale Nov 19 '15 at 01:09
  • @macroland `goto` also decreases your chances of someone looking at your code if you have a bug. At least for me, if I see `goto`, I don't look at the code any further, unless I *have* to. – PaulMcKenzie Nov 19 '15 at 01:10
  • There are appropriate uses of all of these, including `goto`. (state machine implementations, for example, are often done using `goto`s) – Joe Nov 19 '15 at 01:18
  • 1
    There is no guarantee that the operating system your program is running on has a "pause" command. Also, you can't guarantee the platform has a screen. Some platforms run programs in a "command window", which may not be cleared. Some platforms that support terminals, may not support color terminals. – Thomas Matthews Nov 19 '15 at 01:27

1 Answers1

7

Okay, let's go one-by-one. I will be posting links to great post by some other people, there's no points reinventing the wheel, and they've done a pretty good job.

using namespace std;

Why is "using namespace std" considered bad practice?

system ("pause");
system ("cls");
system ("color f0");//and other system commands

Why system() is evil

break;

There's nothing necessarily bad about break if used in moderation. Too many breaks in a for loop might suggest your logic is getting a bit too complex, and may be factored out, but in general most 'dangers' with break and continue can be alleviated by writing clear code.

Community
  • 1
  • 1
Tomasz Kaminski
  • 900
  • 7
  • 14