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.

11
  • 1
    Does this answer your question? Approaches for checking multiple conditions? Commented Jan 27, 2023 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. Commented Jan 27, 2023 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. Commented Jan 28, 2023 at 0:01
  • 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 Commented Jan 28, 2023 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 Commented Jan 28, 2023 at 12:25

1 Answer 1

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.

3
  • 1
    For those of you playing the home game, the second code example is effectively the Builder Pattern, or a variation thereof. Commented Jan 28, 2023 at 16:23
  • AKA Fluent Interface/Method Chaining Commented Jan 28, 2023 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. Commented Jan 28, 2023 at 16:46

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.