-
-
Notifications
You must be signed in to change notification settings - Fork 630
solution for challenge 3 #553
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
WalkthroughAdds a new Go program defining Employee and Manager types, with methods to add, remove, search employees and compute average salary. The main function constructs a Manager, adds two employees, removes one by ID, prints average salary, and attempts a lookup by ID. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Main
participant Manager
rect rgb(240,250,255)
note right of Main: Program start
Main->>Manager: instantiate Manager{Employees: []}
Main->>Manager: AddEmployee(emp1)
Main->>Manager: AddEmployee(emp2)
end
rect rgb(250,245,240)
Main->>Manager: RemoveEmployee(id=...)
Manager-->>Main: Employees updated
end
rect rgb(245,255,245)
Main->>Manager: GetAverageSalary()
Manager-->>Main: float64 average or 0
end
rect rgb(255,250,245)
Main->>Manager: FindEmployeeByID(id=...)
alt found
Manager-->>Main: *Employee
else not found
Manager-->>Main: nil
end
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Pre-merge checks❌ Failed checks (2 warnings, 1 inconclusive)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 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.
Actionable comments posted: 2
🧹 Nitpick comments (2)
challenge-3/submissions/kushalShukla-web/solution-template.go (2)
23-33
: Consider using range and removing redundant else.The logic is correct, but the code can be more idiomatic.
Apply this diff to simplify:
func (m *Manager) RemoveEmployee(id int) { var tempValue []Employee - for i := 0; i < len(m.Employees); i++ { - if m.Employees[i].ID == id { + for _, employee := range m.Employees { + if employee.ID == id { continue - } else { - tempValue = append(tempValue, m.Employees[i]) } + tempValue = append(tempValue, employee) } m.Employees = tempValue }
35-46
: Consider using camelCase naming and range loops.The logic is correct, but consider these optional improvements for consistency with Go conventions.
Apply this diff:
func (m *Manager) GetAverageSalary() float64 { if len(m.Employees) == 0 { return 0 } - var averagesalary float64 - for i := 0; i < len(m.Employees); i++ { - averagesalary += m.Employees[i].Salary + var averageSalary float64 + for _, employee := range m.Employees { + averageSalary += employee.Salary } - averagesalary = averagesalary / float64(len(m.Employees)) - return averagesalary + return averageSalary / float64(len(m.Employees)) }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
challenge-3/submissions/kushalShukla-web/solution-template.go
(1 hunks)
🔇 Additional comments (2)
challenge-3/submissions/kushalShukla-web/solution-template.go (2)
19-21
: LGTM!The implementation correctly appends the employee to the slice.
57-68
: LGTM!The main function effectively demonstrates the Manager functionality with clear examples of adding, removing, computing averages, and finding employees.
func (m *Manager) GetEmployee() { | ||
|
||
} |
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.
Remove or implement the GetEmployee stub.
This method is empty and has no return value. If it's not needed, remove it. Otherwise, implement the intended functionality.
func (m *Manager) FindEmployeeByID(id int) *Employee { | ||
for i := 0; i < len(m.Employees); i++ { | ||
if m.Employees[i].ID == id { | ||
return &m.Employees[i] | ||
} | ||
} | ||
return nil | ||
} |
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.
Potential dangling pointer after slice modifications.
Returning a pointer to a slice element (&m.Employees[i]
) becomes unsafe when RemoveEmployee
is called, as it rebuilds the slice. The returned pointer may then reference invalid or incorrect data.
Option 1: Return by value instead of pointer
-func (m *Manager) FindEmployeeByID(id int) *Employee {
- for i := 0; i < len(m.Employees); i++ {
- if m.Employees[i].ID == id {
- return &m.Employees[i]
+func (m *Manager) FindEmployeeByID(id int) (Employee, bool) {
+ for _, employee := range m.Employees {
+ if employee.ID == id {
+ return employee, true
}
}
- return nil
+ return Employee{}, false
}
Then update the caller in main:
- employee := manager.FindEmployeeByID(2)
- if employee != nil {
- fmt.Printf("Employee found: %+v\n", *employee)
+ employee, found := manager.FindEmployeeByID(2)
+ if found {
+ fmt.Printf("Employee found: %+v\n", employee)
}
Option 2: Use a map instead of slice for O(1) lookups
This would also eliminate the pointer safety issue and improve performance.
🤖 Prompt for AI Agents
In challenge-3/submissions/kushalShukla-web/solution-template.go around lines
48–55, the function returns a pointer to a slice element (&m.Employees[i]) which
can dangle after slice modifications; change the API to return the Employee by
value (and a found flag) instead of a pointer: update the function signature to
return (Employee, bool), return the matched element by value with true, and
return (Employee{}, false) when not found; then update all callers in main/tests
to handle the value + bool pattern. As an alternative (optional) refactor,
replace the slice with a map[int]Employee for O(1) lookups and safe value
returns.
No description provided.