Coding standards... and me.

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

astions wrote:@aka, I think your totally right about how tabs have a set width. But isn't that related to document formating? As in for print? I think code is a bit different.
Nope, same for editors as well.

Some editors will allow you to change how wide a tab is on screen but as long as it uses a tab character it will be represented the same way on any editor.

Say you have editor1 that displays all tabs as 3 spaces wide. It doesn't insert 3 spaces but the tab character is shown as 3 spaces wide.

Next you have editor2 that displays all tabs as 4 spaces wide. Same thing, it doesn't insert 4 spaces but the single tab character is shown as 4 spaces wide.

If you cut a block of code from editor1 and insert it into editor2 the tabs from the code created in editor1 will be show as 4 spaces wide in editor2.

You don't have to do anything and when cutting and pasting in the reverse either.

But if you have two editors that use SPACES for tabs and you cut from editor1 with 3 spaces per tab and paste to editor2 with 4 spaces per tab the formatting will be off by 1 space in the code you cut and pasted. You will have to go through and add or remove spaces to get it to line up properly.

If you use REAL tabs you never have to worry about that. Using REAL tabs is by far the better of the two as it is more standard.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I see what your saying. My thinking is that tabs are used to go to a specific indent in a document. In documents spaces wouldn't work because most documents are composed of variable width fonts. So, in a document editor, you wouldn't be able to align text perfectly without tabs because of this variance.

I think for the most part a vast majority of programmers use fixed width fonts. I believe this eliminates the need for tabs as the letter spacing is uniform.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

astions wrote:I think for the most part a vast majority of programmers use fixed width fonts. I believe this eliminates the need for tabs as the letter spacing is uniform.
I guess you didn't understand what I was talking about. :)

The use of tabs over spaces is needed more so with fixed width fonts. Tabs will ALWAYS be set to match the fixed width font size and will always use the same width settings no matter what editor is being used. The same cannot be said of spaces.

Here is an example...

Say you use 4 spaces instead of tabs for your indentation. You load a PHP script written by someone else who uses 3 spaces instead of tabs for their indentation. When you start editing their script by adding new code your 4 space tabs will be off compared to the rest of the program. You will either have to manually edit your indentations to fit, change your tab space number in the editor to match or use a search and replace to change the 3 space to 4 space indentation before you edit the file. A lot of unneeded and useless effort created by using non-standard spaces.

If both you and the other programmer used real tabs instead of spaces for indentation you wouldn't have to do any of the above for the indentations to match. You could edit the code and not worry about indentation spacing.

In otherwords

TABS for indentation = STANDARD

SPACES for indentation = Whatever the programmer preferred
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

It's the people that use 3 spaces as a tab that brought me over to using regular tabs.
User avatar
The Phoenix
Forum Contributor
Posts: 294
Joined: Fri Oct 06, 2006 8:12 pm

Post by The Phoenix »

AKA Panama Jack wrote:Couldn't be more wrong on that because you will find different people claiming coding standards that use spaces instead of tabs have different idea as to what is the proper number of spaces to use in place of tabs.
Thats why the standard spells it out. The PEAR coding standard, the PHP Coding standard for Zend Framework, and others do as well. Thats why its a *standard*, its specified.

AKA Panama Jack wrote:You load a PHP script written by someone else who uses 3 spaces instead of tabs for their indentation. When you start editing their script by adding new code your 4 space tabs will be off compared to the rest of the program. You will either have to manually edit your indentations to fit, change your tab space number in the editor to match or use a search and replace to change the 3 space to 4 space indentation before you edit the file. A lot of unneeded and useless effort created by using non-standard spaces.
Notice, in this example, you aren't following the standard. The script written by someone else is following a standard - 3 spaces. You can either conform to the standard, or move to your own. And yes, breaking that standard causes problems. Thats why its a standard - for consistency.
AKA Panama Jack wrote:If any programmer wants to have display consistency they would never use spaces but stick with tabs as tabs because the display will always be the same no matter what editor they use.
Modern editor (nano), modern display program (websvn), and modern comparison program (unix standard diff) - And yet tabs causes problems, where spaces do not.

The 'any modern program' argument is dead. There's the toolchain, there's the reality. Tabs cause inconsistencies, spaces don't. Anything beyond that is simply a workaround.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Coding standards... and me.

Post by s.dot »

scottayy wrote:I don't want to start the tabs vs spaces debate though.
scottayy wrote:My point of this topic is do you conform to coding standards? If so, which? Was it hard to get into practice of using the standards? Are there any (like me) that you just refuse to use?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Coding standards... and me.

Post by superdezign »

scottayy wrote:My point of this topic is do you conform to coding standards? If so, which? Was it hard to get into practice of using the standards? Are there any (like me) that you just refuse to use?
Plenty.
  • Making the first letter of a function lowercased... I just can't bring myself to do it.
  • Using one underscore to name member variables... In C++, I used to use 'm_' as a prefix, and then I started using '__' (two underscores), and got stuck on it. It stands out more to me.
  • Camel casing on variable... And I don't know why. Most of my variables are only one word, so once I make it two I end up leaving it all lowercase, and don't notice until much later. I think I'll go through and start doing it though.
  • The space before the parentheses on the if and for statements... That bugs me.
  • And I have to match brackets... If I don't, it looks ugly and I end up with a lot of bracket-related errors from not seeing it clearly.
