Skip to main content
Code Review

Return to Revisions

3 of 3
Commonmark migration

Avoid Global Variables

In the code there are 2 global variables:

int minEle = 0;
stack<int> s;

Using global variables is something that all experienced programmers avoid. Global variables make the code very hard to write, debug and maintain. It is very difficult in programs larger than this one to local where a global variable is modified. Global variables can also cause C and C++ program that consist of multiple files not to link into an executable image. This is discussed on stackoverflow.com, however, if you do a Google Search on why are global variables bad you will find many more references.

Avoid using namespace std;

If you are coding professionally you probably should get out of the habit of using the using namespace std; statement. The code will more clearly define where cout and other identifiers are coming from (std::cin, std::cout). As you start using namespaces in your code it is better to identify where each function comes from because there may be function name collisions from different namespaces. The identifiercout you may override within your own classes, and you may override the operator << in your own classes as well. This stack overflow question discusses this in more detail.

Variable Types

It would be better to use the variable type size_t rather than int for the variables t and n. The restrictions on both t and n indicates that the value will never be less than 1, which means the value will never be less than zero. The variable type size_t is unsigned.

Variable Names

Single letter variable names such as t, n and s make reading and debugging code very difficult. The variable names should really indicate what the variable is for, examples testCount, elementCount, specialStack. The variable minEle, might be better named as minElement.

pacmaninbw
  • 26.1k
  • 13
  • 47
  • 114
default

AltStyle によって変換されたページ (->オリジナル) /