Writing clean code can be a challenge when you start on a new project. Trying to clean up code in an already existing application without breaking ...
For further actions, you may consider blocking this person and/or reporting abuse
If
switch
is implemented by the compiler as a look-up table it might offer somewhat better performance, especially as the number of options grows. Actually, I often preferswitch
to chainedif
s as to me it's more aesthetic; it's saying clearly "Here's a set of options to choose from". You can still implementreturn
instead ofbreak
in any of the cases. But I guess a lot of things are down to personal preference.Whenever I see multiple choices of this kind I like to use tables, so I'd try something like this, which also permits easy initialization from JSON (warning: untested code):
Great point! This was just to illustrate an example but yours illustrates it even better :)
Thanks for sharing it!
To be honest, I would prefer to have a designated factory to create these things so as not to mix behavioural and creational logic in one class.
I'd agree with you there Mikhail. It makes the overall program so much more concise and readable.
One of the joys (and frustrations) of JavaScript is there are so many different ways of doing the same thing. As I typed the words "look-up table" I suddenly recognized a pattern, where all the text could be extracted into constant data separate from the behavior. Sometimes you approach these things sideways and the pattern leaps out at you from nowhere.
if I were to refactor the getName function, I would simply do:
or even:
It includes points 1) and 2), and I think is simpler and more readable.
I don't understand what's wrong with the switch statement? Instead of assigning the variable you could have return for each case and this will also save you the break statement as well.
Moreover instead of calling track.getGenre() once, you do that multiple times, and functions may implement complex logic.
Was going to comment with the same. You can even ditch line 2
let backgroundImage: BackgroundImage;
now this variable isn't used.Yep, forgot that one.. lint usually reminds me in this case :)
100% agreed about calling track.getGenre() once, should definetely do that.
as for that last example, that's actually the good one, where you return from each case, which IMO helps keep the flow of the code linear and simple
Number 5 - assign track.getGenre() to a string once at the beginning and compare to that variable.
var genre = track.getGenre();
if (genre == "jazz"){}
...
No need to run the overhead of whatever logic is involved in getGenre() in each if.
good call! thanks for spotting it :)
Hypothetical question: What happens if you add a new genre and you don’t want the default banner to be returned?
What I’m getting at is that it’s much safer to throw in a default. Or if it’s typescript it’s safer to return a
never
type. That way you can exhaustively consider enum values or any union of types. I describe that technique here.Good point, for sure a switch statement should have a default.
I didn't include it in this case for brevity, and this is just an example. For something slightly more evolved I would actually resort to the factory pattern to create my return types and simplify the creation logic even more
Thank you for this post.
It sums up very nice, simple advices to make code cleaner 👌
Inline Variable (related to #4) and Extract Variable (related to #2) are the two refactorings I'm probably using the most, on a daily basis.
I'm currently working on a VS Code Extension to get the editor do these refactorings automatically for us. My goal is to make these typical operations easy to use.
For example, I can now solve #5 very quickly with Remove Redundant Else.
Maybe you'd be interested in using it.
It's called Abracadabra: bit.ly/vscode-abracadabra
If you feel like trying it, I'd love to get your feedbacks, so I can make it better.
Thanks again for sharing these practices. Have a nice day 😉