Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

###It's called main. It's not called everything.

It's called main. It's not called everything.

We should be breaking our code down into functions. We also have lots of quite large comment blocks. Properly written functions with good names will make our code self-documenting enough to cut down on the amount of comments required without reducing the readability of our code (because writing readable code is important).


Typically, when we are getting input from a user, we want to prompt them with what sort of input we're expecting. And I like to making this combination of prompting and getting input quite easy and natural, and it's typically something you're going to do repeatedly (as you've done here), so it's typically going to be one of the first functions I write:

std::string getInput(std::string prompt) {
 std::cout << prompt;
 std::string value;
 std::cin >> value;
 return value;
}

Of course, this probably isn't perfect--it only works when we want to grab strings, but I think it gives you an idea of just one of the ways we can break our code down into functions rather than turning main into everything.

The next most logic function to break out of here is a function for returning a vector of N people. That would look something like this:

std::vector<std::string> getPeople(int numberOfPeople) {
 std::vector<std::string> people = std::vector<std::string>();
 while (people.size() < numberOfPeople) {
 people.push_back(getInput("Enter the next person: "));
 }
 return people;
}

And now, in main, you're using that something like this:

int main() {
 std::vector<std::string> competitors = getPeople(getIntInput("How many people? "));
 // ...
}

This replaces the first ten lines of main with a single line and eliminates some unnecessarily variables. Moreover, we've replaced a lot of comments with appropriately named functions that make the code at least equally readable (arguably moreso because I can read it in line instead of having to read it, and then refer back to the comments above it), and I don't have to worry about remembering to update the comments if I've changed the code.

We should always strive to first break down our problems into the smallest possible units, and then solve those smaller problems one at a time.

###It's called main. It's not called everything.

We should be breaking our code down into functions. We also have lots of quite large comment blocks. Properly written functions with good names will make our code self-documenting enough to cut down on the amount of comments required without reducing the readability of our code (because writing readable code is important).


Typically, when we are getting input from a user, we want to prompt them with what sort of input we're expecting. And I like to making this combination of prompting and getting input quite easy and natural, and it's typically something you're going to do repeatedly (as you've done here), so it's typically going to be one of the first functions I write:

std::string getInput(std::string prompt) {
 std::cout << prompt;
 std::string value;
 std::cin >> value;
 return value;
}

Of course, this probably isn't perfect--it only works when we want to grab strings, but I think it gives you an idea of just one of the ways we can break our code down into functions rather than turning main into everything.

The next most logic function to break out of here is a function for returning a vector of N people. That would look something like this:

std::vector<std::string> getPeople(int numberOfPeople) {
 std::vector<std::string> people = std::vector<std::string>();
 while (people.size() < numberOfPeople) {
 people.push_back(getInput("Enter the next person: "));
 }
 return people;
}

And now, in main, you're using that something like this:

int main() {
 std::vector<std::string> competitors = getPeople(getIntInput("How many people? "));
 // ...
}

This replaces the first ten lines of main with a single line and eliminates some unnecessarily variables. Moreover, we've replaced a lot of comments with appropriately named functions that make the code at least equally readable (arguably moreso because I can read it in line instead of having to read it, and then refer back to the comments above it), and I don't have to worry about remembering to update the comments if I've changed the code.

We should always strive to first break down our problems into the smallest possible units, and then solve those smaller problems one at a time.

It's called main. It's not called everything.

We should be breaking our code down into functions. We also have lots of quite large comment blocks. Properly written functions with good names will make our code self-documenting enough to cut down on the amount of comments required without reducing the readability of our code (because writing readable code is important).


Typically, when we are getting input from a user, we want to prompt them with what sort of input we're expecting. And I like to making this combination of prompting and getting input quite easy and natural, and it's typically something you're going to do repeatedly (as you've done here), so it's typically going to be one of the first functions I write:

std::string getInput(std::string prompt) {
 std::cout << prompt;
 std::string value;
 std::cin >> value;
 return value;
}

Of course, this probably isn't perfect--it only works when we want to grab strings, but I think it gives you an idea of just one of the ways we can break our code down into functions rather than turning main into everything.

The next most logic function to break out of here is a function for returning a vector of N people. That would look something like this:

std::vector<std::string> getPeople(int numberOfPeople) {
 std::vector<std::string> people = std::vector<std::string>();
 while (people.size() < numberOfPeople) {
 people.push_back(getInput("Enter the next person: "));
 }
 return people;
}

And now, in main, you're using that something like this:

int main() {
 std::vector<std::string> competitors = getPeople(getIntInput("How many people? "));
 // ...
}

This replaces the first ten lines of main with a single line and eliminates some unnecessarily variables. Moreover, we've replaced a lot of comments with appropriately named functions that make the code at least equally readable (arguably moreso because I can read it in line instead of having to read it, and then refer back to the comments above it), and I don't have to worry about remembering to update the comments if I've changed the code.

We should always strive to first break down our problems into the smallest possible units, and then solve those smaller problems one at a time.

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

