Skip to main content
Commonmark migration
Source Link

Reformat code

#Reformat code TryTry to use IDE options where you can reformat code, right now it is hard to read. There is everything in one place.

Create small functions

#Create small functions RightRight now, your code is a one big function. Firstly, divide your code into small functions that are doing one thing. You could end up with 10 or 15 really small functions. After that, thing about functions that are similar to each other, they are part of one bigger responsibility. You might be able to see some "objects" and create appropriate classes.

#Hardcoding values

Hardcoding values

You have code like this

std::ifstream file("C:\\Users\\MyPC\\Desktop\\Programs\\Seals.csv");

This can be done for example using command line arguments. You will be passing path to file when you start running your program, that will be much more flexible.

Next examples of hardcoding values

std::cout << database[sealChoice][14] << std::endl;
while (switchChoice <= 0 || switchChoice >= 4 || std::cin.fail())

Try to give these every number meaningful name, it will much more easier for readers to understand.

Strange code

#Strange code II cannot get why do you do this

   size_t dbsize = database.size();
   database.resize(dbsize + 1);

Personally, I use resize in only one case. Suppose that I know that my container will be filled with 10 numbers, so before starting pushing into the vector I resize to the mentioned size. You are doing something that will be done better and automatically (no need to write code like this).

Next

int dbval =  static_cast<int>(database.size());
int entryval = static_cast<int>(database[0].size());

I know that works, but I would go with simply

auto dbval =  database.size();
auto entryval = database[0].size();

#Reformat code Try to use IDE options where you can reformat code, right now it is hard to read. There is everything in one place.

#Create small functions Right now, your code is a one big function. Firstly, divide your code into small functions that are doing one thing. You could end up with 10 or 15 really small functions. After that, thing about functions that are similar to each other, they are part of one bigger responsibility. You might be able to see some "objects" and create appropriate classes.

#Hardcoding values

You have code like this

std::ifstream file("C:\\Users\\MyPC\\Desktop\\Programs\\Seals.csv");

This can be done for example using command line arguments. You will be passing path to file when you start running your program, that will be much more flexible.

Next examples of hardcoding values

std::cout << database[sealChoice][14] << std::endl;
while (switchChoice <= 0 || switchChoice >= 4 || std::cin.fail())

Try to give these every number meaningful name, it will much more easier for readers to understand.

#Strange code I cannot get why do you do this

   size_t dbsize = database.size();
   database.resize(dbsize + 1);

Personally, I use resize in only one case. Suppose that I know that my container will be filled with 10 numbers, so before starting pushing into the vector I resize to the mentioned size. You are doing something that will be done better and automatically (no need to write code like this).

Next

int dbval =  static_cast<int>(database.size());
int entryval = static_cast<int>(database[0].size());

I know that works, but I would go with simply

auto dbval =  database.size();
auto entryval = database[0].size();

Reformat code

Try to use IDE options where you can reformat code, right now it is hard to read. There is everything in one place.

Create small functions

Right now, your code is a one big function. Firstly, divide your code into small functions that are doing one thing. You could end up with 10 or 15 really small functions. After that, thing about functions that are similar to each other, they are part of one bigger responsibility. You might be able to see some "objects" and create appropriate classes.

Hardcoding values

You have code like this

std::ifstream file("C:\\Users\\MyPC\\Desktop\\Programs\\Seals.csv");

This can be done for example using command line arguments. You will be passing path to file when you start running your program, that will be much more flexible.

Next examples of hardcoding values

std::cout << database[sealChoice][14] << std::endl;
while (switchChoice <= 0 || switchChoice >= 4 || std::cin.fail())

Try to give these every number meaningful name, it will much more easier for readers to understand.

Strange code

I cannot get why do you do this

   size_t dbsize = database.size();
   database.resize(dbsize + 1);

Personally, I use resize in only one case. Suppose that I know that my container will be filled with 10 numbers, so before starting pushing into the vector I resize to the mentioned size. You are doing something that will be done better and automatically (no need to write code like this).

Next

int dbval =  static_cast<int>(database.size());
int entryval = static_cast<int>(database[0].size());

I know that works, but I would go with simply

auto dbval =  database.size();
auto entryval = database[0].size();
Source Link
Newbie
  • 710
  • 1
  • 7
  • 19

#Reformat code Try to use IDE options where you can reformat code, right now it is hard to read. There is everything in one place.

#Create small functions Right now, your code is a one big function. Firstly, divide your code into small functions that are doing one thing. You could end up with 10 or 15 really small functions. After that, thing about functions that are similar to each other, they are part of one bigger responsibility. You might be able to see some "objects" and create appropriate classes.

#Hardcoding values

You have code like this

std::ifstream file("C:\\Users\\MyPC\\Desktop\\Programs\\Seals.csv");

This can be done for example using command line arguments. You will be passing path to file when you start running your program, that will be much more flexible.

Next examples of hardcoding values

std::cout << database[sealChoice][14] << std::endl;
while (switchChoice <= 0 || switchChoice >= 4 || std::cin.fail())

Try to give these every number meaningful name, it will much more easier for readers to understand.

#Strange code I cannot get why do you do this

   size_t dbsize = database.size();
   database.resize(dbsize + 1);

Personally, I use resize in only one case. Suppose that I know that my container will be filled with 10 numbers, so before starting pushing into the vector I resize to the mentioned size. You are doing something that will be done better and automatically (no need to write code like this).

Next

int dbval =  static_cast<int>(database.size());
int entryval = static_cast<int>(database[0].size());

I know that works, but I would go with simply

auto dbval =  database.size();
auto entryval = database[0].size();