Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Cleaning up my code.. a little advice?

  1. #1
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default Cleaning up my code.. a little advice?

    Okay, so it is no secret that me and php don't get along. I wrote a few things and it works. It works well.

    But looking at it I think I have more in there than I need. So I decided to clean it up. Stream line it if you will.

    The problem is that I don't know enough about php to do it without breaking the code.

    So I ask.. what is the best method of doing this? Is there something I can read about that will help me prune my coding for this script?

    I just don't know what I am doing, but I hate how long it is, and how much is there that I don't actually need.
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Hard to say.

    First, use proper tabs and line breaks, if you haven't already.

    Second, see if you can simplify any operations with simpler/faster functions.

    Commenting your code can also be a help (though you could strip comments for the final script to maximize efficiency), as you can see quickly what stuff does. Commenting might be annoying, but it's very helpful.

    You can test how long a repeated series of actions takes by looping through it 100+ times then comparing it, and you can see which takes less time from start of execution to the end.

    Regex is certainly something to look into, if you have the time to learn it. I haven't yet, and I wish I knew it, but actually starting on it is daunting-- seems I always have something else to do first.

    Quote styles are important too-- double quotes allow parsing of the string inside, so they are slower. Use single quotes unless you need that parsing.

    Nothing else I can really say without seeing the code itself.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    Well... because of the nature of the code I can't post it here (google indexing and all) but if you want to see it I can email or PM it to you.

    Cause, what you just said.. umm.. yeah.
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Post chunks if you want, or you could send it to me if you need some insight. I don't personally "want" to see it; it's fine if you don't send it; but if you do need some help, I could take a look.
    I can't claim I'm the best at efficiency, though. I'm good at using what I know (which is a fairly limited amount in some sense) well, to do a lot, but that doesn't mean it's always the easiest/best way.

    Any particular questions? Type of code? (Databases? Math? etc.)
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    No, I will send it, it's not a "private" code so to speak. It is a type of password thing I have been working on for my game site, and due to google indexing I don't really want my players seeing my password code

    I have commented it with where things go, what changes I need to make as I put it on line and I also commented my questions below each section. It works, and it works pretty good. I just think I have redundant things and unnecessary things in it.

    I will email the file, it's a single page PHP file.

    Thanks for looking!
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  6. #6
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Replace the password with random words in it.
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Attach it in a ZIP file. Google only caches files it reads, as far as I'm aware. We can remove it later. You can also change the actual password.

    For clean code:
    • avoid repetition: if you find yourself with a lot of lines of code that look the same, chances are you should've used a loop.
    • Use object orientation where appropriate: PHP's OO support is a bit pants, but still better than nothing. If you're working with databases, see Propel.
    • Separate your code from your HTML. See Smarty.
    • Won't make your code cleaner in itself, but it's a good idea to have one entrance page include all the others (if done properly). This enables you to have clean URLs based on $_SERVER['PATH_INFO'], especially if you combine it with mod_rewrite. It also allows you to include what you need without doing it explicitly in each page.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    I suppose it is better to see it, I do have repetitive lines, but they are in different files. (read: go on different pages) It is pretty clean I just think there is extra crap in there that I don't really need.

    I have zipped up the file I sent to Daniel, note it is not a log in script, so the entrance page include is kind of out. I do have a piece in there that gets placed on other pages (checks sessions) and it is total PHP, my host says they don't support SQL_Auth, and I don't feel like testing them on that one (although I proved them wrong with PHP_Auth, LOL)

    I don't mind using what I have, I just think it is good practice to use clean, small, efficient code, and I have never dealt with this much php to know how to clean it up properly.

    notes included.
    Last edited by BLiZZaRD; 11-21-2007 at 06:30 AM.
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Placing the same code on different pages is a very bad idea.

    It's much better to write functions and base it on that.

    include('main.php');

    Then you can just have all of that processing scripted in one page, and then you can just call each chunk as a function, like "processtop()", etc.

    That will allow you to not worry if it's perfect, either, because you can update the main page any time you want.

    Your script didn't seem to really have any specific problems.

    It was mainly if and else statements. Might be ways to get it simpler, using functions.

    Switch statements can compress it further, too, at times.

    if and else aren't very complex for the processor, either, though it seems more advanced programmers have less long lists of if/else, so there are some methods to look into.

    A couple things could use regex, and there may be some more efficient string functions if not. strchr(), which I recently discovered (thanks to tech_support), seems much better than the constant use of substr(...,strpos(...)) that always shows up in string parsing.
    If that doesn't work perfectly, I would suggest writing your own better substring function for simpler code, and less repetition.

    Look through your code; if you write anything 3 or more times, make it a function; if you write it twice, you can, though that doesn't help so much (by the time you write the function and call it twice, you haven't saved much work).
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #10
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    Okay, well the part that is going on all the pages is going to be an include, LOL. SO that isn't a problem.

    Like I said the code works well, I was just assuming there was something I didn't need in there, little extra crap, you know?

    Thanks for looking! I appreciate it!
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

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
  •