KBD

Keith Devens .com

Friday, July 4, 2008 Flag waving
And certainly both Horses were doing, if not all they could, all they thought they could, which is not quite... – C.S. Lewis (The Horse and His Boy, ch 10)
← Procrastination hack: “(10+2)*5” | 43 FoldersInstapundit.com: Hurricane Katrina prompts growth in gun ownership →

Daily link icon Tuesday, October 18, 2005

Code style

Was just reminded of something (code taken from here (linked earlier)). Compared to the following:

public class Point
{
    private int x, y;
    
    public Point()
    {
        x = 0;
        y = 0;
    }

    public int X
    {
        get
        {
            return x;
        }
        set
        {
            x = value;
        }
    }

    public int Y
    {
        get
        {
            return y;
        }
        set
        {
            y = value;
        }
    }
}

Something like the following:

public class Point{
    private int x, y;
    
    public Point(){
        x = 0;
        y = 0;
    }

    public int X{
        get{
            return x;
        }
        set{
            x = value;
        }
    }
    
    public int Y{
        get{
            return y;
        }
        set{
            y = value;
        }
    }
}

or particularly the following:

public class Point{
    private int x, y;
    
    public Point(){
        x = 0;
        y = 0;
    }

    public int X{
        get{ return x; }
        set{ x = value; }
    }
    
    public int Y{
        get{ return y; }
        set{ y = value; }
    }
}

seems so clearly superior to me that I don't see what there is to debate. It's beyond me how anyone would like to read the first version, which is the exact same code as the final version but with 16 extra lines of nothing to wade through. It's almost twice as many lines as the final version.


While I'm here I might as well harp on tabs vs spaces. I'll never understand why some people prefer to only use spaces instead of tabs. Tab means "one level of indentation". Furthermore, some people like to use two spaces to indent their code. That's perfectly fine, but use tabs so I don't have to care about how you like to view your code.

← Procrastination hack: “(10+2)*5” | 43 FoldersInstapundit.com: Hurricane Katrina prompts growth in gun ownership →

Comments XML gif

David Lindquist wrote:

Jamie Zawinski's classic take on tabs vs. spaces:

http://www.jwz.org/doc/tabs-vs-spaces.html

Boy Keith, tabs-vs-spaces AND brace style all in one post. You are a brave, brave man. Smiley

∴ David Lindquist | 18-Oct-2005 1:09pm est | #8482

Erik wrote:

As sad as it may seem, there are companies that part of their yearly/quarterly/etc review is how many lines of code they produced... which version do you think those people like?

I myself would find another job than work for a place like that, but some people for financial or other reasons don't have that luxury (i.e. a friend of mine).

∴ Erik | 18-Oct-2005 1:16pm est | #8483

