Code: Show/Hide while(die == false)
{ an If statment { bla bla bla die=true} an If statment {another 'If'{blablabla die=true;} bla bla bla die=true;} an If statment {another 'If'{blablabla die=true;} bla bla bla die=true;} And so on } |
Code: Show/Hide while (1)
{ if () break; if () break; if () break; } |
Code: Show/Hide void foo(int n)
{ if(n < 0) return; //lots of code here } |
Code: Show/Hide void foo(int n)
{ if (n >= 0) { //code here... } } |
Code: Show/Hide void foo(int n)
{ //Pre: n must be an integer >= 0. //Post: (w/e the function does as a result.) if (n >= 0) { //code here... } } |
D1st0rt wrote: | |
Ekted, so basically this would be bad?
|
Code: Show/Hide while(1)
0041470E mov eax,1 00414713 test eax,eax 00414715 je main+29h (414719h) { } 00414717 jmp main+1Eh (41470Eh) for(;;) { } 00414719 jmp main+29h (414719h) |
Smong wrote: |
Going back to the loop, is while(1) better than for(;![]() |
50% Packetloss wrote: |
while sets up a conditional jump while the for loop will just loop forever in this case. So ASM wise, an infinate FOR loop takes less code |
Code: Show/Hide int foo(int n)
{ if (n > 0) return 1; else if (n < 0) return -1; else return 0; } |
Code: Show/Hide int foo(int n)
{ int ret; if (n > 0) ret = 1; else if (n < 0) ret = -1; else ret = 0; return ret; } |
Cyan~Fire wrote: |
I don't use this style in cases that you have said are dangerous. I only use it in cases like the above, or in window procedures.
It especially makes sense to use multiple returns in window procs since, depending on the message, the function's code is completely different and isolated from the rest. |
Code: Show/Hide case WM_INITDIALOG:
for (i = 0; i < NUM_MSGS; i++) SendDlgItemMessage(page, IDC_M_SEL, CB_ADDSTRING, 0, (LPARAM)message_names[i]); for (i = 0; i < NUM_CINEM; i++) SendDlgItemMessage(page, IDC_M_SELC, CB_ADDSTRING, 0, (LPARAM)cinem_names[i]); return TRUE; |
Code: Show/Hide BOOL DialogProc (...)
{ BOOL ret = FALSE; ... switch (msg) { ... case WM_INITDIALOG: for (i = 0; i < NUM_MSGS; i++) SendDlgItemMessage(page, IDC_M_SEL, CB_ADDSTRING, 0, (LPARAM)message_names[i]); for (i = 0; i < NUM_CINEM; i++) SendDlgItemMessage(page, IDC_M_SELC, CB_ADDSTRING, 0, (LPARAM)cinem_names[i]); ret = TRUE; break; ... } return (ret); } |
Cyan~Fire wrote: |
Like I really need all that code bloating the size of my executable? |
Cyan~Fire wrote: |
It's still ugly though... and it's not like having a return there is a safety hazard in any way. I just don't think we all should be so strict about it.
On a side note, I #included the old iostream headers today in Dev-C++ and it complained at me for not using those darned template classes. Like I really need all that code bloating the size of my executable? Basically, I say all us likers of clean code (even if we do disagree on returns) should stage a coup d'etat against ANSI, MS, and Mr. Plauger and rule the world in its place. Or maybe I'm just really too tired to be talking. |
MGB wrote: |
I'm sure nothing you are making right now even comes close to the efficiency of making a program that will need real-time speed. And the size of the executable is negligable for what you are working with. |
Ekted wrote: |
If you look at each function and decide "this one doesn't need to be so strict" and "this one does", then you fall into the trap of making a mess as you edit, design, maintain, and evolve. |
Ekted wrote: |
I don't know your experience level, your career, or how old you are, but I think you will find yourself becoming more strict as you go after betting burned or watching it happen in other people's code in the same project you are one (when you have to work all weekend to find the function they didn't care to design right). |
Quote: |
However, if my current plan of things works out, I'm going to be co-founding a video game company... but that's probably just pretentions. |
Cyan~Fire wrote: |
More like I'll work for you, since I'm sure you wouldn't ever want me controlling what you code ![]() |