Hello! Currently I am modifying a ton of php for someone and I have noticed habbit's that really annoy me. I just want to know what the reason behind these habbits may be.
First, i've seen this alot placing a curly brace a line down. Also, whitespace in between parenthesis?
This is how I might define a method in a class.

public function fetch(){
    //some code
}

Now this is how this person would.

public function fetch( )
{
    //some code
}

Okay, i've seen the line down for curly brace before i'm kind of used to it but it still REALLY annoys me for some reason, I think it's a waste of space. Another thing this individual does is put whitespace between all parentheseis. So here are some examples of this:

require_once( "../includes/config.php" );
//and
$zPage->Render( );
//and
$zPage = new ZPage( "account" );

Why?? That adds up to, if you have a large project that could even create a noticeable space difference. There is one more thing that I noticed that I have never seen before and have absolutely no idea what the purpose of this is. The first line after the opening php tag, has no code written in it, just around 45
tabs over, that's right empty line, no code but 45 presses of the tab key. I wouldn't have noticed it but with Notepad++ it makes a dotted line where tabs are so I noticed it. But really? what could possibly be the purpose of this? Just 45 tabs at the top of every document...?
What do you think of these habbits? Any of you do this stuff, or know why people do?
Interested to hear.

Recommended Answers

All 40 Replies

There's a saying here in Brazil that could be translated to something like "Each crazy person has it's own habbits" (Cada louco com sua mania).

Let me show you some examples:

I don't break line to {, but I add space in ( something ), and I don't it it's ().

Some people don't use space between parameters, I do, like ( param1, param2 )

