0

I am using code igniter.

I have quite a large list of items being sent through post, some will be set, some wont be.

I have two ways of approaching the same problem.

/*DYNAMIC CODING */

$fields = array(
  'field1', 'field2',
  'field3', 'field4',
  'field5', 'field6',
  'fiel'... ...);

foreach($fields as $f)
  $$f = $this->input->post($f);

/* OR LITERAL CODING? */

  $field1 = $this->input->post('field1');
  $field2 = $this->input->post('field2');
  $field3 = $this->input->post('field3');
  $field4 = $this->input->post('field4');
  $field5 = $this->input->post('field5');
  $field6 = $this->input->post('field6');
  $field7 = $this->...

Both methods will work, my question is, is there any reason to use one over the other?

Hailwood
  • 89,623
  • 107
  • 270
  • 423
  • What I don't understand is what is the point of creating all these variables? – Jani Hartikainen Apr 19 '11 at 00:58
  • @Jani Hartikainen well, they are coming from a post request... I have to assume you don't work directly with the post array do you? ... – Hailwood Apr 19 '11 at 00:59
  • @Jani Hartikainen also take note that the fields arn't actually called fieldx, they could for example be `uid, uname, fname, lname, doby, dobm, dobd, ...` the names are just examples, otherwise I would just use a `for()` loop. – Hailwood Apr 19 '11 at 01:05
  • Ok, thought they were just going to be arbitrarily named fields. Perhaps it makes sense to do this if they are actually named in some sort of sensible way. The example is really too constrained to go about it in any more depth, but that wasn't the question so I'll just leave it at this :) – Jani Hartikainen Apr 19 '11 at 01:09

4 Answers4

4

Use dynamic coding in my experience, because it is my personal preference.

If your preference is either or the other, then use whatever you feel comfortable with, if you are coding with a means of someone else editing the code, whatever you choose make sure you add;

// a comment

to explain your code. Using either will differ in process time by a minimal unnoticeable amount of milliseconds, but understanding your code and commenting it correctly will save you alot more than milliseconds in the long run!

Version1
  • 657
  • 5
  • 13
1

the literal coding might work better when getting variable autocomplete to work in your IDE, if you use one (like eclipse for example)

otherwise, whatever style you prefer

bumperbox
  • 10,166
  • 6
  • 43
  • 66
1

Literal coding may have a small performance benefit (as the dynamic code is first generating an array and then looping over it.)

But to me, the dynamic code is a lot more like how we talk and think. You don't say "Today I went to the grocery store and then I went to the bowling alley and then I went to my friend's house", you say "Today I went to the grocery store, bowling alley and my friend's house." Besides, dynamic code it's a lot less verbose which decreases the silly need to review over lots of lines of code (with the array, you know what's going to be in it once you see a pattern), you can instantly see what's going on, and it just looks better.

Yet it really depends on how you code. Some will completely disagree with what I say, and some will agree. If you can breathe a little easier seeing long repetitive lines, go for it. But whatever you do, don't forget THE most important coding-best practice: Comments.

Zirak
  • 38,920
  • 13
  • 81
  • 92
  • 1
    I disagree with comments being important if the majority of the code is expressive through precise short functions and well named variables. I'd be pretty upset to see the commented code `$fields = array(...); //array of fields to go into post` – Anther Apr 19 '11 at 01:07
  • Although I agree with Anther, I think Ziraks point was a general, if you're going to use dynamic coding (*cough*shortcuts*cough*) make sure it is understandable, and comment when it needs it. – Hailwood Apr 19 '11 at 01:09
  • Yeah, in his completely simplistic example they aren't. But like nearly any other best practice, commenting is more of an art than a science. Choosing how to and when to put your comments is much more important than having comments by itself. – Zirak Apr 19 '11 at 01:10
  • //starts a session `session_start();` //prints 'hello world' `echo 'hello world';` :D – Hailwood Apr 19 '11 at 01:12
  • @Hailwood lol, Those are life changing comments right there. personally, I prefer to throw confusing code into its own function, that way it documents itself in a way that keeps it from ever having comments that aren't up-to-date. Instead of just having `foreach($fields as $f) $$f = $this->input->post($f);` floating around, I'd prefer to just want to call convert_fields_to_post_variables(); but something definitely more carefully named, probably mirroring exactly what the comment would say. – Anther Apr 19 '11 at 04:33
0

You should use the first option, with an array of values and a loop, simply because those variables will probably change sometime in the future and updating an array is a lot simpler than updating multiple lines of code.

I would also guess that you need to reference those field names somewhere else along the way as well.

A.J. Brown
  • 913
  • 7
  • 14