Mark
Mark

Reputation: 9919

Best pattern to implement multiple conditional logic

Firstly, I have read, and searched ways to refactor conditional logic - seems that 3 main approaches are suggested : Polymorphism (haven't tried, but don't think I can apply it this situation), Enums (have used) and The Strategy Pattern (I have used several times and like it).

However I have about 6-7 boolean conditions to check, and depending whether each one is true/false I want to do something different i.e.

true, false, false, true

false, false, true, false

true, true, ... you get the picture..

The boolean values are set by different preferences, that are all related in someway, but I need to handle differently depending on which ones are true or false. The amount of preferences could well increase as well, so something that is scalable and maintainable is what I'm after.

I know I can use The Strategy Pattern here, but not without a lot of conditional checks first (which I'm trying to avoid).

Example situation, the project centres around a music app, and specifically what to do when a track has finished, and what options dictate what happens next i.e :

SHUFFLE

REPEAT

PLAY_FROM_ALBUM_SONGS

PLAY_FROM_ARTIST

PLAY_FROM_GENRE

PLAY_FROM_ALL_SONGS

So a basic example would be the first and last are true (Shuffle & Play from all songs) and rest are false. some cases if one is true, another has to be false - you cant play all songs from your library if you're playing just an album, so when some options are chosen they automatically change other ones they directly affect.

Any suggestions how this sort of conditional logic, based on multiple conditions (6/7+), can be refactored so it wont look like a behemoth of ugly code.

Upvotes: 1

Views: 1453

Answers (1)

user4668606
user4668606

Reputation:

In your example you should start by classifying the conditions. Some are exclusive, some may influence one another, and all can be grouped based on what part of the behaviour they specify.

In your example the classification might look like this:

  • Song selection: FROM_ALBUM, FROM_ARTIST, FROM_GENRE, FROM_ALL_SONGS
  • Repetition: REPEAT
  • Order: SHUFFLE

Repetition and Order each only contain one element, and song-selection is based on a hierarchy from left to right. If FROM_ARTIST is true, FROM_ALBUM is implicitly true as well, since an album is a subset of tracks produced by the artist (unless you count features as well, but I'll ignore this here). So after the classification, things look quite a lot simpler already.

Since REPEAT and SHUFFLE handle their own specific behavior without influence from any other flag, we can handle them via a trivial if-clause, or some workaround that maps each property to a specific action. For the other flags, the simplest would be to utilize the hierarchy and search for the flag that covers the largest set of songs and set it up as filter.

The main-trick here wouldn't be to apply any design-pattern, but actually to classify the flags based on their meaning.

Upvotes: 3

Related Questions