Good code and bad code?

Discussion in 'PHP' started by Alvin, Aug 18, 2006.

  1. #1
    Just wondering whats is the difference between good and bad code?

    I mean if a script is working fine without bugs but code can be still called a bad one?
    why?
     
    Alvin, Aug 18, 2006 IP
  2. Monty

    Monty Peon

    Messages:
    1,363
    Likes Received:
    132
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Because it's cumbersome, inelegant, difficult to maintain...

    I'm sure there is other reasons, but I lack vocabulary :D
     
    Monty, Aug 18, 2006 IP
  3. sketch

    sketch Well-Known Member

    Messages:
    898
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    148
    #3
    I guess it depends on what you mean by "bad" and "good".

    To me, good code is clean, properly written and documented code that runs as efficiently as possible, easy to work with and that'll work on most systems without modification.
     
    sketch, Aug 18, 2006 IP
  4. Alvin

    Alvin Notable Member

    Messages:
    2,076
    Likes Received:
    164
    Best Answers:
    0
    Trophy Points:
    210
    #4
    okay got it... but for php what is considered as good code?

    should HTML be embedded in php script or it should be kept separate?
     
    Alvin, Aug 18, 2006 IP
  5. shamess

    shamess Well-Known Member

    Messages:
    1,127
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    185
    #5
    What I've started doing with PHP with HTML is if it's just a simple thing you need to add that needs php, like the date into your template, just use <?php date(); ?> but if the html is only a small snippet, then just echo the HTML... That doesn't make sense, does it? xP

    Just do what ever you can read. Oh, remember to comment, even if you think something is incredibly obvious, you might not remember what a line does in a few months! So comment almost every line.
     
    shamess, Aug 18, 2006 IP
  6. commandos

    commandos Notable Member

    Messages:
    3,648
    Likes Received:
    329
    Best Answers:
    0
    Trophy Points:
    280
    #6
    it's bad if you read your code the next day and you dont understant it .
     
    commandos, Aug 18, 2006 IP
  7. tandac

    tandac Active Member

    Messages:
    337
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #7
    Seperating out PHP and HTML just make sense for larger sites. Similarly using a system such as PEAR's database abstraction layer also just makes sense.

    Sure you don't have to. Sure you can usually make sense of code that mixes HTML and PHP. Sure you don't need to abstract your database layer. Just don't plan on easily changing databases.

    Good PHP code as already said is:
    - understandable (doesn't do anything too wierd)
    - well indented with apropriate whitespace
    - has comments
    - uses include files to handle repeatable tasks (db connection, authentication etc)
    - is bug free and works
    - does apropriate error handlings Users should not see PHP error messages ever
    - Does the apropriate validation to prevent security problems
    - Avoids using unsafe PHP calls without extra checks
    - If the database supports it: using transactions would be a nice touch

    That's just off the top of my head. Additions and suggestions are welcome.
     
    tandac, Aug 18, 2006 IP
  8. Alvin

    Alvin Notable Member

    Messages:
    2,076
    Likes Received:
    164
    Best Answers:
    0
    Trophy Points:
    210
    #8
    what does that transactions mean?
     
    Alvin, Aug 18, 2006 IP
  9. daboss

    daboss Guest

    Messages:
    2,249
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    0
    #9
    ok... let me have a go at it... :D

    working code can still be bad code because:
    1. the programming flow is all over the place... no one other than the original coder can understand it... in fact, the original coder may have a hard time understanding it after a while himself...

    2. is cumbersome... something that can be accomplished in 1 simple step is done using a few steps - yup, it still works, but slower...
     
    daboss, Aug 18, 2006 IP
  10. tandac

    tandac Active Member

    Messages:
    337
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #10
    Transactions are a database feature. Inserts, updates and deletes are are recorded and only applied to the database when everything is successful. For example:
    begin a transaction
    bill a customer
    debit account
    ** some problem happens **
    roll back the transaction

    In this case the customer does not get billed because of the error. Later on we can retry the billing again. In a poorer quality database without transactions, some tables may end up with in complete information or missing information.
     
    tandac, Aug 18, 2006 IP
    Alvin likes this.
  11. DrMalloc

    DrMalloc Peon

    Messages:
    130
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Good code gets the job done the best with the least overhead or complication, and works in all intended environments out of the box. If you want to see how other people are doing this, the coding standards for adding to the wordpress and drupal projects are available, which give some good tips.

    Here are some small examples in php:

    Overhead: Not using preg functions when php's str functions would do the same job.

    Complication: Designing or implementing a big abstracted class library for a small task, like a templating library when it clearly isn't needed.

    Portability: Making sure that the code works regardless of whether magic_quotes_gpc is turned on or not. Not using short php tags (<? and ?>) is another one.
     
    DrMalloc, Aug 19, 2006 IP
  12. tandac

    tandac Active Member

    Messages:
    337
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #12
    If you're writing anything more than a quick hack I think a template library is essential. The same holds true if you're working with designers. Let the designers design and the coders code. The two task can be easily kept seperate.

    I stand by my above list for any site that is not a quick hack. The poor developer who comes along later and has to maintain your code will thank you as well.
     
    tandac, Aug 19, 2006 IP
  13. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #13
    This is a bit OT, but I used to preach the use of a templating system, such as smarty, but have come to the realization that Php *is* a template language in and of itself. Why introduce another language that needs to be learned and that will slow down the application?

    Which one of the following looks like more code and more overhead?

    Using the Smarty templating engine

    code.php
    
    include 'smarty/Smarty.class.php';
    $msg = 'Hello world!';
    $tpl = new Smarty;
    $tpl->assign ('msg', $msg);
    $tpl->display ('display.tpl');
    PHP:
    display.tpl
    <html>
        <head>
            <title>{$msg}</title>
        </head>
        <body>
            <h1>{$msg}</h1>
        </body>
    </html>
    Code (markup):

    Using Php (A templating language)

    code.php
    <?php
    $msg = 'Hello world!';
    include 'display.php';
    ?>
    PHP:
    display.php
    <html>
        <head>
            <title><?php echo $msg; ?></title>
        </head>
        <body>
            <h1><?php echo $msg; ?></h1>
        </body>
    </html>
    Code (markup):
    This is just a trivial example of printing a variable, but Smarty has most if not all the php constructs (control structures), the only thing is, they're uglier and harder to learn (IMO). If the designer has to learn the Smarty language, they might as well learn a bit of Php.
     
    exam, Aug 19, 2006 IP