Keith (http://keithdevens.com/) wrote:

I was never persuaded by JWZ's take. He basically held the "religious" issue and the "technical" issue up and said "let's throw out the technical issue of how tabs are interpreted and just use spaces, which are unambiguous". This leaves the "religious" issue unresolved and people's preferences still at odds. What I don't understand about the argument is that if you just choose the other way and ignore the technical issue, letting people configure their editors however they want, the "religious" issue goes away altogether.

Keith | 18-Oct-2005 1:27pm est | http://keithdevens.com/ | #8484

David wrote:

I tend to agree.

But the bottom line seems to be that regardless of what method you prefer, if the people you are working with do not use the same method, your editor configuration is moot.

∴ David | 18-Oct-2005 1:37pm est | #8485

Heather wrote:

I find the spacing helpful contextually.

If the logic is mundain (as above), then space is not a commodity. If the logic involved is super complex, and involved, then space helps me discern visually.

So the answer is both, sometimes, maybe, for me.

WHATEVER MAKES MY JOB FASTER, EASIER, AND ACCOMODATING TO MY TEAM, AND THE GOAL/DELIVERABLE.

∴ Heather | 18-Oct-2005 4:08pm est | #8486

Adam V. (http://adamv.com) wrote:

I collapse my trivial property definitions down as in the 3rd example.

I know it was just an example, but that definition of Point really bugs me vs just making the freaking X and Y public.

∴ Adam V. | 18-Oct-2005 5:37pm est | http://adamv.com | #8489

Adam V. (http://adamv.com) wrote:

As a further side track, if I kept the properties in Point I'd attribute them thusly: [DebuggerStepThrough]

∴ Adam V. | 18-Oct-2005 5:40pm est | http://adamv.com | #8490

Keith (http://keithdevens.com/) wrote:

If the logic involved is super complex, and involved, then space helps me discern visually.

Of course. As you can see I used space judiciously above; space must be used sparingly; only when it adds meaning and clarity to the code. Vertical space is precious, as the code one can view on screen at once is closely related to what one can readily keep in mind at once.

Keith | 18-Oct-2005 5:47pm est | http://keithdevens.com/ | #8491

Revence 27 wrote:

You'll never convince people to change, Keith.

I use the space-waster. Because it was in the books I used to learn. And because the first '}' doesn't ever vanish on you.

Have you read '1TBS' from the Jargon file?

This is the stuff of holy wars. And nobody changes in the end. I even realise I still do it in Perl, after reading the perlstyle manpage.

Long live Python?

∴ Revence 27 | 19-Oct-2005 9:13am est | #8494

Keith (http://keithdevens.com/) wrote:

And because the first '}' doesn't ever vanish on you.

I'm glad you brought up that argument because it's often used and it's completely unconvincing to me. The first '{' can't vanish on you because if you leave out the '{' you'll get a compile error pretty fast. You simply can't forget it.

Have you read '1TBS' from the Jargon file?

Yeah, but I can never figure out whether people use "1TBS" to mean K&R's style precisely (which is inconsistent, with the brace on the following line for function definitions) or just in general the style that always puts braces on the same line.

Long live Python?

Heck yeah long live Python!

Keith | 19-Oct-2005 9:58am est | http://keithdevens.com/ | #8495

Adam Vandenberg (http://adamv.com/) wrote:

Use ReSharper when doing C# and you'll know before compiling if there are any syntax (or other catchable data errors) ahead of time; it puts red blips in a right-hand gutter that you can click to.

I can't use Visual Studio without it.

∴ Adam Vandenberg | 19-Oct-2005 11:39am est | http://adamv.com/ | #8497

Patrick Smallwood (http://www.kabal-invasion.com/bugjuice) wrote:

We've covered this issue before (http://keithdevens.com/weblog/archive/2004/Dec/30/curly-braces#comments ), and last time, you even said "I can see how this could be a problem" in response to my exact reason for using spaces instead of tabs.

Memory lapse, or just a need to restate your preference?

There is a rational set of reasons people choose spaces over tabs, and brackets-on-their-own-line. You have different rational reasons that lead you to choose otherwise. If you don't see where there is room to debate, you've chosen to ignore the reasons previously given.

∴ Patrick Smallwood | 19-Oct-2005 5:49pm est | http://www.kabal-invasion.com/bugjuice | #8500

Keith (http://keithdevens.com/) wrote:

Heh, I forgot about the Putty issue. However, you did leave out what I wrote immediately before the portion you quote:

I don't work with Putty enough to let its bugs dictate my coding habits...

Keith | 20-Oct-2005 12:09am est | http://keithdevens.com/ | #8502

Patrick Smallwood (http://www.kabal-invasion.com/bugjuice) wrote:

Sure, but that wasn't critical to answering your claim - that you'll never understand why people choose spaces over tabs.

Your choice not to use putty is just that - your choice. It amuses me the number of people that assume most people use an IDE for programming. I can't stand IDE's - virtually every feature they have gets in the way of me getting my coding done.

As I said last time, your mileage may vary. I'm just trying to make clear why some people are "insane" enough to choose spaces over tabs, and one-line-per-brace for their layout. Different strokes for different folks, and its not just preference - we do actually have reasons for choosing things differently.

Thats the value of diversity - you get exposed to situations and logic that you wouldn't otherwise have yourself.

∴ Patrick Smallwood | 20-Oct-2005 11:22am est | http://www.kabal-invasion.com/bugjuice | #8504

Revence 27 wrote:

The first '{' can't vanish on you because if you leave out the '{' you'll get a compile error pretty fast. You simply can't forget it.

It's not about forgetting. I think it's more about Making wrong code lok wrong.

Like, if you have a problem with a block, you are where it starts, you just look for the next curly on the same column.
Of course, you can look for the next statement on the column, as well.
It's just a perference that is as useful as the space saver (and that's why this rant is going on - both can (and do) work).

Soon, we'll be weailing about leaving a space between the asterisk and the name in pointer notation.

∴ Revence 27 | 23-Oct-2005 5:37am est | #8522

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

July 2008
SunMonTueWedThuFriSat
 12345
6789101112
13141516171819
20212223242526
2728293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 5 posts

Recent comments XML

Girls, please don't get breast implants

> And no, you will not be receiving​a picture.

:-(...

Keith: Jul 2, 6:05am

Javascript clone function

This is a clever way to clone an​object if you are using YAHOO UI.​Same tec...

Antonio: Jul 1, 12:47pm

I hate Norton Antivirus

Oh just one other thing norton is​great at keeping people out of your​compu...

kevin.sands: Jul 1, 12:50am

Terminator 3 was awful

I think the biggest reason why T3​totally blew was because Edward​Furlong g...

76.167.172.64: Jun 29, 3:06am

Generated in about 0.205s.

(Used 8 db queries)

mobile phone