0

It might be a really stupid question but;

If I am creating a data-set let say,

    [WebMethod(Description = "Returns a Fruit XML")]
    public XmlElement GetALLFruits()
    {
         Fruit fruit = new Fruit();
         fruit.Name = "Mango La Ka Buma";
         fruit.expiryDate = "11/11/1911";
         getFruitCrateDetails(fruit);

         return fruit.xml; //just giving example syntax might be wrong
    }

    public void getFruitCrateDetails(Fruit fruit)
    {
         FruitCrate crate = new FruitCrate();
         crate.id = 999;

         Fruit.Crate.add(crate);

         // Now here do I need to return "Fruit" object or not ?
    }

And I have 10 or 20 method, should i keep them in methods or combine in 1 big method.

Mathematics
  • 7,314
  • 25
  • 77
  • 152
  • Do you mean that the fruit object that is set in GetAllFruits will add Id of 999 once the function is called? – Nisarg Shah Aug 07 '13 at 09:10
  • yes, as I am contructing a big XML file – Mathematics Aug 07 '13 at 09:10
  • Well in that case you will need to set the return type to Fruit. instead of void. – Nisarg Shah Aug 07 '13 at 09:12
  • And also when you call the method getFruiteCrateDetails you need to set fruit = getFruiteCrateDetails – Nisarg Shah Aug 07 '13 at 09:13
  • yes, but i got 10,20 methods like that, if i will keep doing fruit = method 1, fruit = method 2, it might loose method 1 data and reassign method 2 data to fruit? – Mathematics Aug 07 '13 at 09:14
  • Well Whenever you will call method 1 pass fruit as argument. And Get the value of fruit in the same object named fruit. – Nisarg Shah Aug 07 '13 at 09:15
  • fruit = method1(fruit);//method1 will set some properties of fruit. fruit = method2(fruit);//method2 will set some properties of fruit. But make sure not even a single method overwrites the property values. – Nisarg Shah Aug 07 '13 at 09:17

2 Answers2

1
[WebMethod(Description = "Returns a Fruit XML")]
public XmlElement GetALLFruits()
{
     Fruit fruit = new Fruit();
     fruit.Name = "Mango La Ka Buma";
     fruit.expiryDate = "11/11/1911";
     fruit = getFruitCrateDetails(fruit);//This is call to method 1. Don't worry your values will not be lost fruit.Name will remain as it is.


     return fruit.xml; //just giving example syntax might be wrong
}

public Fruit getFruitCrateDetails(Fruit fruit)
{
     FruitCrate crate = new FruitCrate();
     crate.id = 999;
 fruit.crate.Add(crate);//Now no other method should set crateValues
 return fruit;
}

public Fruit getFruitCrateDetails1(Fruit fruit)
{
 SomeNewProperty = "test";
 fruit.PropertyName = SomeNewProperty;//Now no other method should set value for SomeNewProperty
 return fruit;
}

Please read the comments. And i have not tested the code. So there are possibilities that you may not get the desired output. I have tried my best to explain you.

Nisarg Shah
  • 354
  • 1
  • 14
0

This is generally a stylistic choice but most developers frown on large methods.

By dividing a method up, you get these things for free;

  • Reuse of code - you can call that method twice without duplicating code.
  • Separation of concerns - a golden rule i use is that each method should do was it say it does in its name. Complex methods will call other methods in turn.

What you end up with is a clean, modular system with bite size pieces of logic to read and understand without a wall of text cluttering your mind.

In addition, methods prefixed with 'get' should return a value. If not, consider naming it 'add'. This is another stylistic thing but if you work in a team, it helps to match your method names to the method signature.

Gusdor
  • 14,001
  • 2
  • 52
  • 64