|
|
|||||||
| Home | Register | Downloads | FAQ | Members List | Calendar | Arcade | Mark Forums Read |
» Less advertising throughout
» Post and participate in discussions
» Network with other forum members
» Free private messaging
![]() |
|
|
Thread Tools | Display Modes |
|
|
#81 |
|
代言人
![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2006
Location: 應許之地
Posts: 7,056
|
I know, I even align my variables and constants LOL like this: Code:
/* Fourth order Runge Kutta method */
for ( int j = 0; j <= (Points - 1); j++ )
{
for ( int m = 0; m < 4; m++)
for ( int n = 0; n < 3; n++ )
{
switch (m)
{
case 0:
X = initial[0];
Y = initial[1];
Z = initial[2];
break;
case 1:
X = initial[0] + k[0][0] / 2;
Y = initial[1] + k[0][1] / 2;
Z = initial[2] + k[0][2] / 2;
break;
case 2:
X = initial[0] + k[1][0] / 2;
Y = initial[1] + k[1][1] / 2;
Z = initial[2] + k[1][2] / 2;
break;
case 3:
X = initial[0] + k[2][0];
Y = initial[1] + k[2][1];
Z = initial[2] + k[2][2];
break;
}
switch (n)
{
case 0:
k[m][n] = h * ( (float)SIGMA * ( Y - X ) );
break;
case 1:
k[m][n] = h * ( X * ( (float)RHO - Z ) - Y );
break;
case 2:
k[m][n] = h * ( X * Y - (float)BETA * Z );
break;
}
}
}
I hate it when it looks like 'switch (n)' Isn't switch (m) beautiful? xDDDD~~~ *stares at switch(n)*
__________________
![]() |
|
|
|
| Advertisement | [Remove Advertisement] | ||
|
|
|
#82 |
|
You're already dead...
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Sep 2007
Location: Planet Vegeta
Posts: 5,385
|
hehe something i do differently though, is i indent my cases from the 'switch'. also, when i have just 1 statement on every switch case, i usually try to fit each case on one line (my widescreen monitor also lets me have wider-code) i would probably write switch(n) something like this: Code:
switch (n) {
case 0: k[m][n] = h *((Y-X) * (float)SIGMA); break;
case 1: k[m][n] = h * (X *((float)RHO - Z) - Y); break;
case 2: k[m][n] = h * (X*Y - (float)BETA * Z ); break;
}
well formatted code is sometimes just so nice to look at, so i totally understand what you mean when you say switch(m) is beautiful ![]() ps. if it was a hw assignment for uni, i would write switch(n) like this: Code:
switch (n) {
case 0: // Insert comment here
k[m][n] = h * ( (float)SIGMA * ( Y - X ) );
break;
case 1: // Insert comment here
k[m][n] = h * ( X * ( (float)RHO - Z ) - Y );
break;
case 2: // Insert comment here
k[m][n] = h * ( X * Y - (float)BETA * Z );
break;
}
__________________
"It was, of course, a lie what you read about my religious convictions, a lie which is being systematically repeated. I do not believe in a personal God and I have never denied this but have expressed it clearly. If something is in me which can be called religious then it is the unbounded admiration for the structure of the world so far as our science can reveal it." - Albert Einstein check out my blog ![]() Last edited by cottonvibes; December 4th, 2009 at 04:36.. |
|
|
|
|
|
#83 |
|
代言人
![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2006
Location: 應許之地
Posts: 7,056
|
'Art' of coding xD I do the same when I put comments in switch :O I might take your way to do single line cases xD
__________________
![]() |
|
|
|
|
|
#84 | |
|
You're already dead...
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Sep 2007
Location: Planet Vegeta
Posts: 5,385
|
also another trick i like to use is on cases like this: Code:
if (x != y)
doCrap();
else {
doSomeOtherCrap1();
doSomeOtherCrap2();
}
Code:
if (x == y) {
doSomeOtherCrap1();
doSomeOtherCrap2();
}
else doCrap();
Quote:
![]() yeh coding is definitely an art. well... depends on the person coding i guess. there are some people that obviously don't think of it as art, as their code looks like crap, and they completely ignore formatting as well as being terribly inconsistent with the way they write their code. for those people its just a tool to accomplish something.
__________________
"It was, of course, a lie what you read about my religious convictions, a lie which is being systematically repeated. I do not believe in a personal God and I have never denied this but have expressed it clearly. If something is in me which can be called religious then it is the unbounded admiration for the structure of the world so far as our science can reveal it." - Albert Einstein check out my blog ![]() |
|
|
|
|
|
|
#85 |
|
Registered User
![]() ![]() ![]() ![]() Join Date: Apr 2007
Location: indiana
Posts: 694
|
mmm case statements beg for enums. and yeah, flipping the negative conditional is good practice.
|
|
|
|
|
|
#86 | |
|
代言人
![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2006
Location: 應許之地
Posts: 7,056
|
Quote:
![]() I always like my else statement to be one line if possible xD omg we have a lot of taste in common, don't we? XP~ I hate the naming convention of a lot of people too, besides format.
__________________
![]() |
|
|
|
|
|
|
#87 |
|
代言人
![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2006
Location: 應許之地
Posts: 7,056
|
for bool functions, I do Code:
bool func(int x, int y)
{
return
(x == y)
&& (x > CONST);
}
Code:
bool func(int x, int y)
{
if (x == y && x > CONST)
return true;
else return false;
}
It's less calculation, easier to read, looks nicer. xD
__________________
![]() |
|
|
|
|
|
#88 | |
|
Knowledge is the solution
![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2002
Location: Pittsburgh, US. Previously in Mexico City
Posts: 7,160
|
Quote:
The secret of "overly verbose code", is just following a coding guidelines that just says that every line should only apply one change to the code context, (except for standardized structures like the for initialization, etc). Doing it that way makes it incredibly easy for anyone to follow an algorithm, since they can see line per line what a program is doing. Trying to stuff 30 changes into a single line doesn't really do anything for code efficiency and does nothing for other programmers who will look later at your code. Although I don't advocate either extreme, I tend to lean more towards being a little more verbose than necessary so that anyone can eventually look into my code and extend it (and hence my papers get more citations )
__________________
|
|
|
|
|
|
|
#89 |
|
代言人
![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2006
Location: 應許之地
Posts: 7,056
|
Well, I rely a lot on comments on helping readers. One thing I hate about verbose code is that it usually slows down the speed.
__________________
![]() |
|
|
|
|
|
#90 | ||
|
You're already dead...
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Sep 2007
Location: Planet Vegeta
Posts: 5,385
|
heh i would just do: Code:
bool func(int x, int y) {
return (x == y) && (x > CONST);
}
btw another cool trick you can do in c++ when dealing with integers and you need to convert them to bool you can do this: Code:
int a = 140; bool b = !!a; // makes b == true Code:
int a = 140; int b = !!a; // b == 1 a quick example is say you had the following code: Code:
int a = 140;
int b;
if (a) { b = 8; }
else { b = 0; }
Code:
int a = 140; int b = (!!a) << 3; // b == 8 i love cool bitwise tricks like that. languages like c#/java limit tremendously how much cool tricks you can do (you can't do the above in c#/java). Quote:
![]() anyways i've mainly just been talking about my preferences, i'm not saying my preferences are the correct way to do something, but rather its how i like it. i know great coders that like to code more spaced out than me; i don't try and convince them my way is better, but i do try to defend my style when its criticized. most coders like to code the spaced-out way. compactness is more rare so it needs more defending xD Quote:
when i've been specifically talking about 'overly verbose' its different than the way you defined it. there is some code in pcsx2 for example that is very bloated, in terms of "it has way more code than needed to accomplish the same task". a quick good example is what fadingz showed: comparing something like this: Code:
bool foo() { return x==y; }
Code:
bool foo() {
if (x==y) return true;
else return false;
}
it is like just blurting out w/e pops into your head first and writing it down; instead i always try and optimize it to get a nice consice result like the above. now that is a very simple example, but imagine now 5k lines of someone that codes in a bloated way, has silly inefficient algorithms, and has stuff formatted terribly (i mean like not even lining up the start of lines; and not caring about it) once you imagine that you get some of the old source files i've seen in pcsx2. i won't link them to protect the authors identity though ;p
__________________
"It was, of course, a lie what you read about my religious convictions, a lie which is being systematically repeated. I do not believe in a personal God and I have never denied this but have expressed it clearly. If something is in me which can be called religious then it is the unbounded admiration for the structure of the world so far as our science can reveal it." - Albert Einstein check out my blog ![]() |
||
|
|
|
|
|
#91 | |
|
Knowledge is the solution
![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2002
Location: Pittsburgh, US. Previously in Mexico City
Posts: 7,160
|
Quote:
![]() Well, in any case i won't push the point any further since we seem to have reached a deadlock here. Just for the record, where I actually use tricks like that for any particular reason (performance, trying to compact code, etc), I always try to leave a more extended version of the code as a comment for people to know what's going on. @the pcsx2 example: Well that's an example I can agree with, though more than being verbose that is just plain redundant.
__________________
Last edited by Proto; December 4th, 2009 at 06:14.. |
|
|
|
|
|
|
#92 | |||
|
Crazy GFX coder
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Location: Dominican Republic/Austria
Posts: 8,101
|
Quote:
am not trying to start a fight over here as iīve been trying to finally establish a normal conversation and debate. i really understand/accept/agree or whatever your preference is that you like to code things unnecessarily complicated/unreadable/compact but somehow am under the impression that you actually think that technique makes you "cool" or more "advanced" and i just donīt think its that way. if thatīs not the case than just ignore the above text. Quote:
you know??? because it could give the impression to others that may not mature in the materia that those languages are "limited" but in other things and not on the area you actually mean.. cool tricks are fine but sometimes those are not relevant for coding. just by instance i want to mention a small comparison about VB6 and VB.Net. since youīre a VB coder yourself you will know that there are several tricks that can be done in VB6 that canīt be accomplish anymore in VB.Net.for example you can declare a variable as "Optional" or even as "Any", while this ability may be cool sometimes it doesnīt make much sense just like the "Option Explicit" to Force explicit declaration of all variables in a file just because by default its not that way... also that doesnīt mean VB.Net is "limited" by any means. again this is just my point of view and by any means an attack and i really have no idea why we keep misunderstanding each other ![]() Quote:
__________________
![]() Current development tools: Visual C++.net, Visual C#.net Visual VB.net, Visual Webdeveloper.net Bloodshed Dev C++, Borland C++ Visual Basic 6 Last edited by @ruantec; December 4th, 2009 at 06:55.. |
|||
|
|
|
|
|
#93 | |||
|
You're already dead...
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Sep 2007
Location: Planet Vegeta
Posts: 5,385
|
Quote:
tbh, i don't know what the code ends up compiling to though on x86-32. all i know is that in certain situations it is more compact to use than explicitly using conditionals. it might end up using some variation of SETcc in the compiled result... its possible it would be a slowdown in some cases as well... when you think about conditional jumps though you have to think about branch prediction. jumps are fast if branch is predicted correctly, but you get a hit on misses. current cpus have very sophisticated and fast jumps though. your work with CUDA sounds interesting. the architecture sounds like something where a lot of bitwise tricks would come in handy. maybe someday i'll work with CUDA and see first hand though xD but currently i have a lot of other stuff i wish to learn that comes first ![]() Quote:
i don't like the term deadlock as i don't really feel like i've been arguing here, but rather just expressing my opinions in a conversation about programming xD ideally i want to learn more tricks similar to what i post, which is why i constantly try to promote conversation; even if it may sound like i'm being an elitist or what not... generally in our pcsx2 dev channel we have a lot of random coding conversations, and talk about cool tricks and different ways to code stuff... when i learn a cool new trick i find it really exciting and go on to use it when the situation comes up. Quote:
my code now looks a lot different from 3 years ago, and obviously a whole lot different than 10 years ago when i started coding with vb. eventually you do learn advanced tricks besides just the 'basics'. if not then you have not progressed as a coder. i love learning new advanced tricks, and i like sharing what i have learned. by having such conversations, everyone wins. even through debates (and sometimes arguments) you can learn new things. i have had debates with many people here, i used to be known for debating with thanakil all the time about random stuff; yet thanakil always brought up great support for his claims and i would learn new information. the problem is when people don't support their claims with any useful information. when that happens nobody learns any real knowledge, but instead they just hear the opinions of another individual. i want to learn; and what i want to learn about is programming techniques, tricks, architecture, and cool stuff... i should also note that i get angry when people spread false information to others that don't know enough to know its incorrect. its common for people new to programming to just blindly believe other more-experienced coders statements; regardless if they're correct or not. in those cases i don't hesitate to point out the flaws in the original arguments; i mainly do this to stop the spread of incorrect knowledge.
__________________
"It was, of course, a lie what you read about my religious convictions, a lie which is being systematically repeated. I do not believe in a personal God and I have never denied this but have expressed it clearly. If something is in me which can be called religious then it is the unbounded admiration for the structure of the world so far as our science can reveal it." - Albert Einstein check out my blog ![]() |
|||
|
|
|
|
|
#94 |
|
Knowledge is the solution
![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2002
Location: Pittsburgh, US. Previously in Mexico City
Posts: 7,160
|
Well, in appreciation or the trick you gave me here is one of mine. (though maybe you already know it) How to divide (or multiply) by powers of 2 without doing any divisions at all: given a dividend 'x' and a divisor 'y' that is a power of 2. int divisor = ffs(y) -1; //* int result = x >> divisor; //nothing new here. * (find first set, which obtains the position of the first set bit (bit that is one) starting from the fist significant bit. Complete with the -1 returns a result equal to doing log(y)/log(2)). This is useful in GPU's since divisions are an operation that takes as much as 4 to 16 times (depending on the precision) than a bitwise/add/multiply operation.
__________________
|
|
|
|
|
|
#95 | |
|
You're already dead...
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Sep 2007
Location: Planet Vegeta
Posts: 5,385
|
Quote:
also, for beginners they will not know enough to know that their language is limited. there is an old chinese saying: "the frog in the well won't know the ocean." essentially saying that people won't know more than what they've seen/learned/experienced. its true that beginner coders could misunderstand when i talk about other languages being limited; but i am having conversations here with people already familiar with coding, not beginners. also, in many threads involving which languages people should learn programming with, i always recommend stuff like vb/java/c#. to a beginner, c++ is evil and the difficulty might scare them off from programming.
__________________
"It was, of course, a lie what you read about my religious convictions, a lie which is being systematically repeated. I do not believe in a personal God and I have never denied this but have expressed it clearly. If something is in me which can be called religious then it is the unbounded admiration for the structure of the world so far as our science can reveal it." - Albert Einstein check out my blog ![]() |
|
|
|
|
|
|
#96 | |
|
Registered User
![]() ![]() Join Date: Sep 2006
Location: surrey
Posts: 111
|
Quote:
just yesterday we got a bit of a lecture about using if(b.e.) return false else return true. basically it was use return b.e. instead. |
|
|
|
|
|
|
#97 | |
|
You're already dead...
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Sep 2007
Location: Planet Vegeta
Posts: 5,385
|
Quote:
although your in-depth explanation using ffs() made me question if i was ![]() a shift is always nicer than a divide/mul operation when applicable on pretty much any current hw. that only works with integers however; when working with CUDA i assume most of the time you work with floats which obviously you can't do that trick with. anyways in a related note, i'm sure you probably know about the & trick to do modulus with powers of 2. quick example: 11 % 8 == 11 & (8-1) also by the same principal you can easily tell if a number if even/odd by using an AND. if (n & 1) // is odd else // is even when i learned about that i thought it was interesting xD
__________________
"It was, of course, a lie what you read about my religious convictions, a lie which is being systematically repeated. I do not believe in a personal God and I have never denied this but have expressed it clearly. If something is in me which can be called religious then it is the unbounded admiration for the structure of the world so far as our science can reveal it." - Albert Einstein check out my blog ![]() |
|
|
|
|
|
|
#98 | |
|
Knowledge is the solution
![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2002
Location: Pittsburgh, US. Previously in Mexico City
Posts: 7,160
|
Yup, I knew about that one. Actually all those tricks that dance around divide and modulus are all things we have to do everyday unfortunately, given the low performance of those operations on the GPU. Quote:
__________________
|
|
|
|
|
|
|
#99 | |
|
You're already dead...
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Sep 2007
Location: Planet Vegeta
Posts: 5,385
|
Quote:
there's something interesting about using the gpu to do floating point calculations regarding ps2 emulation. the ps2 does not support nan/inf float values, neither does cuda afaik. i believe cuda treats this range as just normal floating point values; which the ps2 does as well. many problems on pcsx2 are due to x86-32 cpus supporting inf/nans. so potentially running the calculations on the gpu would fix that (of course its unpractical due to the overhead; CUDA's benefit is when running many parallel calculations, which emulation isn't suited for).
__________________
"It was, of course, a lie what you read about my religious convictions, a lie which is being systematically repeated. I do not believe in a personal God and I have never denied this but have expressed it clearly. If something is in me which can be called religious then it is the unbounded admiration for the structure of the world so far as our science can reveal it." - Albert Einstein check out my blog ![]() |
|
|
|
|
|
|
#100 | |||
|
Crazy GFX coder
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Location: Dominican Republic/Austria
Posts: 8,101
|
Quote:
Quote:
Quote:
__________________
![]() Current development tools: Visual C++.net, Visual C#.net Visual VB.net, Visual Webdeveloper.net Bloodshed Dev C++, Borland C++ Visual Basic 6 Last edited by @ruantec; December 4th, 2009 at 08:33.. |
|||
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|