Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to centrally maintain a mathematical formula in C# (web) so it can be changed if needed?

We have an application that has a LOT of mathematical checks on the page and according to it, the user is given a traffic light (Red, green, yellow).

Green = He may continue
Red = Dont let him continue
Yellow = Allow to continue but warn

These formulas operate on the various text-fields on the page. So, for example, if textbox1 has "10" and texbox2 has "30"... The formula might be:
T1 * T2 > 600 ? "GREEN" : "RED"

My question is:
Is it possible to somehow centralize these formulas?

Why do I need it?
Right now, if there is any change in a formula, we have to replicate the change at server-side as well (violation of DRY, difficult to maintain code)

One option could be to
- store the (simple) formula as text with placeholders in a config(?)
- replace the placeholders with values in javascript as well as server-side code
- use eval() for computation in JS
- use tricks outlined here for C#

In this approach issue could be different interpretations of same mathematical string in JS and C#.

Am i making sense or should this question be reported?!! :P

like image 588
Ramanpreet Singh Avatar asked Jan 22 '13 10:01

Ramanpreet Singh


People also ask

How do you write mathematical expressions in C?

In C, just as in virtually all programming languages, the plus sign (+) is used to add two values, the minus sign (−) is used to subtract two values, the asterisk (*) is used to multiply two values, and the slash (/) is used to divide two values.

What is the order we use to execute a mathematical operation in C?

Here all arithmetic operators are used. As the addition, subtraction, division and multiplication operators are used in the given expression, higher priority operator executes first, and then other operator executes. So, multiplication and division operator execute first.


1 Answers

Depending on your application's requirements, it may be acceptable to just do all the validation on the server. Particularly if you have few users or most of them are on a reasonably fast intranet, you can "waste" some network calls to save yourself a maintenance headache.

If the user wants feedback between every field entry (or every few entries, or every few seconds), you could use an AJAX call to ask the server for validation without a full page refresh.

This will, of course result in more requests than doing the validation entirely on the client, and if many of your users have bad network connections there could be latency in giving them the feedback. My guess is the total bandwidth usage is about the same. You use some for every validation round-trip, but those are small. It may be outweighed by all that validation JS that you're not going to send to clients.

The main benefit is the maintenance and FUD that you'd otherwise have keeping the client and server validation in sync. There's also the time savings in never having to write the validation javascript.

In any case, it may be worth taking a step back and asking what your requirements are.

like image 100
solublefish Avatar answered Nov 15 '22 16:11

solublefish