That's all I can think of at the moment... My 'standards' are a mixture of C++, ActionScript, and PHP. I do have set standards, they just aren't the same as everyone else's.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Coding standards... and me.

Post by Christopher »

scottayy wrote:My point of this topic is do you conform to coding standards? If so, which? Was it hard to get into practice of using the standards? Are there any (like me) that you just refuse to use?
The majority of programmers just confirm to standards as they come because they just don't mind as long as most generally follow it. Then there is a vocal minority who discuss picayune differences in formatting as if they were mightily significant and pronounce how impossible it would be not conform to this or that tiny detail because it is an offense to all logic and reason. It has always been this way in programming and probably always be that way.

From your OP :
scottayy wrote:I forget which topic it was in where someone told me that adapting to standards is a sign of a mature programmer.
The reason this is true is because the the real mark of a mature programmer is being open to learning design and best practices. To do that you need to open yourself up to the ideas of the programming community. Once you do that, things like formatting become less significant because they pale in comparison to delving deeper into understanding more important issues.
(#10850)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I find it quite easy to adopt different conventions really. When writing symfony app I use one convention, I (almost) follow Zend conventions for my own code, when working for my employer I use another and when coding in Java I use Java's conventions.

I think once you start typing the first few lines, your fingers just get moving in the right directions :)

I notice far more variation in conventions in PHP than I do in other languages. I guess this is because PHP has quite a diverse community of developers and lets you program in different styles without any real convention easily.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

I notice far more variation in conventions in PHP than I do in other languages. I guess this is because PHP has quite a diverse community of developers and lets you program in different styles without any real convention easily.
Absolutely and it's a shame, I sometimes have to tweak the code people post here so that can actually read it. For instance

Code: Select all

$a = $b/ ( $c ++ *- .5);
would confuse the hell out of me but

Code: Select all

$a = $b / ($c++ * -0.5)
makes perfect sense. I try to tell myself that it doesn't matter if you like "if(" or "if (" but I find myself favouring whichever one I'm most used to at the time and I sometimes judge other's code depending how closely their coding standards meet mine, it's a subconscious thing.

The thing I really like about obeying a standard is you don't have to think as much. I remember making considerations about how I should lay out a piece of code but once you've gotten used to some coding standards you don't have to think any more, you can just type away, makes things faster and more focused, for me. Generally I use Zend's coding standards to the letter, with a few exceptions. I really dislike this:

Code: Select all

$bigArray = array($longVariableNameHere,
                  new Some_Class_For_Stuff($pie),
                  array(PATH_SEPARATOR, DIRECTORY_SEPARATOR
                        PHP_EOL),
                  ANOTHER_PARAMETER_FOR_FUNCTION);
I opt instead for this

Code: Select all

$bigArray = array(
    $longVariableNameHere,
    new Some_Class_For_Stuff($pie),
    array(
        PATH_SEPARATOR, DIRECTORY_SEPARATOR
        PHP_EOL
    ),
    ANOTHER_PARAMETER_FOR_FUNCTION,
);
Also I always end the last element with a comma.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

d11wtq wrote:I think once you start typing the first few lines, your fingers just get moving in the right directions :)
Hehe, I've gotten so used to PHP's dollar sign, that I name all of my variables in JavaScript and such starting with dollar signs. Luckily, it's a valid character. :P
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

I would say that's unlucky. You shouldn't really be doing that.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

ole wrote:I would say that's unlucky. You shouldn't really be doing that.
I wasn't at first, but then in JavaScript I didn't notice because I was going back and forth from PHP to JavaScript. I wrote a framework to work with, so I figured I'd just go with it... Claim it's 'for PHP developers.'

When I program in any language that allows strict data typing (ActionScript, C++), I usually follow their standards very well, but with PHP and JavaScript... I'm not even sure I *know* of any standards for JavaScript. I just kind of treat it like PHP. I'm also looking at this discussion for some things I could change in my code and as I was doing a lot of it, I actually started noticing ways to optimize my programming, separate duties better, and avoid repetition. Of course, it's due to the fact that I was re-reading and re-writing my code, but I can't say I don't like it. :-D

The if statements still bug me though... If I can get my mind to read them, I'll stick with them, but going from years of C++ to changing to PHP after less than a year of PHP is still a bit of a transition for me. And lowercased function names... I'm trying, I really am.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

superdezign wrote:I'm not even sure I *know* of any standards for JavaScript.
There are several, but I'd suggest adopting Mozilla's: http://developer.mozilla.org/en/docs/Ja ... tyle_guide

In terms of indentation and brace positions it's much like Java standards.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Cool. ^_^

I've been looking through PHP standards (Zend, not all of the other 'standards' that are floating around... there are a lot of different ones higher up in search engine rankings than Zend) and most of it seems very nice and all, but one thing I noticed is that a few site said we should not use '//' style comments. Is this true?
Post Reply