But a co-worker, adds line break to { and don't add space in (param)

Another co-worker add a lot of blank lines, just like this:

public function add() {

    // Code only here

}
// First Line break
// Seconds Line break
public function close() {

    // Code only here

}

About the php tag, I use like this:

<?php echo 'something'; ?>

OR

<?php
    $myVar = 'something';
    //something
    echo $myVar;
?>

So... who knows... every person grow up learning and experiencing something different. No one has the same standarts of another, in anything.

Yes, there are lot's of published coding standarts, but how far should we take them?

For me, if the code is readable, I'm fine with it.

I think is much better to have the thinking aligned about structure, performance and naming standarts over writing with the same syntax.

Sorry about the bad english, I'm not felling so good today. But I like the topic, that's why I wrote such a mess.

Seeya.

Lol. I thought it was a cuban only proverb "Cada loco con su tema"

I always put opening curly braces on their own line. Otherwise, I just have a really hard time wrapping my mind around where code blocks start and end. It's easy to match them up that way. All the tab stops at the top might be used to help align intentations through the page ... again, to help with nested code blocks. They're visual aids.

commented: I prefer that style too. +0

Oh I see, the tabs make sense now kind of. Also another syntax thing that bugs me is in CSS stylesheets, spaces after colons. That's not such an odd thing but it bugs me a lot for some reason, for example I might write

background:#181818;

Someone else would write

background: #181818;

How about you guys? The whitespace just bugs me so if I test out a code snippet I always have to go through and remove the whitespace!
Also for one line styling I HAVE to add whitespace or I go mad, which is kind of hypocritical with the parenthesis I mentioned in the OP but I have to go like this

body { background:#cccccc;font-family:arial;font-size:14px; }

Can't stand this

body {background:#cccccc;font-family:arial;font-size:14px;}

Don't know what it is I'm just extrememly picky, you guys?

I try to strike a balance in my use of whitespace.

Ifindthattoolittlemakesthecodehardtoread and                        too                   much





can


also                   be                      a   

                        pain.

I add white-space and spaces for my CSS. If there is more than one style to be applied to an element, I indent it like I indent my PHP code. Otherwise, I put it on one line. It looks like this:

body
{
    background: #CCCCCC;
    font-family: arial;
    font-size: 14px;
}

div#foo { color: green; }
div#bar { color: blue; }

@Reverend Jim haha, very true and good example :D
@Dani oic, you still break down for the curly brace, and you add whitespace after the colon, but you add whitespace on single line like me :D
With HTML there can't be to much varying syntax? Cause how strict it is if something veries a lot it's incorrect. Any ideas?

Each and every one of us has his/hers way of doing things and for various reasons. Others be because that feels right, others because they carry habits from one language to another and others because it helps us read our code now or it will help us understand our code when we revisit it a few months/years in the future.

I've met this guy that writes SQL pretty much identical with C++. He can't help it but press tab where it feels necessary. To me it's both useless effort and brilliant code to read.
Then there was this other guy that would simply press enter to word wrap (in SQL), no matter how complex his code got. It was next to impossible to read, sometimes even for him.

I agree with AleMonteiro if the code is readable its ok, but sometimes that extra few characters can make a lot of difference.

I'd like to think that the more experience one gains with reading and writing code, the less these little details matter. A modicum of common sense in the coding style and overall consistency is what matters.

This attitude is especially important if you plan to step into the professional world, because you'll be asked to compromise when designing style guidelines for a team or even throw out your personal style entirely to conform to an existing style guideline.

Does anyone have an idea what the worst habbit they have seen is? Or the most inconsistent and just horrible code snippet is?

Does anyone have an idea what the worst habbit they have seen is?

Zero indentation, no contest.

I have written some pretty horrific LINQ one liners in my time when I've been rushing.

Generally I newline curled bracers, nest everything and if a line gets too long, drop each argument to its own line, indented as appropriate to the current nested scope.

See some interesting code here.

When experts in a language try to write impenetrable code. ;) IOCCC entries are actually beautiful in a perverse way because it takes a great deal of skill and effort to obfuscate what are normally straightforward programs into something that requires expert-level abilities to figure out.

Compare this with plain bad code, which is ugly because the lack of skill and effort is apparent at a glance.

Deceptikon no indentation, I agree with an exception of html i think. Flat html isn't to bad in my opinion.

How do you keep track of open/closed tags, NardCake, if you don't indent your HTML??

I think some of the nastier space saving habits come from the days code had to be to printed out on paper. For me proper indentation is a must.

I run my C code through the Code::Blocks IDE source code formatter ...

// create a delay(millisec) function

#include <stdio.h>
#include <time.h>

void delay(long msec);

int main()
{
    int k;

    for (k = 0; k < 10; k++)
    {
        printf("k = %d\n",k);
        delay(1000);          // milli seconds
    }
    getchar();  // wait
    return 0;
}

// use clock() to generate a delay
void delay(long msec)
{
    long start = clock();   // needs time.h

    while (clock() <= (start + msec))
        ;
}

Flat html isn't to bad in my opinion.

Obviously I don't know anything about your experience, but this statement suggests you've only worked with what I'd consider to be "small" web pages. Any small program isn't too bad, but it gets out of hand almost immediately.

I always indent my html, just saying where i've seen flat html it's not as annoying as some oddities in non-markup.

I use CodeMaid to organise my code and StyleCop to add the necessary formatting like blank lines, comments and the rest.

Edit: I code in C# atm.

Im my miniscule experience, I've found that when editing someone elses code it's not how they've laid it out that makes it easy/hard to read/follow but how well it's commented, if it's even commented at all.

commented, if it's even commented at all.

Newbies oftenly forget to comment and document their work...

Code should speak for itself, only rarely code is not clear enough to be read without comment.

"Code never lies, comments sometimes do." - I forget who.

Code should speak for itself, only rarely code is not clear enough to be read without comment.

I agree. I usually only comment to remind myself why I did something a particular way, but I really should be commenting at least a little more than I do.

Commenting the obvious is rather distracting in code.

I have worked with many horrible codes with almost no comments.All Javascipt code in a single line.Many ope source projects are based in similar fashion.Some of them with on which i have worked on are tinyMCE editor,CKEditor,....

Easy way to use them is to use any IDE and format them.I use Eclipse IDE to format code in proper manner.

As for comment section is concerned,i like making it so clean that anyone else can easily know at first glance what this method is intended to do

oddly enough... sometimes you want your code to be confusing...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.