SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard bronze trophy cydewaze's Avatar
    Join Date
    Jan 2006
    Location
    Merry Land, USA
    Posts
    1,096
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Coldfusion best coding practices

    I have been wanting to start this thread for ages, but have avoided doing so because it's going to showcase what a complete newb I am, and make it obvious that I'm mostly self-taught when it comes to this stuff. But I really want to learn, so maybe it's worth the ridicule. Who knows! There's a 1% chance that I've been doing it right by accident all along. Anyway, here goes.

    For the past several months I've been slowly building a CF app that my office has been using (and is so far working quite well) that I have gotten lots of help with from the great members here at Sitepoint. As time goes on, I keep adding more and more functionality to the app, and with that functionality has come complexity. The bigger the app gets, the more knotted up the code gets. Everything in the app works fine, but I'm wondering if there's a better way to do it, or if everyone else is doing it the same way I am.

    I basically have a CF page that has a list of our office staff. On the same page is a list of projects. The app's database is used to populate several web pages, including our staff directory and list off projects (which lists which staff members are on each project). The app is what we use to keep all these lists up-to-date, and it keeps all the staff info in one spot.

    When a user logs in to the app, they arrive at the aforementioned page. There's a link to add staff members, and they can also click on a staff member's name to edit his/her info, or delete them. Likewise with the projects.

    In order to do this, I have been using the following convoluted method:

    When you click on the "add staff" link, or click on a person's name, you're taken to a form, and an argument is passed in the URL string that tells the form whether you're adding or editing someone. In the form, there's a hidden input that gets a value depending on what you're doing.

    When you submit the form, you're taken back to the previous page. At the top of this page there's code that looks at what the form passed, and there's a series of <cfif> statements. It goes something like this:

    (I've used plain English for simplicity sake)

    < if the form was adding someone>
    query here

    < elseif the form was editing someone >
    query here

    < elseif the delete user box was checked >
    query here

    </ if>

    And since I used the same method for the projects, we have even more of these...

    < if the form was adding a project >
    query here.


    And so on.

    So, as you can see, I have a mass of conditional statements, and it got to be a bit of a chore to sort through these when making changes or adding features. I've always hated this, so recently I moved all the actual code above to cfc files, so while I still have the mass of IF statements, it looks like this instead:

    < if the form was adding someone>
    <cfinvoke component="users" method="add">

    So it's less messy, but still the same idea.

    Is this the accepted way of constructing an app like this, or is there something completely different of which I'm unaware? If there's anything that's neater, or considered better practice, I'd love to learn more about it.

    Also, I want other coders to be able to make sense of what I've done in the event I get hit by a train tomorrow or something, so it helps to do things in a manner that's considered standard.

  2. #2
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awesome article.
    Even my site is coded on clod fusion.
    I hope it will really help me..

  3. #3
    SitePoint Wizard bronze trophy cydewaze's Avatar
    Join Date
    Jan 2006
    Location
    Merry Land, USA
    Posts
    1,096
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Errrr...

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,276
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    "clod fusion" LOL

    cydewaze (do you have a first name i could use instead?) i think your issue is perhaps as much to do with form design as with coding practices

    think about it like this -- if every action had its own separate form, so that the form was perfectly tailored to the action, then the form processing templates would be simple indeed, and there could be one form processing template for each form, and no coding complexity

    i think the complexity arose when you designed one form to handle all actions and one template to handle the single form, which has to unravel all the actions...

    what would it take to have one template per action? just a question to get you thinking...
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Wizard bronze trophy cydewaze's Avatar
    Join Date
    Jan 2006
    Location
    Merry Land, USA
    Posts
    1,096
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    cydewaze (do you have a first name i could use instead?) i think your issue is perhaps as much to do with form design as with coding practices
    My name's Paul!


    Quote Originally Posted by r937 View Post
    think about it like this -- if every action had its own separate form, so that the form was perfectly tailored to the action, then the form processing templates would be simple indeed, and there could be one form processing template for each form, and no coding complexity

    i think the complexity arose when you designed one form to handle all actions and one template to handle the single form, which has to unravel all the actions...

    what would it take to have one template per action? just a question to get you thinking...
    That's interesting. And here I was thinking that making the same form do everything would be simpler.

    Right now the user form handles all the adding, editing, and deleting function, and it's called user.cfm. I could just as easily have adduser.cfm, edituser.cfm, and deluser.cfm.

    So rather than having to pass a hidden form value, depending on what the form is supposed to do, it would just pass the required info. But then on the page that executes, I'd still have the mess.

    Right now it's like this.

    Step 1
    [main page] -> user clicks "edit" and is taken to user.cfm

    Step 2
    [user.cfm] -> user makes changes, and clicks "submit". This takes them back to the main page.

    Step 3
    [main page] executes the query (or more recently, the component) and displays the new user list.


    I think the Step 3 is the complicated part. Maybe I should make each form point to its own processing page, and then have that page forward back to the main page, but that seems rather clunky too!

    Thanks for the info Rudy. I'm just wondering how experienced coders handle stuff like this.

  6. #6
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am a noob at coldfusion myself, but i do have a few things to say.

    (I've used plain English for simplicity sake)
    That's called Pseudocode, and is good to use in this instance.

    Also, I want other coders to be able to make sense of what I've done in the event I get hit by a train tomorrow or something, so it helps to do things in a manner that's considered standard.
    If you put a comment before each chunk of code explaining what it is doing, then programmers who look at it will get it right away. Usually a comment specifies preconditions and postconditions (if there is any), and what exactly the code is doing. Doing this will greatly help programmers who come after you. Believe me. It would have saved me months (yes literally) if the code i have to look through had comments explaining everything.

    In coldfusion, comments are like

    <!--- Comments here --->

    (Yes you need a space after the "<!---" and before the "--->" i think)

    Hope that helps some.

  7. #7
    SitePoint Wizard bronze trophy cydewaze's Avatar
    Join Date
    Jan 2006
    Location
    Merry Land, USA
    Posts
    1,096
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Wej View Post
    If you put a comment before each chunk of code explaining what it is doing, then programmers who look at it will get it right away.
    Yep I've already done that. Still, thanks.

  8. #8
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just FYI Wej...

    You don't need a space before your opening and closing comments. This:
    <!--- my comment --->

    is just as valid as this:
    <!---my comment--->
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  9. #9
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by creole View Post
    Just FYI Wej...

    You don't need a space before your opening and closing comments. This:
    <!--- my comment --->

    is just as valid as this:
    <!---my comment--->
    The book lied to me!!!! O well....i will still do it that way because it looks nicer...

  10. #10
    SitePoint Wizard bronze trophy cydewaze's Avatar
    Join Date
    Jan 2006
    Location
    Merry Land, USA
    Posts
    1,096
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Don't feel bad Wej... I do it too.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •