I'm curious if there is a pattern or at least a better way to code this situation. For example, say you're writing a rest api for a reporting workflow. You have a User class and a Job class. Each User has a Job, and each Job a JobType. Some actions will be the same for the user but have additional logic based on their Job Type - submitting a Report for a user in Finance or Legal will have some more logic involved than an HR user's Report. Based off of only the User's id I want to check if their report requires extra work and then execute the proper logic.
Pseudocode:
SubmitReportForUser(int userId, Report report) {
DoCommonReportWork(report)
if (ReportsNeedMoreWork(userId)){
DoExtraReportWork(userId, report);
}
}
ReportsNeedMoreWork(int userId) {
JobType = GetJobTypeForUser(userId); // Get from db
return (JobType == Finance || JobType == Legal);
}
DoExtraReportWork(userId, report) {
JobType = GetJobTypeForUser(userId); // Get from db
if (JobType == Finance) { DoFinanceReportWork(report); }
else if (JobType == Legal) { DoLegalReportWork(report); }
}
Having to load the JobType twice and doing multiple checks on what the JobType is both come off as code smells to me, but I'm not sure the best way to go about this situation. The only immediate fix I see would be to load the JobType earlier and have ReportsNeedMoreWork
and DoExtraReportWork
take the JobType instead of the user id, but that doesn't get around the duplicated if/elses. This is a simplified example, in my case there are many more JobTypes that would involve extra work.
Thanks for reading so far, any feedback is appreciated! This is an old codebase I'm working with, but looking to improve upon. Thanks!