3

I'm curious as to what is considered better practice in the programming community.

On submit, a function is called, where some validation is done, and then a post is made through AJAX. I was considering having a function handle them both. I would change all element IDs to be the same, with only the difference of an incrementing number (e.g. txt1, txt2 / txt1Cnt, txt2Cnt / txt1Lbl, txt2Lbl, etc...)

Then I'd add loops and conditional statements to properly handle each form. This sounds like the fun way, which is why I wanted to do it, but now I'm thinking that maybe it would not be considered very efficient, since I wouldn't need to differentiate all the different elements if I just had 2 functions. Especially because the forms are not identical.

So, my question is, is there a rule of thumb when it comes to this sort of thing? Is it better to have less functions, or less complexity?

Thanks for your time.

user2592690
  • 265
  • 2
  • 11
  • There's no rule of thumb, do whatever floats your boat! – adeneo Aug 04 '13 at 17:28
  • Does either approach actually reduce complexity instead of moving it from one place to another? If so, go with that one. – Joni Aug 04 '13 at 17:30

5 Answers5

3

There are several things you need to consider in these cases.

  1. Code reusage - Breaking code into functions which do one or several certain things will let you reuse them later.

  2. Code Readability - Code can be more readable when you divide it into logical functions. Especially in cases when someone else will be dealing with your code

  3. Performance - If this function is called many times, in most cases it is better to have 1 function

Tala
  • 8,888
  • 5
  • 34
  • 38
  • I would place your first case at the bottom since the others are far more important in most general cases. – Bart Aug 04 '13 at 17:35
  • Yes, I agree. I've also flagged question as mainly opinion based – Tala Aug 04 '13 at 17:37
  • Thanks for your reply. After reading your answer I suppose it could go either way. They are administrative forms, so they will only be used to update the site. The code won't be reused. Perhaps the readability point would be the biggest factor in this case. In any case, thanks for your reply, as my curiosity on this subject goes beyond this particular circumstance. – user2592690 Aug 04 '13 at 17:38
  • I'm happy to help. If this answered your question, would you mind accepting the answer? – Tala Aug 04 '13 at 18:25
1

A good rule of thumb is the Single Responsibility Principle, which says that a function should do only one thing.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
  • Thanks for your reply. Both forms need exactly the same validation. it's just a matter of the number of textboxes, the text in the output, and the php file they are submitted to. Great point...good to keep in mind, though. – user2592690 Aug 04 '13 at 17:40
0

More simple functions, less complexity.

Charlie Burns
  • 6,994
  • 20
  • 29
0

My rule of thumb is: if there's a chance that I'll need this logic further on for another purpose, then it's better to separate it to its own method or function.

It forces you to spend some extra time abstracting some conditionals to fit the general case, but it saves you N extra times of repeating the logic in other use case.

ffflabs
  • 17,166
  • 5
  • 51
  • 77
0

Generally I like to make sure a single function or method performs a single significant task. This has two benefits;

  • Improve Code Readability - If you come back to your code in x months time, when you can no longer remember writing the code, it can be less time consuming to follow and adapt a sequence of shorter functions than a small number of large functions that each perform multiple tasks.
  • Increase Code Reusability - This applies more to OOP classes, but is also relevant in procedural. If you write a function that performs a reasonably specific task you can then re-use the function again in future projects, requiring the adaptation of a few parameters or paths rather than extracting the parts of a long, multi-functional function to rebuild the function to the new project's needs.
Chris Brown
  • 4,445
  • 3
  • 28
  • 36