0

I have a script as follow :

Int MethodOne (int param) {
    If (param > 5) {
        Return 0;
    }
    Return param;
}

Int MethodTwo (int param) {
    If (param > 5) {
        Return 0;
    }
    Return param * anotherVariable;
}

Is it possible to apply the conditional 'method should not execute if param > 5' to all my methods without rewriting it inside every method?

Souk21
  • 91
  • 1
  • 7

5 Answers5

4

One way is not to pass an int (see primitive obsession)

public class NotMoreThanFive 
{
  public int Value {get; private set;}
  public NotMoreThanFive(int value) 
  {
    Value = value > 5 ? 0 : value;
  }
}

now

Int MethodOne (NotMoreThanFive param) {
    Return param.Value;
}

Int MethodTwo (NotMoreThanFive param) {
    Return param.Value * anotherVariable;
}
Paul D'Ambra
  • 7,629
  • 3
  • 51
  • 96
1

I don't know a way of doing what OP asked, so here is the best answer I could come up with. One way could be to create another Restriction() method so you can change it once and not in every method.

bool Restriction(int param)
{
    return param <= 5;
}

int MethodOne(int param)
{
    return Restriction(param) ? param : 0;
}

int MethodTwo(int param) 
{
    return Restriction(param) ? param * anotherVariable : 0;
}
Arthur Rey
  • 2,990
  • 3
  • 19
  • 42
  • And how this satisfy the condition _without rewriting it inside every method_? – Steve Nov 21 '15 at 10:46
  • Well, it doesn't. But I don't know a way of doing what OP asked, so I tried to give the best answer I could come up with. – Arthur Rey Nov 21 '15 at 10:47
  • 1
    I agree with you, but your comment should be added at the beginning of your answer – Steve Nov 21 '15 at 10:48
1

You can use Function.

Func<int,int> restrict = x => x <= 5 ? x : 0;

int MethodOne (int param) {
    return restrict(param);
}
int MethodTwo (int param) {
    return restrict(param) * anotherVariable;
}
//...

And when you just do restrict(param) it will do that check for you and will return desired number.

M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
0

Instead of writing multiple methods, just write one method that takes a parameter to decide which functionality it needs to call and put your condition at the top of that method:

Int MethodOne (int func, int param) {
    If (param > 5) {
        Return 0;
    }

    switch(func)
    {
        case 1: Return param; break;
        case 2: Return param * anotherVariable; break;
        ...
    }
}
dotNET
  • 33,414
  • 24
  • 162
  • 251
0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication8
{
    class Program
    {
        delegate int restriction(int x, string methodName);

        static void Main(string[] args)
        {
            int x = MethodOne(6);
            Console.WriteLine(x);

            int x1 = MethodOne(4);
            Console.WriteLine(x1);

            int x2 = Methodtwo(6);
            Console.WriteLine(x2);

            int x3 = Methodtwo(4);
            Console.WriteLine(x3);



            Console.ReadLine();
        }

        public static int restrictionMethod(int param, string methodName)
        {
            if (param > 5)
            {
                param = 0;              

            }
            if (methodName == "MethodOne")
            {
                return param;
            }
            else if (methodName == "Methodtwo")
            {
                return param * 4;
            }

            return -1;
        }

        public static int MethodOne(int param) 
       {
           restriction rx = restrictionMethod;
           int returnValue = rx(param, "MethodOne");
           return returnValue;
       }

        public static int Methodtwo(int param)
       {
           restriction rx = restrictionMethod;
           int returnValue = rx(param, "Methodtwo");

           return returnValue;
       }


    }
}
jamir
  • 62
  • 1
  • 8