-
Notifications
You must be signed in to change notification settings - Fork 33
Никонов Сергей #8
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
base: main
Are you sure you want to change the base?
Conversation
| throw std::runtime_error{"Not implemented"}; | ||
| int64_t x1 = a; | ||
| int64_t x2 = b; | ||
| return x1 + x2; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
две новые переменные создаватьизлишне, достаточно в return написать выражение, используя для одного из операндов static_cast
| if ((value & static_cast<uint8_t>(CheckFlags::DEST)) !=0) { | ||
| std::cout<< comma << "DEST"; | ||
| comma = ","; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
можно было избавиться от дублирования похожего кода
| constexpr double CENTIMETER = 0.01; | ||
| constexpr double FOOT = 0.3048; | ||
| constexpr double INCH = 0.0254; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
это хорошо, METER только лишний в данном случае
| } | ||
|
|
||
| std::cout << "0b"; | ||
| for (int i = static_cast<int>(bytes * 8 - 1); i >= 0; i--) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
префиксный декремент должен быть
| if (b == 0) { | ||
| std::cout << "no solutions"; | ||
| } | ||
| else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
лучше было сделать две короткие ветви с return без else и ещё оного уровня вложенности
|
|
||
| double discriminant = static_cast<double>(b) * b - 4.0 * a * c; | ||
| if (discriminant < 0) { | ||
| std::cout << "no solutions"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
аналогично, после лучше было поставить return а дальше убрать else, у последнего не было бы вложенности и не нужно было бы пролистать оставшийся код, чтоббы увидеть, что больше ничего в std::cout не добавляется общего для случаев
| double CalculateRMS(double values[], size_t size) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| } No newline at end of file | ||
| if (size == 0 || values == nullptr) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
на nullptr часто принято не сравнивать явно, а пользоваться неявным приведением указателей к bool, !values
| double mean_of_squares = sum_of_squares / size; | ||
| double rms = std::sqrt(mean_of_squares); | ||
|
|
||
| return rms; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
выше две лишние переменные, которые не используются ещё где-то, достаточно было аписать выражение сразу в return
| double ApplyOperations(double a, double b /* other arguments */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| double ApplyOperations(double a, double b, double (*operations[])(double, double), size_t size ) { | ||
| double sum = 0.0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
переменную нужно создать после прохождения условий по выходу из функций, всегда стараемся уменьшить область видимости переменных перемещая их определения ближе к непосредственному использованию
| /* return_type */ FindLastElement(/* ptr_type */ begin, /* ptr_type */ end, /* func_type */ predicate) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| const int* FindLastElement(const int* begin, const int* end, bool (*predicate)(int)) { | ||
| if (begin == nullptr || begin == nullptr || begin > end || begin == end) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
проверка с неявным приведением к bool была бы компактнее
| } No newline at end of file | ||
| void PrintMemory(double value, bool reverse = false) { | ||
| uint64_t bytes; | ||
| std::memcpy(&bytes, &value, sizeof(value)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
можно было переписать код чтобы не дублировать, вызывая для double и int третью функцию, куда передавать указатель, размер и флаг
| } | ||
|
|
||
| const char* FindLongestSubsequence(const char* begin, const char* end, size_t& count) { | ||
| char* result = FindLongestSubsequence(const_cast<char*>(begin), const_cast<char*>(end), count); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Это UB const_cast, в данном случае снимается константность с изначально константного объекта, это UB, корректно было всю работу перенести в константную версию и её вызывать из не константной, и тогда снимать с результата константность было бы безопасно, так как мы знали бы, что вызывали константную версию из не константной
18thday
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SaintNikon основные моменты
- мало заданий
- UB const_cast
- есть дублирования,которых можно избежать
No description provided.