0% found this document useful (0 votes)
2 views6 pages

Code Review Template

code review cs362 osu

Uploaded by

christran751
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Code Review Template

code review cs362 osu

Uploaded by

christran751
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Activity 3 - Code Review Hands On

Code Snippets to Review


You will need to provide feedback as demonstrated in the assignment description for the
following five snippets. Please use this template document to complete this assignment.
You will need to provide written feedback and also “corrected” code just like in the
examples in the assignment description.
Name: Christopher Tran
email: [email protected]

Snippet 1

Feedback: Good job making the code concise. Plus, good job for correctly performing
division using the / operator (correctly using the forward slash to indicate a division).
Having said that, the name of the function and parameter provides no clue about what the
purpose of your function is supposed to do. The name x and y are just too way, vague. My
suggestion would be to name the variables and the function itself (i.e., divide_num) with a
more descriptive name and to also include a docstring that explains the function’s purpose,
expected inputs, and output. That way, it will improve code readability and maintainability.
Additionally, and this is important, I noticed that your code has no error handling for
division by zero, which can lead to a ZeroDivisionError.
Lastly, your code does have unnecessary blank lines and the lack of spacing to separate the
two parameters of x and y, which can cause some linting errors.
Corrected Code:
Snippet 2

Feedback: First of all, I like your use of a descriptive function name and inputs (e.g.,
employee) so that users can have an idea of what the function is supposed to do. I also like
how descriptive you wrote your docstring, explaining further what the function is
supposed to accomplish. Also, a good job in recognizing that the argument employees is a
list of objects that contain instances with status, rate, and hours attributes. A bonus for
using the += operator to make the code more Pythonic.
One issue I noticed was your use of the name x in your for loop. The variable name x is too
vague. I recommend using a more meaningful name, like employee.
Also, instead of having 2 for loops, you could iterate through the employee list once, while
using conditional statements (if and elif) to differentiate between the different employee
statuses (minimum vs supervisor). You want to do this to keep/make the code DRY.
Currently, your code is WET, since you loop through the list 2 times on separate occasions
Corrected Code:
Snippet 3

Feedback:

Good job on your use of a descriptive function and argument name for your member
function. Plus, nice descriptive docstring that helps explain what the function is supposed
to do. All of this made the intent of the function very clear. Although, I would recommend
cleaning it up a bit.

Also, it is very good that you realize that you did not need an additional else statement
because once the loop ends without finding a match, then found will stay as it is and return
False. It shows that you attempted to make your code DRY.

First and foremost, because we are simply just searching for a match, you could make your
code more Pythonic by simply using the built-in keyword called in instead of writing a
whole for loop, and defining a variable called found.

Technically, since the purpose of the function is to search, you can immediately return the
condition itself. That way, the command val in values (which is essentially the same as a for
loop) can stop executing once it finds that value. This avoids unnecessary iterations and
makes your function faster. As of now, the “loop” will continue to run, even if val happens
to be the first element found.

Before, your code would simply be iterating through the entire list and checking each
element even if val was already found. Thus making it WET. With these adjustments, it
should be DRY.

Corrected Code:
Snippet 4

Feedback: I like your use of descriptive function names, arguments, variables,you’re your
use of comments that helps make the intent of the function very easy to understand. I also
like your decision to use list comprehension.
As opposed to using string concatenation, I would suggest using f-strings because not only
do they make the code more readable, but they are also faster since we do not have to
manually convert non-string data types. In this case, your original code attempted to
concatenate a float data type with a string data type, which can cause a Type Error.
I also seem to notice that you don’t account for cases where the list might be empty. In this
case, a ZeroDivisionError because an empty list will have a length of 0. So, make sure to
include a way to check if the list is empty. I recommend using an inline if statement (as
opposed to a regular if block statement) because we are assigning values based on a
condition to one variable (value)
Aside from that, you do have some linting errors in your code. A line should not be longer
than 79 characters. In your case, the first line of your docstring is at 86 characters, which
will cause a linting error. That, and your docstring, seems to be quite confusing and unclear.
More specifically, the use of the word processes is vague, e.g., what kind of processing?
Lastly, because functions should do exactly one thing, I think perhaps you should separate
them, i.e., one function that processes (rounds) a list of numerical values and returns the
updated list, and a second separate function that’s whole purpose is to calculate the
average and then display the result of the list. But when doing this way, make sure to
account for if the list is empty, by using an inline if statement (as opposed to a regular if)
because we are assigning values based on a condition to one variable (value)
Corrected Code:

Or, if we want to separate the two functions


Snippet 5

Feedback: I like your use of a descriptive function name because it clearly communicates
its purpose. The variable name is also short, but at the same time, we can tell what it is
supposed to represent, i.e., a list or array.
Overall, your logic is easy to follow. This is a good starting point.

One issue I noticed is that you are attempting to modify the list in place while iterating over
it at the same time. This can be problematic because removing elements at the same time of
iterating the list will cause index shifting which can cause some elements to be skipped or
resulting in unexpected behavior. For example, when an item is removed, all elements to
the right of that element will be shifted to the left, so the next item may be skipped by the
loop.
Another issue is that doing it your way will result in making changes to the original list arr
because we are removing elements from it, which is not what is intended, as stated by your
docstring.
So, to combat this, I would recommend you use list comprehension to create a separate
array (i..e, even_arr) that will look at the original array for any even numbers and copy that
number into the new list. This way, we can keep the original list (arr) intact.
And finally, another small issue I noticed is related to your docstring. In particular, you
misspelled integers.
Corrected Code:

You might also like