###It's called main. It's not called everything. It's called main. It's not called everything.

We should be breaking our code down into functions. We also have lots of quite large comment blocks. Properly written functions with good names will make our code self-documenting enough to cut down on the amount of comments required without reducing the readability of our code (because writing readable code is important).


Typically, when we are getting input from a user, we want to prompt them with what sort of input we're expecting. And I like to making this combination of prompting and getting input quite easy and natural, and it's typically something you're going to do repeatedly (as you've done here), so it's typically going to be one of the first functions I write:

std::string getInput(std::string prompt) {
 std::cout << prompt;
 std::string value;
 std::cin >> value;
 return value;
}

Of course, this probably isn't perfect--it only works when we want to grab strings, but I think it gives you an idea of just one of the ways we can break our code down into functions rather than turning main into everything.

The next most logic function to break out of here is a function for returning a vector of N people. That would look something like this:

std::vector<std::string> getPeople(int numberOfPeople) {
 std::vector<std::string> people = std::vector<std::string>();
 while (people.size() < numberOfPeople) {
 people.push_back(getInput("Enter the next person: "));
 }
 return people;
}

And now, in main, you're using that something like this:

int main() {
 std::vector<std::string> competitors = getPeople(getIntInput("How many people? "));
 // ...
}

This replaces the first ten lines of main with a single line and eliminates some unnecessarily variables. Moreover, we've replaced a lot of comments with appropriately named functions that make the code at least equally readable (arguably moreso because I can read it in line instead of having to read it, and then refer back to the comments above it), and I don't have to worry about remembering to update the comments if I've changed the code.

We should always strive to first break down our problems into the smallest possible units, and then solve those smaller problems one at a time.

###It's called main. It's not called everything.

We should be breaking our code down into functions. We also have lots of quite large comment blocks. Properly written functions with good names will make our code self-documenting enough to cut down on the amount of comments required without reducing the readability of our code (because writing readable code is important).


Typically, when we are getting input from a user, we want to prompt them with what sort of input we're expecting. And I like to making this combination of prompting and getting input quite easy and natural, and it's typically something you're going to do repeatedly (as you've done here), so it's typically going to be one of the first functions I write:

std::string getInput(std::string prompt) {
 std::cout << prompt;
 std::string value;
 std::cin >> value;
 return value;
}

Of course, this probably isn't perfect--it only works when we want to grab strings, but I think it gives you an idea of just one of the ways we can break our code down into functions rather than turning main into everything.

The next most logic function to break out of here is a function for returning a vector of N people. That would look something like this:

std::vector<std::string> getPeople(int numberOfPeople) {
 std::vector<std::string> people = std::vector<std::string>();
 while (people.size() < numberOfPeople) {
 people.push_back(getInput("Enter the next person: "));
 }
 return people;
}

And now, in main, you're using that something like this:

int main() {
 std::vector<std::string> competitors = getPeople(getIntInput("How many people? "));
 // ...
}

This replaces the first ten lines of main with a single line and eliminates some unnecessarily variables. Moreover, we've replaced a lot of comments with appropriately named functions that make the code at least equally readable (arguably moreso because I can read it in line instead of having to read it, and then refer back to the comments above it), and I don't have to worry about remembering to update the comments if I've changed the code.

We should always strive to first break down our problems into the smallest possible units, and then solve those smaller problems one at a time.

###It's called main. It's not called everything.

We should be breaking our code down into functions. We also have lots of quite large comment blocks. Properly written functions with good names will make our code self-documenting enough to cut down on the amount of comments required without reducing the readability of our code (because writing readable code is important).


Typically, when we are getting input from a user, we want to prompt them with what sort of input we're expecting. And I like to making this combination of prompting and getting input quite easy and natural, and it's typically something you're going to do repeatedly (as you've done here), so it's typically going to be one of the first functions I write:

std::string getInput(std::string prompt) {
 std::cout << prompt;
 std::string value;
 std::cin >> value;
 return value;
}

Of course, this probably isn't perfect--it only works when we want to grab strings, but I think it gives you an idea of just one of the ways we can break our code down into functions rather than turning main into everything.

The next most logic function to break out of here is a function for returning a vector of N people. That would look something like this:

std::vector<std::string> getPeople(int numberOfPeople) {
 std::vector<std::string> people = std::vector<std::string>();
 while (people.size() < numberOfPeople) {
 people.push_back(getInput("Enter the next person: "));
 }
 return people;
}

And now, in main, you're using that something like this:

int main() {
 std::vector<std::string> competitors = getPeople(getIntInput("How many people? "));
 // ...
}

This replaces the first ten lines of main with a single line and eliminates some unnecessarily variables. Moreover, we've replaced a lot of comments with appropriately named functions that make the code at least equally readable (arguably moreso because I can read it in line instead of having to read it, and then refer back to the comments above it), and I don't have to worry about remembering to update the comments if I've changed the code.

We should always strive to first break down our problems into the smallest possible units, and then solve those smaller problems one at a time.

added 223 characters in body
Source Link
nhgrif
  • 25.4k
  • 3
  • 64
  • 129

