Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Code better?

  1. #1
    New Coder
    Join Date
    Feb 2009
    Posts
    96
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Code better?

    Hey Guys.

    I seriously don't know how to code "really" good.
    I don't really know where to indent and not and stuff.

    For example, for a table in HTML, look how I do it:

    Code:
    <table width="100%" height="50" cellpadding="6"
    cellspacing="0" class="backtable">
    <tr>
    <td valign="top">Testing</td>
    </tr>
    </table>
    For an echo and an if in php, I do this:

    PHP Code:
    <?php
    if($blah ==1)
    {
        echo 
    "Yes";
    }
    I mean, am I doing this right? and is there a site that will show how to code better or something?
    Another thing is, when the line is getting full (I know it shouldn't get too long) where do I break?

    Thanks IA.

  • #2
    Regular Coder
    Join Date
    Nov 2006
    Posts
    246
    Thanks
    13
    Thanked 26 Times in 24 Posts
    There's no real right or wrong way. Like with PHP function hierarchy, most people do it by tag hierarchy, simply so the lower the risk of missing a closing tag. For the most part, you can do whatever is easiest for you to understand, because as far as the browser is concerned, it could all be crammed on one line. I do it like this:

    Code:
    <table width="100%" height="50" cellpadding="6" cellspacing="0" class="backtable">
    <tr>
        <td valign="top">Testing</td>
    </tr>
    <tr>
        <td valign="top">Testing</td>
    </tr>
    </table>
    
    <div id="id1">
        <div id="id1-1">Testing</div>
        <div id="id1-2">
            <div id="id1-2-1">Testing</div>
        </div>
    </div>
    
    <ul>
        <li>List Item 1</li>
        <li>List Item 2</li>
        <li>List Item 3</li>
    </ul>

  • #3
    New Coder
    Join Date
    Feb 2009
    Posts
    96
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Hmmm that looks really good.
    Thanks a lot man. I'll do just like that (If you don't mind xD)

    Thanks.

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    you indent your code to make it easy to read and understand when you come back over it, That means anything is "right" when you feel that.
    some people use to indend code like this:

    PHP Code:
    if ( myfunc$par1$par2 ) == )
    {
        
    callthisfunction$otherpar );

    I hate this,
    I prefere this:
    PHP Code:
    if(myfunc($par1$par2) == 1){
        
    callthisfunction($otherpar);

    but that don't mean that the way I do it is "right" for somebody else,
    In my opinion, feel free to choice your own style and that will be "right",

    Edit: that don't apply when you work in a team, is obvious that you must adopt the team style,

    best regards


  •  

    Posting Permissions

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