Skip to main content
Wrap long line.
Source Link
Kyralessa
  • 3.7k
  • 2
  • 22
  • 21

I'm writing a method and depending on a config field I need to change where I get my data from. What this results in me having to write code that looks like this:

List<string> result = new List<string>(); if (configField) { result.Add(fieldA); } else { result.Add(...BusinessLogic...); } 

I'll have to write that if statement many many times so of course I want to turn it into a method instead so I can just write result.Add(newMethod(configField, fieldA, dataForBusinessLogicToParse)). This method would be useless to anyone not writing the specific method I'm writing so does it make sense to declare this method as a separate private method or can I just declare it inline as a delegate like this:

Func<Enum, int, int> newMethod =  (configField, fieldA, dataForBusinessLogicToParse) => ...businessLogic... 

I'm worried declaring it inline might make the code more difficult to understand but I think it makes the class cleaner.

I'm writing a method and depending on a config field I need to change where I get my data from. What this results in me having to write code that looks like this:

List<string> result = new List<string>(); if (configField) { result.Add(fieldA); } else { result.Add(...BusinessLogic...); } 

I'll have to write that if statement many many times so of course I want to turn it into a method instead so I can just write result.Add(newMethod(configField, fieldA, dataForBusinessLogicToParse)). This method would be useless to anyone not writing the specific method I'm writing so does it make sense to declare this method as a separate private method or can I just declare it inline as a delegate like this:

Func<Enum, int, int> newMethod = (configField, fieldA, dataForBusinessLogicToParse) => ...businessLogic... 

I'm worried declaring it inline might make the code more difficult to understand but I think it makes the class cleaner.

I'm writing a method and depending on a config field I need to change where I get my data from. What this results in me having to write code that looks like this:

List<string> result = new List<string>(); if (configField) { result.Add(fieldA); } else { result.Add(...BusinessLogic...); } 

I'll have to write that if statement many many times so of course I want to turn it into a method instead so I can just write result.Add(newMethod(configField, fieldA, dataForBusinessLogicToParse)). This method would be useless to anyone not writing the specific method I'm writing so does it make sense to declare this method as a separate private method or can I just declare it inline as a delegate like this:

Func<Enum, int, int> newMethod =  (configField, fieldA, dataForBusinessLogicToParse) => ...businessLogic... 

I'm worried declaring it inline might make the code more difficult to understand but I think it makes the class cleaner.

Tweeted twitter.com/StackSoftEng/status/1029382277959245825
Question Protected by gnat
Source Link
Mykroft
  • 201
  • 1
  • 2
  • 5

Is it good practice declare a function inline?

I'm writing a method and depending on a config field I need to change where I get my data from. What this results in me having to write code that looks like this:

List<string> result = new List<string>(); if (configField) { result.Add(fieldA); } else { result.Add(...BusinessLogic...); } 

I'll have to write that if statement many many times so of course I want to turn it into a method instead so I can just write result.Add(newMethod(configField, fieldA, dataForBusinessLogicToParse)). This method would be useless to anyone not writing the specific method I'm writing so does it make sense to declare this method as a separate private method or can I just declare it inline as a delegate like this:

Func<Enum, int, int> newMethod = (configField, fieldA, dataForBusinessLogicToParse) => ...businessLogic... 

I'm worried declaring it inline might make the code more difficult to understand but I think it makes the class cleaner.