What's Your Coding Style?
Re: What's Your Coding Style?
Narf...
"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?
"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?
Every good solution is obvious once you've found it.
Re: What's Your Coding Style?
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.
I actually didn't know the style I used had a name - cool find on the Wikipedia link.
Re: What's Your Coding Style?
You hit the nail on the head, there; the GNU style is, to me, illegible.The others are various degrees of crap, IMNSHO, with the GNU style being the winner in the crap department by a wide margin.
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.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.
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.My suggestion is to use 4 spaces for indent.
You mean theLithorien 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.
Code: Select all
int main(int argc, char** argv)
{
if (argc < 2)
{
printf("Usage: %s [-abcdefg] file\n", argv[0]);
}
}
That's my second favourite after 1TBS/K&R.
Re: What's Your Coding Style?
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.Synon wrote:You mean theLithorien 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.style?Code: Select all
int main(int argc, char** argv) { if (argc < 2) { printf("Usage: %s [-abcdefg] file\n", argv[0]); } }
That's my second favourite after 1TBS/K&R.
Re: What's Your Coding Style?
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.
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.
Re: What's Your Coding Style?
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.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.
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.
Re: What's Your Coding Style?
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 tabsSolar wrote: I agree, if you use them consistently, tabs are just as good as an indent. But consistency is easier to achieve with spaces.
- Owen
- Member
- Posts: 1700
- Joined: Fri Jun 13, 2008 3:21 pm
- Location: Cambridge, United Kingdom
- Contact:
Re: What's Your Coding Style?
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.earlz wrote: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 tabsSolar wrote: I agree, if you use them consistently, tabs are just as good as an indent. But consistency is easier to achieve with spaces.
Re: What's Your Coding Style?
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.
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.
Re: What's Your Coding Style?
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:
Pascal (2 spaces on the left are forum artifacts):
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;
}
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.
//############################################################################//
Re: What's Your Coding Style?
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.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?
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;)
{
...
}
Every good solution is obvious once you've found it.
Re: What's Your Coding Style?
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 `,`Solar wrote: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.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?
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
}
- NickJohnson
- Member
- Posts: 1249
- Joined: Tue Mar 24, 2009 8:11 pm
- Location: Sunnyvale, California
Re: What's Your Coding Style?
Idk, that seems pretty extreme to me.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.
This:
Code: Select all
for ( i = 0; i < 10; i++ ) {
}
Code: Select all
for (i = 0; i < 10; i++) {
}
Code: Select all
for(i=0;i<10;i++){
}
Re: What's Your Coding Style?
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:NickJohnson wrote: looks pretty much just as clean as this:Code: Select all
for (i = 0; i < 10; i++) { }
Code: Select all
if(condition) {
...
}
Code: Select all
if(condition)
{
...
}
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
Re: What's Your Coding Style?
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"Solar wrote:This is several spaces short in my book, but the crucial one is between "--" and ">". Spaces add information.
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)
- 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)