Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gui pereira cpp exercises #91

Closed
wants to merge 4 commits into from
Closed

Conversation

GuiMacielPereira
Copy link

Description of work
Purpose of work
Second PR for the two cpp exercises.

Summary of work
Added 4 new files in total (following the instructions): two main.py files, one text input file (holmes.txt) and one text output file (for the word counts). The two main.cpp files were compiled using a simple g++ command, did not use CMake. Note that in the first exercise, if using CMake, the CMake source directory needs to be the same as the main.cpp file.

To test:
Nothing

@@ -0,0 +1,107 @@
/**

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably get this working with cmake. CMake is the industry standard make tool for c++, and mantid uses this. Mantids cmake workflow is.... complicated... so it's good get a grounding in this early.

}
buffer.str(line); // Write line to buffer
}
return *this; // Don't really understand this yet, returns pointer to the object itself?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this returns a pointer to the instance, the * before this dereferences it. Combined a reference to the object itself is returned.


buffer.clear();

// Apply punctuaction extraction filter

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, if you find yourself wanting to write a comment like this, refactor into a function called AppyPunctuationFilter or something.

while (!(buffer >> word_out)) { // Output here

// if (buffer.bad()) break; // Probably needs something like this, but I am too tired to think about which cases
if (!ist.good()) break; // !good() covers bad(), fail() and eof()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always prefer retaining the curly braces for clarity. I've seen so many bugs caused by these one line if statements.

}
}
// Count ocurrences of a word
std::map <std::string, int> counts = CountOcurrencesInVector(word_list);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given your implementation, you could use std::unordered_map which is just a straight up hash table so more efficient.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're looping over the words again here. Could we have counted them during the previous loop?

return ist.good(); // It makes more sense to be buffer.good() but that doesn't work
}

std::map<std::string, int> CountOcurrencesInVector(std::vector<std::string> all_words) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're passing by value here, which makes a copy of the vector. This can be expensive, so you will want to pass by reference or pointer if possible (ideally the former).

In addition, if you know you will not want to mutate the passed object in the function, you will want to pass a cost object.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's worth reviewing your use of variables throughout, after reading up on "const correctness". If it doubt, use a const.


std::map<std::string, int> counts;

for (std::string word : all_words) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically in range based for loops we prefer to use auto, and given that we don't want to create a new copy or change the item, we can just auto const &: for (auto const &word : all_words)

int base;
int radius;
std::string type;
Polygon (int h, int b) : height {h}, base {b} {};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using this design, the variable radius is uninitialized. This could lead to unpredictable behaviour, and in fact most linting tools (including ours) will not let it through.

class Rectangle: public Polygon {
public:
Rectangle (int h, int b) : Polygon(h, b) {type = "Rectangle";};
double get_area () {return height*base;};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an implicit cast here from int to double, as you are multiplying two ints (returning and int), then assigning it to a double.

Again, linters and compilers will likely complain about this.

By not using CMake your compiler probably isn't configured to output these warnings?

If you look in Cmake/CommonSetup.cmake there is this code block:

if(MSVC)
  add_compile_options(/W3 /WX)  # W3 avoids std lib warnings
else()
  add_compile_options(-Wall -Wextra -pedantic -Werror)
endif()

class Circle : public Polygon {
public:
Circle (int r) : Polygon(r) {type = "Circle";};
double get_area () {return 3.14*radius*radius;};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If our function does not mutate any of the class member variables, we can make it a const function:
double get_area () const {return 3.14*radius*radius;};

Installed cpplinter from Google and formatted my code accordingly.
Added const throughout function arguments and function attributes.
Split some big functions into separate functions.
Passed arguments either by reference or pointers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants