Skip to main content
Inspiring
July 15, 2008
Question

cfc code looking better now?!

  • July 15, 2008
  • 5 replies
  • 431 views
Thanks for everyone´s help I feel a lot more confident now, what do you think of this code now? I understand the concepts more now thanks again to everyone

1.According to Adam if I understand him and encapsultation right I just need to put everything into a session and return the session to the calling page in this specific case which I have done now so I can just cfreturn the errors.
2.If I give each of the cfargument tags a default attribute value it sets a value in the this scope and in the getemp function but not in the emp session which is the same (eg emp.username= arguments.username). So what is the point of setting a default value?

Thank you
This topic has been closed for replies.

5 replies

Inspiring
July 15, 2008
> there is no point in setting a default value for a REQUIRED argument -
> if it is required it must have a value. if no value was passed to this
> function for this argument - it will throw an error because it is
> required; it will NOT use the value you specified as default.

Are you sure about that last statement?

My position is - irrespective of runtime behaviour as per above - that it's
a non sequitur to have an argument that's required that has a default.
Either it's required (ie: the calling code needs to provide it. NEEDS TO),
or it's optional, and possibly has a default.


> i would side with someone else's previous suggestion:
> do not use a CFC to process your form submissions

Agree to here.

> - submit a form to a
> cfm page

And here.


> that will validate the data and then,

Disgaree now. I would pass the validation to CFC methods; so they can be
reused. It's the jonb of the CFM template to receive the HTTP request;
it's not its job to perform business logic like validation (IMO).

I would do things like combining form fields (say a three-part date
drop-down) into a single date argument, filter out unnecessary form fields
(submit button values), and stuff like that; but as far as the validation
of the actual important stuff on the form: I'd farm that off to my CFC.
Quite possibly I'd have a two-step process: one for validation, one for
storage (or whatever happens to the form data after validation).


> sorry, in addition to English not being my native language, i am also
> far from the best teacher in the world (my wife is) and my explanations
> are not always concise or very clear...

Azadi, let me assure you your English is far better than a lot of the
native English speakers around here (probably even "most of ~"). I have
never had even the slightest problem understanding what you have written.
So that's bloody good work, if English ain't your native lingo.

--
Adam
Inspiring
July 15, 2008
Hydrowizard wrote:
> yes sorry azadi a few points there I have to sort out. the var scope I don't get-there is no default value on that ever?
>
> thanks dan great stuff really appreciated


If you want a 'default' value for a var scoped local variable just set
it when you define it.

I.E. <cfset var myLocalVar = "default">

Inspiring
July 15, 2008
yes sorry azadi a few points there I have to sort out. the var scope I don't get-there is no default value on that ever?

thanks dan great stuff really appreciated
Inspiring
July 15, 2008
Right off the bat I notice that you are writing these as web services. If they are invoked as web services, then the invoking code will have to provide a value for every argument, whether it's required or not and whether you gave it a default or not. I learned that the hard way.

For your specific function, having default values for required arguments is redundant.

Next, this,
<cfset emp.username= arguments.username>
<cfset emp.email= arguments.email>
<cfset emp.telephone= arguments.telephone>

is redundant. It will also crash because you didn't create the emp structure first.

Next, this
<cfparam name="errors" default="#structNew()#">
should be this
<cfset var errors = structnew()>

Regarding this:
1.According to Adam if I understand him and encapsultation right I just need to put everything into a session and return the session to the calling page in this specific case which I have done now so I can just cfreturn the errors.

You probably misunderstood him. If you said stucture instead of session it would make sense.
Inspiring
July 15, 2008
there is no point in setting a default value for a REQUIRED argument -
if it is required it must have a value. if no value was passed to this
function for this argument - it will throw an error because it is
required; it will NOT use the value you specified as default.
default value is set only for OPTIONAL arguments (required="no") which
may or may not be passed a value from the function call - if no value
was passed to the argument, it will assume the default value you specify.

you still have a lot of instances of not VAR-scoping your variables.
forget the <cfparam> tags inside functions - you use cfargument tags for
arguments and cfset var xxx = ...> for variables you create inside your
function instead.

check your var names, too: you are declaring (incorrectly with a
cfparam) a structure named getemp, but then you are populating a
structure named emp... which is supposedly should be in a session scope,
but is not in the code you have posted.

i would side with someone else's previous suggestion:
do not use a CFC to process your form submissions - submit a form to a
cfm page that will validate the data and then, when you are certain the
data is valid and there are no errors, call your CFC which inserts the
data into db. you are making your life unnecessarily complicated trying
to do it the way you are doing it now...

sorry, in addition to English not being my native language, i am also
far from the best teacher in the world (my wife is) and my explanations
are not always concise or very clear...

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/