0

I have the code but I want to be pseudo as possible so I learn from other ways as much as possible.

So what I am building is web based tool and in one section we have a table.

This table renders lines of data given it matches conditions.

So the way it works is user enters a number and based on that we out specific lines in the front end.

E.g

If(one) return lineA

If(two) return lineB . ...

You can see I already I need a minimum of two views but let's say there 20 separate conditions that means 20 separate views.

Then because it's accounting calculations these could be a combo of any, many or all.

The way I've currently done is as follows:

Calcs
If(one) return lineA
If(two) return lineB
If(one and two) return lineA + lineB

Delegation
If(one) return viewA
If(two) return viewA
If(one and two) return viewA + viewB

Views
viewA()
viewB()

Obviously you can see I'm going down spaghetti lane.

Is there anyway to make this work with few conditions as possible?

It would help if you've coded such tools before.

Do I/should I stop coding and draw a large matrix of all possible combos first?

I think these two are irrelevant here but: If it helps this is all done in JS and Angular.

If it helps this is for a accounting/finance tool.

  • StringBuilder result = ""; if(one) result.append(line1); if(two) result.append(line2); if(three) result.append(line3); return result.toString(); – user253751 Jan 27 '23 at 23:27
  • 1
    Does this answer your question? [Approaches for checking multiple conditions?](https://softwareengineering.stackexchange.com/questions/191208/approaches-for-checking-multiple-conditions) – gnat Jan 27 '23 at 23:44
  • 1
    Thanks guys! That's a good link gnat. From the info therein I see they have similar issue and there is no one key fits all solution. I guess some stuff I can do to start making it better is add: 1. exit earlys 2. Reduce function complexity ....and I think this may start to reduce if condition, length and overall design etc. – Codemenot Jan 27 '23 at 23:55
  • 3
    You would probably get a lot better help when you post parts of your *real* code at Codreview.Stackexchange. Your example above is way-too-contrived to be useful. For example, you wrote "user enters a number". Lets call that number x. So what does "If(one)" mean? Does that mean "If(x==1)"? But what the heck then is the meaning of "If(one and two)"? Obviously, it cannot mean "If(x==1 and x==2)". If you alienate your code up to the point where it makes no sense, it becomes hard to give you a sensible answer. – Doc Brown Jan 28 '23 at 00:01
  • At user253751 I know this approach can work well but I have multiple lines sometimes for any condition too and sometimes I think we need to say only if two variables are true and explicitly say third is not to return a view. Maybe I am overthinking maybe I need to re draw to make cleaner from scratch. – Codemenot Jan 28 '23 at 00:02
  • Good point Doc Brown! I have alienation too....as it does become too vague. Let me take that exact snippet and post on their and then add a link here incase any of you here can answer it there. – Codemenot Jan 28 '23 at 00:05
  • Doc Brown before I do that is my point about the matrix table not a valid one? I think it would help if I draw one and try to include every possible combo. I may need a large whiteboard or maybe in a SVG so I can zoom and out as there will be lots conditions. So I can count how many conditions first on paper then reduce them , if I can. I think this could be issue of X's and Y's. Or no? – Codemenot Jan 28 '23 at 00:12
  • It also depends what language you are using. – user949300 Jan 28 '23 at 01:39
  • 1
    @Codemenot , we lack context to understand everything you're talking about as we're not working on your project, so when you're being too vague, it leads to confusion. For example, until I read your most recent comment, I didn't understand that by "draw a large matrix" you meant to draw it *on paper*, so that you can figure out stuff - I thought were talking about rendering it, "draw" it as part of your Angular page. But sure, go ahead, try it - you don't need to ask anyone if you should investigate the problem you're facing. 1/2 – Filip Milovanović Jan 28 '23 at 12:24
  • 1
    @Codemenot - That said, one way to solve this is, you create a class of the form `{ condition, thingToShow }`, then you create a bunch of these objects with different conditions (which are just functions that return a bool), and stick them in a list, and then you filter them so that you take only those where `condition(yourInput)` returns true. Then you combine the all the different `thingToShow` of the remaining objects, and you render that. 2/2 – Filip Milovanović Jan 28 '23 at 12:25
  • `return lineA + lineB` is unreachable after the early returns above it. Move this line to the top if you ever want to give it a chance to be called. – candied_orange Jan 28 '23 at 22:40

1 Answers1

2

You have many conditions, and you have results that are created by combining data for true conditions.

Creat a class that can combine these values. Then your code might look like

   StringResult calcs;
   calcs.addConditional (one, lineA);
   calcs.addConditional (two, lineB);
   return calcs.result;

Or

   return StringResult.create()
         .addConditional (one, lineA)
         .addConditional (two, lineB)
         .result;

The addConditional is as clever or as stupid as you want.

gnasher729
  • 42,090
  • 4
  • 59
  • 119
  • 1
    For those of you playing the home game, the second code example is effectively the Builder Pattern, or a variation thereof. – Robert Harvey Jan 28 '23 at 16:23
  • AKA Fluent Interface/Method Chaining – Robert Harvey Jan 28 '23 at 16:29
  • 1
    More fluent method chaining, because the object “calcs” is actually thrown away at the end of the method. So with method chaining, an object that I don’t care about (calcs) isn’t even visible in my source code. – gnasher729 Jan 28 '23 at 16:46