Refactoring Java Code: How to refactor code to write cleaner and more maintainable code.

preview_player
Показать описание

In this video we go through some more refactoring techniques to write cleaner code with this homework submissions.

--------
Here are the instructions for this assignment:
You are driving a little too fast, and a police officer stops you.
Write a method to compute the result:
0 = no ticket
1 = small ticket
2 = big ticket

If speed is 60 or less, the result is 0.
If speed is between 61 and 80 inclusive, the result is 1.
If speed is 81 or more, the result is 2.

Unless it is your birthday, on that day, your speed can be 5 higher in all cases.

caughtSpeeding(60, false) → 0
caughtSpeeding(65, false) → 1
caughtSpeeding(65, true) → 0
-------

Рекомендации по теме
Комментарии
Автор

nice video pal !

just some point to clarify :

1.extracting the if conditions to new method, i guess it was not much needed here as the conditions were pretty straightforward
2.does making the local variables as final helps jit compiler in some sort of optimization ?


Thanks

SurajYadav-ydcu
Автор

Nice video. Thanks!! I learned a lot. BTW what about using a switch case instead of if-else if-else ?

jaihind
Автор

Good job explaining your thought process, I learned a bit from watching this. I'm fairly new to programming and was curious about a few decisions you made.

Wouldn't this have been much cleaner to simply decrement the speed by 5 when isBirthday is true? Adjusting the input would reduce any changes required downstream. The code appears structured but I had trouble with readability. It's a bit confusing that Ticket object be in a state where it's not a ticket? Maybe TrafficStop would be more appropriate than Ticket.

jeffsmith