I usually always write my code like this
[code]
if (something == somethingelse) {
dosomething(with, "parameters", like, this);
} else {
dosomethingelse();
}
function equalToTest(strInput:String) : Boolean {
if (strInput == "test") return true;
return false;
}
[/code]
[QUOTE=h2ooooooo;20415204]I usually always write my code like this
[code]
if (something == somethingelse) {
dosomething(with, "parameters", like, this);
} else {
dosomethingelse();
}
function equalToTest(strInput:String) : Boolean {
if (strInput == "test") return true;
return false;
}
[/code][/QUOTE]
[lua]
function equalToTest(strInput:String) : Boolean {
return strInput == "test";
}
[/lua]
:<
I have to have 1 line of whitepsace between most things, for example:
I get distracted if it looks like this:
[code]
def Func():
stuff
def Func2():
stuff
[/code]But I'm fine if it's like this:
[code]
def Func():
stuff
def Func2():
stuff
[/code]
Spacing
[code]
a+=4; //NO!
a += 4; //Much better
a = 2+2; //NO!
a = 2 + 2; //Ahh yes.
[/code]
and I put parenthesis everywhere to explicitly define the order of operators even though I know it'll work the way I want without parenthesis.
[code]
a = 4 + 3 * 2; //NO!
a = 4 + (3 * 2); //Much better
[/code]
[QUOTE=Xeon06;20417189]Spacing
[code]
a+=4; //NO!
a += 4; //Much better
a = 2+2; //NO!
a = 2 + 2; //Ahh yes.
[/code]and I put parenthesis everywhere to explicitly define the order of operators even though I know it'll work the way I want without parenthesis.
[code]
a = 4 + 3 * 2; //NO!
a = 4 + (3 * 2); //Much better
[/code][/QUOTE]
This aswell!
I also can't stand people who don't use parenthesis when calling some functions in lua like so:
[lua]print"hello world"[/lua]
I'm looking at you Vicis/Kaze/Dec and Deco!
All my code must look like this:
[code]
void Facepunch(int a, int b){
a+=b;
//Some random function I will never use.
}
[/code]
[QUOTE=Dr Magnusson;20417311]This aswell!
I also can't stand people who don't use parenthesis when calling some functions in lua like so:
[lua]print"hello world"[/lua]
I'm looking at you Vicis/Kaze/Dec and Deco![/QUOTE]
I do that too.
I also do it with tables.
Granted, I only do it at script scope, in functions like require and module, or other "declarative" functions of my own.
[QUOTE=ZeekyHBomb;20412932]What? Isn't the lower one faster?
In the upper loop, it has to create temporary objects and use an addition (binary function), instead of just incrementation (unary function).[/QUOTE]
No, the above is faster, because it doesn't have to check so often whether the for loop is expired or something.
[url]http://en.wikipedia.org/wiki/Loop_unwinding[/url]
I HATE THIS:
[lua]
/*---------------------------------------------------------
Name: Clamp( in, low, high )
Desc: Clamp value between 2 values
----------------------------------------------------------*/
function math.Clamp( _in, low, high )
if (_in < low ) then return low end
if (_in > high ) then return high end
return _in
end
[/lua]
/* comments don't get marked in n++, the extra spaces look like shit and why do we have to put the name of the function in the comment?
better:[lua]
--[[Clamp X between min and max
if x is greater than max return max, else if x is less than min return min, else return x]]
function math.Clamp(x,min,max)
return (x>max)and(max)or((x<min)and(min)or(x))
end
[/lua]
fuck readability, but what's hard to understand here.
also, this is shit.
[lua]
/*---------------------------------------------------------
Name: IntToBin( int )
Desc: Convert an integer number to a binary string
----------------------------------------------------------*/
function math.IntToBin(int)
local str = string.format("%o",int)
local a = {
["0"]="000",["1"]="001", ["2"]="010",["3"]="011",
["4"]="100",["5"]="101", ["6"]="110",["7"]="111"
}
bin = string.gsub( str, "(.)", function ( d ) return a[ d ] end )
return bin
end[/lua]
Why do we have to create table "a" everytime the function is called?
And why are there spaces between some things, and between some things not?
[QUOTE=DrLuke;20417565]No, the above is faster, because it doesn't have to check so often whether the for loop is expired or something.
[url]http://en.wikipedia.org/wiki/Loop_unwinding[/url][/QUOTE]
Firstly, in the case of Lua, it won't be faster because the second loop leaves more of the job to C.
In compiled languages it won't be faster because for-loops can be heavily optimized and in cases where unrolling can help performance, the compiler can unroll a lot better without your meddling.
Lastly, even if you were working with a compiled language with a non-optimizing compiler, you shouldn't sacrifice the portability (in the sense that it will actually be [I]slower[/I] with optimizing compilers) and readability for what may or may not be an extremely tiny performance boost. Manual loop unrolling should only be done after profiling determines that it's absolutely necessary to reduce a bottleneck.
I bet your code is riddled with code that can be optimized simply by following the good programming practices associated with the language you're using.
Don't waste your time unrolling loops. Simple as that.
(edit: And if you factor in the CPU cache, large unwindings can suddenly reduce performance greatly because of the resulting code-bloat.)
[QUOTE=jA_cOp;20415249][lua]
function equalToTest(strInput:String) : Boolean {
return strInput == "test";
}
[/lua]
:<[/QUOTE]
It was just an example, could return String and "a" and "b" for all I care :D
And yes yes, I know
return (strInput == "test" ? "a" : "b");
[QUOTE=h2ooooooo;20418206]It was just an example, could return String and "a" and "b" for all I care :D
And yes yes, I know
return (strInput == "test" ? "a" : "b");[/QUOTE]
Yeah I know it was an example, it was meant as a joke, it's just that it hurts to see code like that </3
Be more considerate in your examples next time, please! :v:
(For the record, if that's an ECMAScript dialect (like ActionScript), that's exactly how I would format my code too :P)
Slight thing, but when people discover CLOS they revert to writing Java instead of canonical Common Lisp. Really, what's the point then?
If you're going to keep on using defmethod everywhere just for type checking you could at least write your own macro that uses "the" - at least it allows you to specify a debugger restart and a custom error message which is why CL's error/condition system exists in the first place! Heck, someone's already probably built one.
I can't stand
[code]
if(!var){
echo "Hello World!";
}
[/code]
I always do
[code]
if(!var){
echo "Hello World!";
}
[/code]
It makes the code longer than necessary, which is especially bad when programming on my CAS in TI-Basic when bored out of my mind.
I code in different ways a lot of times, but I just try to keep my formatting consistent. If it isn't then it bugs me and then I make it all consistent.
My main thing is having "padding" in all my brackets and around operators:
[code]
some_variable = SomeFunction( nother_var + ( 82 / some_array[ i + ( 4 ) ] ));
[/code]
But I pair up like brackets as above at the end.
[QUOTE=TehBigA;20408066][code]
<function / if / loop> { <-----
...
}
[/code][/QUOTE]
I'm quite for this. And additionally, I avoid spaces where I can. I hate
[cpp]bool Stuff(char *arg1 /* Why do people add the * to the variable? It belongs to the type cause it's a pointer of that type! */ , int otherstuff )
{
if( otherstuff == 1 ) {
return true;
}
}[/cpp]
I more like
[cpp]void Stuff(char* arg1,int otherstuff){
if(otherstuff == 1) return true; //I just do these one-liners if possible. Otherwise I use the default {}
}[/cpp]
because you save much space on your screen and you don't have the problem with your code look different when you forget to add spaces on some functions.
[QUOTE=aVoN;20421860]
[cpp]bool Stuff(char *arg1 /* Why do people add the * to the variable? It belongs to the type cause it's a pointer of that type! */ , int otherstuff )[/cpp][/QUOTE]
In C and C++, it's used to emphasize how the language actually interprets it:
[cpp]
char* str, str2;
str = "hello";
str2 = " world!"; /*error! str2 is of type 'char'*/
/*You'd have to do:*/
char *str, *str2;
str = "hello";
str2 = " world!"; /*ok, both are pointers*/
[/cpp]
[QUOTE=jA_cOp;20422448]In C and C++, it's used to emphasize how the language actually interprets it:
[cpp]
char* str, str2;
str = "hello";
str2 = " world!"; /*error! str2 is of type 'char'*/
/*You'd have to do:*/
char *str, *str2;
str = "hello";
str2 = " world!"; /*ok, both are pointers*/
[/cpp][/QUOTE]
I prefer just to never declare more then one variable per line.
I like to write my code in this format:
[cpp]
class MyClass
{
public:
MyClass(const int& internalData); //Prefering const References where neccecary
private:
int m_someInternalData;
};
MyClass::MyClass(const int& internalData) :
m_someInternalData(internalData)
{
if( !isDataValid( m_someInternalData ) )
{
std::cerr << "Error, data: " << m_someInternalData << " is invalid";
}
}
[/cpp]
I used to use the style most of you guys do
[cpp]
void someFunc()
{
...
}
[/cpp]
But I decided to change my style after reading Code Complete 2, it's a bit of a shift but once I got used to it I find it easier to understand the logical structure of my code.
[QUOTE=t0rento;20422878]I prefer just to never declare more then one variable per line.[/QUOTE]
Same. Just putting out the most logical reason for aligning the * to the right, although I don't use it myself. I feel that it's simply worth expanding those few cases I would put several declarations on one line to several lines to be consistent with the left-aligned convention.
However, I do group non-pointer object declarations when there's a logical connection between them, like:
[cpp]
int width, height; //dimensions
int x, y, z; //coordinates
[/cpp]
I hate using tabs, in most languages I keep everything separate with comments.
Sucks, because my job involves a lot of Python.
[QUOTE=ButtsexV2;20423307]in most languages I keep everything separate with comments.[/QUOTE]
What does that mean?
[QUOTE=jA_cOp;20423364][QUOTE=ButtsexV2;20423307]I hate using tabs, in most languages I keep everything separate with comments.
Sucks, because my job involves a lot of Python.[/QUOTE]
What does that mean?[/QUOTE]
I guess:
[code]
function doSomething()
{
/**/if(somethingRelevant)
/**/{
/******/doSomethingElse();
/**/}
}
[/code]
Hopefully not what I think it means, and I think it means
[code]
// Incrementing i by one
i++;
// Decrementing x by two
x -= 2;
[/code]
Mmm missed the I hate tabs bit. Sounds terrible either way though.
[QUOTE=blankthemuffin;20423665]Hopefully not what I think it means, and I think it means
[code]
// Incrementing i by one
i++;
// Decrementing x by two
x -= 2;
[/code][/QUOTE]
How would you do that with tabs?
[QUOTE=arienh4;20423609]I guess:
[code]
function doSomething()
{
/**/if(somethingRelevant)
/**/{
/******/doSomethingElse();
/**/}
}
[/code][/QUOTE]
That... would be terrifying.
[code]
function doSomething()/*************/
{/**********************************/
/**/if(somethingRelevant)/**********/
/**/{/******************************/
/******/doSomethingElse();/*********/
/**/}/******************************/
}/**********************************/
[/code]:barf:
[QUOTE=turb_;20411175]Spaces are also a bitch to backspace compared to tabs.[/QUOTE]
Many text editors support backspacing *space tabs* with one keystroke.
You can get vim and emacs to do it with some configuration, and Kate (KDE text editor) does it.
[QUOTE=arienh4;20423609]I guess:
[code]
function doSomething()
{
/**/if(somethingRelevant)
/**/{
/******/doSomethingElse();
/**/}
}
[/code][/QUOTE]
I do this and then find an replace.
[QUOTE=ButtsexV2;20426837]I do this and then find an replace.[/QUOTE]
Why?
Your IDE or even most Editors can replace tabs with spaces if that's what you want.
Yeah, I'm working with a partner and a mentor. My mentor uses 2 spaces tabs so I found out you can configure that in MSVC++.
Sorry, you need to Log In to post a reply to this thread.