Design question

BeliasBelias Member Posts: 2,998
Hi all,
I have a little design question: here's the scenario.

I have to do 'X' function if a condition is fulfilled, otherwise I have to do 'Y' function. Also, 'Y' function split in other 2 cases.

In these functions I have to modify the same table but in different methods (obviuosly :mrgreen: ).

the question is: is it better to create three functions for the 3 cases (X, Y1, Y2) or 2 functions and an 'if' statement in the 'Y' function?

EDIT: Moreover, I can also Create 'X' and 'Y' and then let 'Y' Call other two functions...

Personally I think it is a designer's choose, but to be sure I ask you too.
-Mirko-
"Never memorize what you can easily find in a book".....Or Mibuso
My Blog

Comments

  • kinekine Member Posts: 12,562
    All depends on possible reuse of each method as separate function and on complexity of the functions. If they are creating one atomic functionality, where parts cannot be used separately, just use one function with case of if statements. If you can use each function separately, create special function for each. 8)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • BeliasBelias Member Posts: 2,998
    it's not an absolute rule i think because, as you say, it depends from the complexity too and sometimes you need to create a function only to make the code clearer..(or more clear? :roll: )

    By the way good rule kine, i'll remember it
    thanks a lot
    :mrgreen:
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • kinekine Member Posts: 12,562
    Yes, it depends. Priorities are these (for me):

    1) If you have complex code, try to divide it into functions
    2) If you have code,which can be reused elsewhere (is "generic" or have specific "inputs" and "outputs" and functionality is not depending on some global variables etc. (outer state of the system)) just keep it as separate function.

    But the detail level of splitting of code into functions is on the developer. You know, to have 150 functions with 5 lines of code each is too much, have one function with 150 lines of code can be too much too... :-) but it depends from case to case. 8) Just make the code as LEGO pieces...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • DenSterDenSter Member Posts: 8,305
    For me the decision to write a function is mostly whether the code is going to be reusable. It has to be a pretty large chunk of code to make me want to write code bits into a function. To make larger chunks of code more readable putting short comments in there is much more effective to me.

    The reusable part is good practice for every developer, even for small bits of code (like GetItem, GetCustomer, etc.), that just makes your code more robust. You make a change to the function and the whole system behaves the same. The readability part is a matter of personal style in my opinion, you will find many different opinions on that.
  • kinekine Member Posts: 12,562
    DenSter wrote:
    For me the decision to write a function is mostly whether the code is going to be reusable. It has to be a pretty large chunk of code to make me want to write code bits into a function. To make larger chunks of code more readable putting short comments in there is much more effective to me.

    The reusable part is good practice for every developer, even for small bits of code (like GetItem, GetCustomer, etc.), that just makes your code more robust. You make a change to the function and the whole system behaves the same. The readability part is a matter of personal style in my opinion, you will find many different opinions on that.

    Yes, the "complexity" is subjective and it is on you when you tell that the code is "complex" enough to split it into more functions :-)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
Sign In or Register to comment.