What's Your Coding Style?

Programming, for all ages and all languages.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: What's Your Coding Style?

Post by Solar »

Narf... #-o

"If you can't solve your problem with a one-liner, your line isn't long enough."
(Perl proverb)

But what is this 0x0d thinggummy thou speakest of? :twisted:
Every good solution is obvious once you've found it.
User avatar
Lithorien
Member
Member
Posts: 59
Joined: Tue Oct 27, 2009 1:40 pm
Location: Hanover, PA

Re: What's Your Coding Style?

Post by Lithorien »

ANSI style, using tabs (not spaces). If I work in an environment where spaces are required, I'll let my editor automatically convert tabs to spaces upon check-in and not worry about it, and save myself the hassle of counting keystrokes.

I actually didn't know the style I used had a name - cool find on the Wikipedia link.
Synon
Member
Member
Posts: 169
Joined: Sun Sep 06, 2009 3:54 am
Location: Brighton, United Kingdom

Re: What's Your Coding Style?

Post by Synon »

The others are various degrees of crap, IMNSHO, with the GNU style being the winner in the crap department by a wide margin.
You hit the nail on the head, there; the GNU style is, to me, illegible.
I advise against having tabs (0x09) in your sources, though, except for Makefiles (where you can't go without them). Too often you get inconsistencies introduced by different editing environments.
Again, I agree. I personally use 4-space tabs, but I have gedit replace them with spaces. I tend to follow the Linux kernel coding style (except for the 8-space indents thing) because I find it very readable.
My suggestion is to use 4 spaces for indent.
Python suggests that too. I like the Python notion of encouraging good style. If you use four-space indents in one place, and then use six space indents elsewhere, it will throw an exception. It can be annoying (really, it should just be a warning), but ultimately it makes your code a lot more readable.
Lithorien wrote:ANSI style, using tabs (not spaces). If I work in an environment where spaces are required, I'll let my editor automatically convert tabs to spaces upon check-in and not worry about it, and save myself the hassle of counting keystrokes.

I actually didn't know the style I used had a name - cool find on the Wikipedia link.
You mean the

Code: Select all

int main(int argc, char** argv)
{
    if (argc < 2)
    {
        printf("Usage: %s [-abcdefg] file\n", argv[0]);
    }
}
style?

That's my second favourite after 1TBS/K&R.
User avatar
Lithorien
Member
Member
Posts: 59
Joined: Tue Oct 27, 2009 1:40 pm
Location: Hanover, PA

Re: What's Your Coding Style?

Post by Lithorien »

Synon wrote:
Lithorien wrote:ANSI style, using tabs (not spaces). If I work in an environment where spaces are required, I'll let my editor automatically convert tabs to spaces upon check-in and not worry about it, and save myself the hassle of counting keystrokes.

I actually didn't know the style I used had a name - cool find on the Wikipedia link.
You mean the

Code: Select all

int main(int argc, char** argv)
{
    if (argc < 2)
    {
        printf("Usage: %s [-abcdefg] file\n", argv[0]);
    }
}
style?

That's my second favourite after 1TBS/K&R.
Yeah, that's exactly the style. In my mind, I sacrifice a few lines per file to more-or-less whitespace for something that's much easier to read - at least for me.
User avatar
JackScott
Member
Member
Posts: 1031
Joined: Thu Dec 21, 2006 3:03 am
Location: Hobart, Australia
Contact:

Re: What's Your Coding Style?

Post by JackScott »

I use the ANSI/Allman indenting style, preferably using real tabs. Variable/function/method names use whatever casing the language says I should use (though I used to use CamelCase for every single identifier, back when I was a young lad and didn't know better).

I think the important lesson in the tabs vs. spaces debate is not that one is any better than the other... this doesn't really matter, it all looks the same in the end. The important thing is not to mix them, because one you do, somebody else is going to have to fix it later. If you're working on a source file with tabs, use tabs also. If you're working on a source file with spaces, use spaces also.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: What's Your Coding Style?

Post by Solar »

JackScott wrote:I think the important lesson in the tabs vs. spaces debate is not that one is any better than the other... this doesn't really matter, it all looks the same in the end.
Not really... tabs have the potential to "jump around" (not only in the editor, but e.g. in printout). Spaces don't. But more importantly, it's much easier to check source for being tab-free than being space-indent free.

I agree, if you use them consistently, tabs are just as good as an indent. But consistency is easier to achieve with spaces.
Every good solution is obvious once you've found it.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Re: What's Your Coding Style?

Post by earlz »

Solar wrote: I agree, if you use them consistently, tabs are just as good as an indent. But consistency is easier to achieve with spaces.
I think tabs are better than spaces for indent because now if your coworker wants K&R style 8-space tabs, all they have to do is reconfigure their editor. While you can continue using 4 or 2-space tabs
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: What's Your Coding Style?

Post by Owen »

earlz wrote:
Solar wrote: I agree, if you use them consistently, tabs are just as good as an indent. But consistency is easier to achieve with spaces.
I think tabs are better than spaces for indent because now if your coworker wants K&R style 8-space tabs, all they have to do is reconfigure their editor. While you can continue using 4 or 2-space tabs
And then you use tabs to line up something inline (Say, the equals signs of a series of assignments) and suddenly his are off the right side of the page. And don't say "Don't use tabs for that"; you will at some point, and it will cause headaches.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: What's Your Coding Style?

Post by Solar »

Exactly.

And moreover, once you work on existing code, in a team, all personal preferences go out of the window: You use the style that's already used in the sources, period.

You might suggest a code reformatting, but unless that has been decided upon, the established code style it is, no discussions. Asking for tabs so I can view sources in a different style from my coworker is asking for trouble.
Every good solution is obvious once you've found it.
User avatar
Artlav
Member
Member
Posts: 178
Joined: Fri Aug 21, 2009 5:54 am
Location: Moscow, Russia
Contact:

Re: What's Your Coding Style?

Post by Artlav »

My preferences below.
One thing i find common, yet hard to read, is the operators being idented by spaces, like a == 0.
Why do such a thing? Does it appear more readable to many people that way?

For me, i usually see the code as a picture, in two dimensions. Thus, the similar code is often stacked in one line when it is not needed or out of focus, to emphasize the pieces that are important for the moment. I often move it around, grouping similar thing in big blocks, or unwrapping line by line when it must be carefully looked at. In presentable state, it's often similar to examples below.

C:

Code: Select all

bool cbMessage(void *id,char *str,void *data)
{
 char buf[255],cmd[255],rest[255];
 memset(buf,0,254);
 
 if(mode==4){
  if(str[0]=='/'){
   sscanf(str,"/%s %[^]-]",cmd,rest);
   if(!strncmp(cmd,"me",2)){
    sprintf(buf,"* %s %s\n\0",nick,rest);
    inmfd(buf);
    sprintf(buf,"PRIVMSG %s :\1ACTION %s\1\n\0",channel,rest);
   }else sprintf(buf,"%s\n\0",str+1);
   send(s,buf,strlen(buf),0);
  }else{
   sprintf(buf,"PRIVMSG %s :%s\n\0",channel,str);
   send(s,buf,strlen(buf),0);
   sprintf(buf,"<%s> %s\n\0",nick,str);
   inmfd(buf);
  }
 }
 off=0;

 return true;
}
Pascal (2 spaces on the left are forum artifacts):

Code: Select all

  //############################################################################//
  unit suicide;
  interface      
  uses windows,sysutils;
  //############################################################################//
  procedure dosuicide;
  //############################################################################//
  implementation
  //############################################################################//
  //Deleter code
  const delcodebin:array[0..41]of byte=(
  $58,$58,$83,$C0,$05,$8B,$18,$8D,$43,$0C,$50,$8B,$43,$04,$FF,$D0,
  $83,$F8,$00,$75,$0D,$B8,$E8,$03,$00,$00,$50,$8B,$43,$08,$FF,$D0,
  $EB,$E5,$6A,$00,$8B,$03,$FF,$D0,$EB,$FE);
  //############################################################################//
  //Windows dir
  function getwindir:pchar;
  var pres:pchar;
  begin
   getmem(pres,255);
   result:=pointer(dword(pres)*ord(getwindowsdirectory(pres,255)>0));
  end;
  //############################################################################//
  //Base
  procedure dosuicide;
  label kill;
  var start:tstartupinfo;
  procinf:tprocessinformation; 
  secatt:tsecurityattributes;
  rdir:string;
  prc:thandle;
  th,thi,n,p:dword;
  del,ext,dlf,slp:pointer;
  buf:array[0..1000]of byte;
  f:file;
  
  begin 
   prc:=0;
   //Deletemoe
   rdir:=paramstr(0)+#0;
   //Launch host
   zeromemory(@start,sizeof(start));start.cb:=sizeof(start);  
   zeromemory(@secatt,sizeof(secatt));secatt.nlength:=sizeof(secatt);  
   if createprocessa(pchar(getwindir+'\notepad.exe'),'',nil,@secatt,true,CREATE_SUSPENDED,nil,pchar(getwindir),start,procinf)then begin  
    prc:=openprocess(PROCESS_ALL_ACCESS,true,procinf.dwprocessid);
    del:=virtualallocex(prc,pointer($08048000),$1000,MEM_COMMIT or MEM_RESERVE,PAGE_EXECUTE_READWRITE);
    if(del<>nil)then begin
     //Addreses
     dlf:=getprocaddress(getmodulehandle('Kernel32.dll'),'DeleteFileA');
     slp:=getprocaddress(getmodulehandle('Kernel32.dll'),'Sleep');
     ext:=getprocaddress(getmodulehandle('Kernel32.dll'),'ExitProcess');
  
     //Writing a datablock
     p:=9;                   
     //Base   
     pdword(@buf[5])^:=dword(del)+p;     
     move(ext,buf[p],length(rdir));p:=p+4;
     move(dlf,buf[p],length(rdir));p:=p+4;
     move(slp,buf[p],length(rdir));p:=p+4;
     move(rdir[1],buf[p],length(rdir));p:=p+length(rdir);    
     buf[0]:=$E9;pdword(@buf[1])^:=p-5;   
     //Appending the code
     move(delcodebin[0],buf[p],length(delcodebin));p:=p+length(delcodebin);
   
     //Injecting into host
     if not writeprocessmemory(prc,del,@buf[0],p,n)then goto kill;     
     th:=createremotethread(prc,nil,0,del,del,CREATE_SUSPENDED,thi);
     if th=0 then goto kill;
  
     //Launch
     resumethread(th);
    end else goto kill;
   end;
   //Done
   halt;
   
   //In case of failure - remove evidence.
   kill:
   terminateprocess(prc,0);
  end;
  //############################################################################//
  begin end.  
  //############################################################################//
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: What's Your Coding Style?

Post by Solar »

Artlav wrote:One thing i find common, yet hard to read, is the operators being idented by spaces, like a == 0.
Why do such a thing? Does it appear more readable to many people that way?
Very much so. Add spaces after "(" and "," plus a space in front of " )". It's for the same reason people (should) put only one statement per line: To make it easier to grasp the structure of code in one glance.

A very nice example discussed recently was the "tends to" operator. Deliberately using your space-deprived notation:

Code: Select all

for(int i=10;i-->0;)
{
    ...
}
This is several spaces short in my book, but the crucial one is between "--" and ">". Spaces add information.
Every good solution is obvious once you've found it.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Re: What's Your Coding Style?

Post by earlz »

Solar wrote:
Artlav wrote:One thing i find common, yet hard to read, is the operators being idented by spaces, like a == 0.
Why do such a thing? Does it appear more readable to many people that way?
Very much so. Add spaces after "(" and "," plus a space in front of " )". It's for the same reason people (should) put only one statement per line: To make it easier to grasp the structure of code in one glance.
I would say I'm probably neutral on that whole thing.. I've heard people say that, but it doesn't make the code more or less readable to me, so I don't do it unless the IDE does it for me (read: only at work when using C#) though I do believe in the space after `,`

My more full coding style in C:

Code: Select all

#define MYSTUFF 10
//or for C++
static const int MYOTHERSTUFF=11;
int my_global=0;
//note use of abbreviations and short names
int my_func(int arg){
    int i,j;
    for(i=0;i<10;i++){
        for(j=0;j<10;j++){
            puts("stuff");
        }
    }
    //note I always include the braces
}
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: What's Your Coding Style?

Post by NickJohnson »

Solar wrote:Add spaces after "(" and "," plus a space in front of " )". It's for the same reason people (should) put only one statement per line: To make it easier to grasp the structure of code in one glance.
Idk, that seems pretty extreme to me.

This:

Code: Select all

for ( i = 0; i < 10; i++ ) {
}
looks pretty much just as clean as this:

Code: Select all

for (i = 0; i < 10; i++) {
}
although I agree that this:

Code: Select all

for(i=0;i<10;i++){
}
is way too squished.
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: What's Your Coding Style?

Post by Creature »

NickJohnson wrote: looks pretty much just as clean as this:

Code: Select all

for (i = 0; i < 10; i++) {
}
Agreed. I'm pretty fond of this style myself too. The first one doesn't really bother me but I think the last one is a bit too cramped to read it easily. I also don't know why people like to use Java-style braces:

Code: Select all

if(condition) {
...
}
I've never liked this way of doing things. Especially with the whole tab/space debate (it doesn't matter which one you're using), it is very nice to have a new-line for this kind of statement because this way the braces will be on exactly the same indentation level. This makes it easier to see what ends where.

Code: Select all

if(condition)
{
...
}
is far easier to read IMHO.
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
Selenic
Member
Member
Posts: 123
Joined: Sat Jan 23, 2010 2:56 pm

Re: What's Your Coding Style?

Post by Selenic »

Solar wrote:This is several spaces short in my book, but the crucial one is between "--" and ">". Spaces add information.
There's also the example that I saw somewhere of "a+++++b", which is usually tokenised as "a ++ ++ + b", which is syntactically invalid, rather than the valid (but confusing) "a++ + ++b"

My code style is pretty much:
- ANSI-style bracketing with four spaces per tab
- If a statement spills onto two lines, I try to roughly line up what I'm continuing with where it starts. As a contrived Python example:

Code: Select all

d["test"] = (a, b, c, d, e,
             f, g, h, i, j)
- I keep small one-statement ifs on one line
- Prefer '++a' to 'a++' when either is valid, such as for counter variables (even though I use mainly C, it's because in C++ the latter is potentially very expensive)
Post Reply