• Coding Tricks
    122 replies, posted
[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] Well that sounds logical. But how said by others, I prefere defining variables each per line. So I like it more to be "char* variable" instead of "char *variable" - Because later - in every good IDE with stuff like "MS-Intellisense" you see your var is of type "char*" - so a pointer of type char. That's why I prefere my way compared to the other.
[QUOTE=aVoN;20430182]Well that sounds logical. But how said by others, I prefere defining variables each per line. So I like it more to be "char* variable" instead of "char *variable" - Because later - in every good IDE with stuff like "MS-Intellisense" you see your var is of type "char*" - so a pointer of type char. That's why I prefere my way compared to the other.[/QUOTE] This!
[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 is so true.
My code for recent simulation I'm writing is pretty weird, mostly because it uses very long variable names (it helps A REAL LOT when viewing code, because "mna1_voltage" is less descriptive than Systems.Electric.Bus["mna1"].Voltage), it is Lua code, and it also uses a lot of weird aligning. Example: [lua] -- target quantity in fuel lines local targetQuantitySection1 = MOLAR_MASS*(reactantPressure*(SECTON1_REALVOLUME))/(8.314*reactantTemperature) local targetQuantitySection2 = MOLAR_MASS*(reactantPressure*(SECTON2_REALVOLUME))/(8.314*reactantTemperature) local targetQuantitySection3 = MOLAR_MASS*(reactantPressure*(SECTON3_REALVOLUME))/(8.314*reactantTemperature) local targetQuantity = targetQuantitySection1 + targetQuantitySection2 + targetQuantitySection3 -- reactant in pipe local pipeQuantitySection1 = get(TK1_MANF) * get(XSV.Systems.FuelCells.PipeSections[1][Reactant].Quantity) local pipeQuantitySection2 = get(TK2_MANF) * get(XSV.Systems.FuelCells.PipeSections[2][Reactant].Quantity) local pipeQuantitySection3 = get(XSV.Systems.FuelCells.PipeSections[3][Reactant].Quantity) local pipeQuantity = pipeQuantitySection1 + pipeQuantitySection2 + pipeQuantitySection3 [/lua]
[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] FYI those are all illegal, "hello" is of type const char[].
[QUOTE=nullsquared;20434021]FYI those are all illegal, "hello" is of type const char[].[/QUOTE] Notice the /**/, the example is (pre-1999) C.
[QUOTE=Dr Magnusson;20424134][code] function doSomething()/*************/ {/**********************************/ /**/if(somethingRelevant)/**********/ /**/{/******************************/ /******/doSomethingElse();/*********/ /**/}/******************************/ }/**********************************/ [/code]:barf:[/QUOTE] God, that's like coding with sticky hands.
[QUOTE=jA_cOp;20434122]Notice the /**/, the example is (pre-1999) C.[/QUOTE] The /* */ didn't say anything about pre-1999 C, all I know is that you say "in C and C++"
[QUOTE=nullsquared;20436473]The /* */ didn't say anything about pre-1999 C, all I know is that you say "in C and C++"[/QUOTE] I think he was referring to /* */ rather than //.
[QUOTE=nullsquared;20436473]The /* */ didn't say anything about pre-1999 C, all I know is that you say "in C and C++"[/QUOTE] That's because what it is actually showing, is valid in C and C++. The example itself is using C < 99 because otherwise why would you ever use /**/ for a comment to the end of the line.
[QUOTE=blankthemuffin;20442375]That's because what it is actually showing, is valid in C and C++. The example itself is using C < 99 because otherwise why would you ever use /**/ for a comment to the end of the line.[/QUOTE] I think you're missing the point. This: [cpp] char *str = "hello"; [/cpp] is not valid in C or C++ (which is what his post said). Whether you want to use /* */ for comments or // is completely irrelevant. [editline]09:04PM[/editline] Yes, the pointers "idea" is correct, but the code itself isn't. That's all I'm pointing out (no pun intended).
[QUOTE=jA_cOp;20418265]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)[/QUOTE] *Highfive* In this case it was ActionScript 3, but could really be most forms of ECMA (except I like AS3 more due to the fact that usual ecma doesn't declare the type, whereas AS3 does.) As someone else wrote, this is horrible: [php] var intNumber1:int = 10; var intNumber2:int = 20; var intResult:int = intNumber * 5 + intNumber2 * 77; [/php] Where as this is definitely much better (even though it does exactly the same, and I'm usually one for "less lines the better" coding. [php] var intNumber1:int = 10; var intNumber2:int = 20; var intResult:int = (intNumber * 5) + (intNumber2 * 77); [/php]
I just can't write this: [code] if(){ } [/code] I need that newline :P [code] if() { } [/code] and I just need the {} where I don't even need them: [code] if() { cout <<..... } else { cout <<..... } [/code]
In Java I make everything final that doesn't actually need to be modified: member variables, method parameters, local variables, whatever. [cpp] public class GreetingMaker { private final String greeting; public GreetingMaker(final String greeting) { this.greeting = greeting; } public String makeGreeting(final String addressee) { return greeting + ", " + addressee + '!'; } public static void main(final String[] args) { final GreetingMaker greetingMaker = new GreetingMaker("Hello"); System.out.println(greetingMaker.makeGreeting("world")); } } [/cpp] I've found that surprisingly few variables actually need to be modified after they're initialized. Making them final guards against accidental modification, or forgetting to initialize a variable in all code paths.
[QUOTE=nullsquared;20442445]I think you're missing the point. This: [cpp] char *str = "hello"; [/cpp] is not valid in C or C++ (which is what his post said). Whether you want to use /* */ for comments or // is completely irrelevant. [editline]09:04PM[/editline] Yes, the pointers "idea" is correct, but the code itself isn't. That's all I'm pointing out (no pun intended).[/QUOTE] It is valid C.
[QUOTE=Wyzard;20447366]In Java I make everything final that doesn't actually need to be modified: member variables, method parameters, local variables, whatever. [cpp] public class GreetingMaker { private final String greeting; public GreetingMaker(final String greeting) { this.greeting = greeting; } public String makeGreeting(final String addressee) { return greeting + ", " + addressee + '!'; } public static void main(final String[] args) { final GreetingMaker greetingMaker = new GreetingMaker("Hello"); System.out.println(greetingMaker.makeGreeting("world")); } } [/cpp] I've found that surprisingly few variables actually need to be modified after they're initialized. Making them final guards against accidental modification, or forgetting to initialize a variable in all code paths.[/QUOTE] Const everywhere looks sexy in c++, final everywhere looks ugly as fuck.
[QUOTE=r4nk_;20447872]Const everywhere looks sexy in c++, final everywhere looks ugly as fuck.[/QUOTE] Not after you get used to it. I see "final" almost as a standard part of a variable declaration, like the "var" keyword in JavaScript. When I see something that [i]isn't[/i] final it jumps out at me because it looks strange, and I read the surrounding code to make sure I understand the cases in which the variable might be left uninitialized, or modified after initialization. Actually, it's C++ where I'm not quite used to it yet, since I started this habit with Java code at work, and I do much less C/C++ than Java these days. I've always been diligent about const-correctness in relation to classes, but something like "int const x = whatever()" as a local variable in a function looks a little weird to me.
[QUOTE=Wyzard;20447366]In Java I make everything final that doesn't actually need to be modified: member variables, method parameters, local variables, whatever. <...> I've found that surprisingly few variables actually need to be modified after they're initialized. Making them final guards against accidental modification, or forgetting to initialize a variable in all code paths.[/QUOTE] Also increases speed a little and it's a good step to immutablility :)
[QUOTE=Wyzard;20447366]In Java I make everything final that doesn't actually need to be modified: member variables, method parameters, local variables, whatever. [cpp] public class GreetingMaker { private final String greeting; public GreetingMaker(final String greeting) { this.greeting = greeting; } public String makeGreeting(final String addressee) { return greeting + ", " + addressee + '!'; } public static void main(final String[] args) { final GreetingMaker greetingMaker = new GreetingMaker("Hello"); System.out.println(greetingMaker.makeGreeting("world")); } } [/cpp] I've found that surprisingly few variables actually need to be modified after they're initialized. Making them final guards against accidental modification, or forgetting to initialize a variable in all code paths.[/QUOTE] I do this too. Also all classes are final unless they are meant to be subclassed (and if they aren't to be instantiated, they're abstract, even if they could be concrete). IMO classes should be final by default, you should either plan for inheritance or prohibit it altogether.
[code] int EveryThing; int IInitialise; int InTermsOfVariables; int MustHaveCaps; [/code]
[QUOTE=t0rento;20426907]Why? Your IDE or even most Editors can replace tabs with spaces if that's what you want.[/QUOTE] I don't know, I've always done it though.
Ew I hate you already Archy.
[QUOTE=Archy;20460902][code] int EveryThing; int IInitialise; int InTermsOfVariables; int MustHaveCaps; [/code][/QUOTE] I only use caps for objects and functions, for variables I separate words/parts with underscores.
[QUOTE=Archy;20460902][code] int EveryThing; int IInitialise; int InTermsOfVariables; int MustHaveCaps; [/code][/QUOTE] I let R# rename my variables for me. CamelCase for public, _camelCase for private.
+1 for ReSharper for C# If you develop in C#, definitely must have [url]http://www.jetbrains.com/resharper/[/url]
[QUOTE=streeter;20470497]+1 for ReSharper for C# If you develop in C#, definitely must have [url]http://www.jetbrains.com/resharper/[/url][/QUOTE] Just if you didn't get it, R# is just an abbreviation for Resharper as C# is for C Sharp.
[QUOTE=Archy;20460902][code] int EveryThing; int IInitialise; int InTermsOfVariables; int MustHaveCaps; [/code][/QUOTE] Meh, I generally do. [cpp] class AClass { int privatevar; public: int PublicVar; };[/cpp] Although it always frustrates me when I come across. [cpp] class Box; Box box;[/cpp]
[QUOTE=arienh4;20470970]Just if you didn't get it, R# is just an abbreviation for Resharper as C# is for C Sharp.[/QUOTE] I'm pretty sure he did get it, hence the "+1" part. He was saying he agreed with you and was encouraging it's use :v:
[QUOTE=mechanarchy;20471098]I'm pretty sure he did get it, hence the "+1" part. He was saying he agreed with you and was encouraging it's use :v:[/QUOTE] Figured I'd at least tell people who didn't. It confused me at first.
[QUOTE=high;20471049]Meh, I generally do. [cpp] class AClass { int privatevar; public: int PublicVar; };[/cpp] Although it always frustrates me when I come across. [cpp] class Box; Box box;[/cpp][/QUOTE] For this, I use some sort of "light" Hungarian Notation (since HN is supposed not to be used anymore). [cpp]class CClassName{ private: bool m_bVariable; } [/cpp] I only use this on classes, structs etc (m_ + typename + varname). For everything else, I use normal naming like "char thisismycharihavetodosomeshitwith" or "int SomeCoolInt".
Sorry, you need to Log In to post a reply to this thread.