###It's called main. It's not called everything.

We should be breaking our code down into functions. We also have lots of quite large comment blocks. Properly written functions with good names will make our code self-documenting enough to cut down on the amount of comments required without reducing the readability of our code (because writing readable code is important).


Typically, when we are getting input from a user, we want to prompt them with what sort of input we're expecting. And I like to making this combination of prompting and getting input quite easy and natural, and it's typically something you're going to do repeatedly (as you've done here), so it's typically going to be one of the first functions I write:

std::string getInput(std::string prompt) {
 std::cout << prompt;
 std::string value;
 std::cin >> value;
 return value;
}

Of course, this probably isn't perfect--it only works when we want to grab strings, but I think it gives you an idea of just one of the ways we can break our code down into functions rather than turning main into everything.

The next most logic function to break out of here is a function for returning a vector of N people. That would look something like this:

std::vector<std::string> getPeople(int numberOfPeople) {
 std::vector<std::string> people = std::vector<std::string>();
 while (people.size() < numberOfPeople) {
 people.push_back(getInput("Enter the next person: "));
 }
 return people;
}

And now, in main, you're using that something like this:

int main() {
 std::vector<std::string> competitors = getPeople(getIntInput("How many people? "));
 // ...
}

This replaces the first ten lines of main with a single line and eliminates some unnecessarily variables. Moreover, we've replaced a lot of comments with appropriately named functions that make the code at least equally readable (arguably moreso because I can read it in line instead of having to read it, and then refer back to the comments above it), and I don't have to worry about remembering to update the comments if I've changed the code.

We should always strive to first break down our problems into the smallest possible units, and then solve those smaller problems one at a time .

###It's called main. It's not called everything.

We should be breaking our code down into functions. We also have lots of quite large comment blocks. Properly written functions with good names will make our code self-documenting enough to cut down on the amount of comments required without reducing the readability of our code (because writing readable code is important).


Typically, when we are getting input from a user, we want to prompt them with what sort of input we're expecting. And I like to making this combination of prompting and getting input quite easy and natural, and it's typically something you're going to do repeatedly (as you've done here), so it's typically going to be one of the first functions I write:

std::string getInput(std::string prompt) {
 std::cout << prompt;
 std::string value;
 std::cin >> value;
 return value;
}

Of course, this probably isn't perfect--it only works when we want to grab strings, but I think it gives you an idea of just one of the ways we can break our code down into functions rather than turning main into everything.

The next most logic function to break out of here is a function for returning a vector of N people. That would look something like this:

std::vector<std::string> getPeople(int numberOfPeople) {
 std::vector<std::string> people = std::vector<std::string>();
 while (people.size() < numberOfPeople) {
 people.push_back(getInput("Enter the next person: "));
 }
 return people;
}

And now, in main, you're using that something like this:

int main() {
 std::vector<std::string> competitors = getPeople(getIntInput("How many people? "));
 // ...
}

This replaces the first ten lines of main with a single line and eliminates some unnecessarily variables. Moreover, we've replaced a lot of comments with appropriately named functions that make the code at least equally readable (arguably moreso because I can read it in line instead of having to read it, and then refer back to the comments above it), and I don't have to worry about remembering to update the comments if I've changed the code.

###It's called main. It's not called everything.

We should be breaking our code down into functions. We also have lots of quite large comment blocks. Properly written functions with good names will make our code self-documenting enough to cut down on the amount of comments required without reducing the readability of our code (because writing readable code is important).


Typically, when we are getting input from a user, we want to prompt them with what sort of input we're expecting. And I like to making this combination of prompting and getting input quite easy and natural, and it's typically something you're going to do repeatedly (as you've done here), so it's typically going to be one of the first functions I write:

std::string getInput(std::string prompt) {
 std::cout << prompt;
 std::string value;
 std::cin >> value;
 return value;
}

Of course, this probably isn't perfect--it only works when we want to grab strings, but I think it gives you an idea of just one of the ways we can break our code down into functions rather than turning main into everything.

The next most logic function to break out of here is a function for returning a vector of N people. That would look something like this:

std::vector<std::string> getPeople(int numberOfPeople) {
 std::vector<std::string> people = std::vector<std::string>();
 while (people.size() < numberOfPeople) {
 people.push_back(getInput("Enter the next person: "));
 }
 return people;
}

And now, in main, you're using that something like this:

int main() {
 std::vector<std::string> competitors = getPeople(getIntInput("How many people? "));
 // ...
}

This replaces the first ten lines of main with a single line and eliminates some unnecessarily variables. Moreover, we've replaced a lot of comments with appropriately named functions that make the code at least equally readable (arguably moreso because I can read it in line instead of having to read it, and then refer back to the comments above it), and I don't have to worry about remembering to update the comments if I've changed the code.

We should always strive to first break down our problems into the smallest possible units, and then solve those smaller problems one at a time .

Source Link
nhgrif
  • 25.4k
  • 3
  • 64
  • 129
Loading
lang-cpp

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