Hey, I’ve finished the time calculator and it runs without fails or errors but I was wanting some help to see ways I could make my code more concise. I feel like I’ve definitely done this the long way and with way more if statements than needed. Also with the two day later test I had to write in a specific condition for it as it kept on saying next day instead of 2 days later and I’m wondering if there is a way to write my code where I don’t need to write an if condition just for that test and scenario. My code is below:
You are right, the number of if
s is sticking out a bit.
The good news is this is the perfect time to look at them and the rest of code again. Refactor and make it cleaner and shorter. Everything is working, so now your understanding of the problem and what is needed, is much better.
Speaking of the if
s again, it’s possible there are parts that are not really different between current various cases. They can be extracted outside of the if
s. Take a look:
new_time = str_end_hour + ":" + str_end_mins + " AM" + final_day + day
new_time = str_end_hour + ":" + str_end_mins + " PM" + final_day + day
new_time = str_end_hour + ":" + str_end_mins + " PM" + final_day
new_time = str_end_hour + ":" + str_end_mins + " AM" + final_day + day
new_time = str_end_hour + ":" + str_end_mins + " AM" + final_day + " (next day)"
new_time = str_end_hour + ":" + str_end_mins + " PM" + final_day + day
new_time = str_end_hour + ":" + str_end_mins + " AM" + final_day
new_time = str_end_hour + ":" + str_end_mins + " PM" + final_day
new_time = str_end_hour + ":" + str_end_mins + " AM" + final_day + day
new_time = str_end_hour + ":" + str_end_mins + " PM" + final_day + day
new_time = str_end_hour + ":" + str_end_mins + " AM" + final_day + day
new_time = str_end_hour + ":" + str_end_mins + " PM" + final_day
new_time = str_end_hour + ":" + str_end_mins + " AM" + final_day + day
Another important thing to keep in mind, focus on making small changes, splitting bigger changes into little steps. Because there are tests, after each change you can make sure everything is still working as expected.
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.