|
how wide you go (if i don't want the overhead of another function call)
|
|
|
|
|
The wider you go, the more the danger of your code being classified as horror.
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
Randor wrote: do you actually use the goto statement that much in production code?
never, not because of my interest but it is not in my control, i used to follow the organisation standards. some force to use the do..while(0) horrors, i personally not against it. but i never do the nested way for readability unless the number of statement is less or there is considerable need of inline function.
BTW, what's your opinion?
modified on Friday, May 2, 2008 3:13 AM
|
|
|
|
|
Rajkumar R wrote: BTW, what's your opinion?
The goto statement has always had a bad reputation. I can remember reading many arguments in the C++ usenet newsgroups debating these issues. And beyond the argument 'The goto statement causes spaghetti code' nobody ever gave a reason why the goto statement was bad.
I'm much older now and more experienced so I can now answer this question myself now. It is my opinion that the goto statement should be avoided. The code that Judy posted is a superior method of implementing the function regardless of how ugly it is. My reasoning is quite simple in that the compiler cannot perform optimizations with all of those unconditional jumps all over the place. So by placing all of those goto statements in your function it causes the compiler to produce very different code. You can confirm this by editing your project and enabling 'Assembly With Source Code (/FAs)' for producing the ASM file.
Now I will argue a point in favor of a well placed goto statement. An experienced programmer may have intimate knowledge of compiler optimizations and determine that a goto statement will be beneficial. For example:
for(int x =0; x < iLimit;++x)
{
for(int y =0; y < iAnotherLimit;++y)
{
for(int i =0;i< ilastlimit;++i))
{
if(FALSE == DoWork(SomeArray[x][y][i]))
{
goto critical_error;
}
}
}
}
critical_error:
CleanUp();
In this example the compiler output will greatly benefit from the goto statement. There is no faster way to exit the nested loops. So here is my final statement regarding the issue.
Randors Conjecture:
The only time at which a C++ programmer should use a goto statement is to escape a deep nested loop.
Best Wishes,
-Randor (David Delaune)
|
|
|
|
|
Randor wrote: My reasoning is quite simple in that the compiler cannot perform optimizations with all of those unconditional jumps all over the place.
That's incorrect. Gotos are hard for humans, not for computers. In fact, the way nearly all compilers work is to transform the code into an intermediate representation where "if (a) { then; } else { otherwise; } becomes
if (a) goto 1 else goto 2
1: then;
goto 3
2: otherwise;
3: ...
And then the optimizer runs on that representation, so it can surely understand gotos. Optimizers can perfectly work with arbitrary control flow graphs, they're not limited to "structured" control flow.
Different source code may cause different machine code to be generated, but there's absolutely no reason that gotos would be less efficient.
For an explanation why goto is bad, read
go-to statement considered harmful[^]
|
|
|
|
|
Rajkumar R
thank you
|
|
|
|
|
Hello friends,
I am using Win32 apllication on Vc6.0 IDE.
I am using 20 buttons.
I want to find out tab order dynamically.
I want change focus to next control if we select next button.
abhi
|
|
|
|
|
|
Also, the Z-Order of the child controls is their tab order.
|
|
|
|
|
How can I know if a 3rd party unmanaged (e.g. cpp )dll is in debug or release ?
|
|
|
|
|
One way would be to see it there are any dependancies that the DLL has. If any of the DEBUG versions of the C runtime, or MFC libraries show up.
Chris Meech
I am Canadian. [heard in a local bar]
In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]
Donate to help Conquer Cancer[ ^]
|
|
|
|
|
how can i delete a record from a file???
i know that i need to put special character like * before the record that i want to delete
and my thoughts are :
search for the first word of the record
then put the special character before it
i know how to search for a word in a file but i dont know how to put the special character before that record
any thoughts ??
no gain without pain
|
|
|
|
|
You have to read the whole file into memory, then you can change it according to any rules you like, and then overwrite the file with the new, edited data. I don't know why you think you need to insert a *, but you really don't.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Hello dear friends,
How do we enable the toolbar to communicate with the webpage.
Suppose I've one interface method in toolbar's dll.
How can I call this method from the webpage that has one button
say "Edit".
When I click the "Edit" button in the webpage the toolbar dll's interface
method should be called.
How we can do this?
Thanks in advance to all.
ritz1234
|
|
|
|
|
hi
i have an error link saying that :
Linking...
File_Commands.obj : error LNK2001: unresolved external symbol "int __cdecl RegSetValueExString(struct HKEY__ *,class CString,int,int,class CString,int)" (?RegSetValueExString@@YAHPAUHKEY__@@VCString@@HH1H@Z)
File_Commands.obj : error LNK2001: unresolved external symbol "int __cdecl RegQueryValueExString(struct HKEY__ *,class CString,int,int &,class CString,int &)" (?RegQueryValueExString@@YAHPAUHKEY__@@VCString@@HAAH12@Z)
File_Commands.obj : error LNK2001: unresolved external symbol "int __cdecl RegQueryValueExNULL(struct HKEY__ *,class CString,int,int &,int,int &)" (?RegQueryValueExNULL@@YAHPAUHKEY__@@VCString@@HAAHH2@Z)
//***************************************************************//
knowing that those functions are declared int header file like that:
int RegQueryValueExString (HKEY hKey, CString lpValueName, int lpReserved, int& lpType, CString lpData, int& lpcbData);
int RegQueryValueExNULL (HKEY hKey, CString lpValueName, int lpReserved, int& lpType, int lpData, int& lpcbData);
int RegSetValueExString (HKEY hKey, CString lpValueName, int Reserved, int dwType, CString lpValue, int cbData);
<br />
and they are declared in the .cpp file as the following:<br />
<br />
lRtn = RegQueryValueExNULL(hKey, "", 0, lRtype, 0, lCdata);<br />
sValue = CString(lCdata,0);<br />
<br />
lRtn = RegQueryValueExString(hKey, "", 0, lRtype, sValue, lCdata);<br />
<br />
lRtn = RegSetValueExString(hKey, "", 0, REG_SZ, sValue, lsize);
//**********************************************//
when i compile ......0 errors
but when runing 3errors
what should i do ?
thanks in advance
lahom
|
|
|
|
|
lahom wrote: //***************************************************************//
knowing that those functions are declared int header file like that:
// Registry API To Query A String Value.
int RegQueryValueExString /* RegQueryValueExA */ (HKEY hKey, CString lpValueName, int lpReserved, int& lpType, CString lpData, int& lpcbData); // "advapi32.dll"
is this some thing tried to port from VB. It seems attempt to port alias in VB declaration. The definitions of the functions are not available.
you replace RegQueryValueExString with RegQueryValueExA (similarly other 2) in cpp where you are calling it.
or comment out those declarations in header and define the Macros like,
#define RegQueryValueExString RegQueryValueExA
#define RegQueryValueExNULL RegQueryValueExA
#define RegSetValueExString RegSetValueExA
|
|
|
|
|
lahom wrote: but when runing 3errors
You've shown linker errors, but what are these runtime errors you refer to?
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
DavidCrow wrote: What errors?
3 link errors
lahom wrote: i have an error link saying that :
|
|
|
|
|
DavidCrow wrote: You've shown linker errors, but what are these runtime errors you refer to?
without linking cannot run the program, make no sense.
lahom wrote: when i compile ......0 errors
but when runing 3errors
when run IDE tries to link ... link error.
|
|
|
|
|
My application has a main window that provides most of the functionality. The window itself is nice and clean, and very user friendly IMO. However, the class CPP file for it is growing large and hard to navigate (I have to use Ctrl+F to find most functions). The reason for this is there are a lot of possible interactions the user can have with the interface, each having a unique function. The .cpp file is currently 24kb (845 lines including whitespace); which is a lot for me, I usually don't write large programs.
I want to try to keep the code organized, and I'd like to know some good practices for doing this. (If I become a programmer as a job, I want to know how to make the code "presentable" and not just a mess.) Is it just a matter of trying to keep the functions sorted in groups (i.e. everything having to do with interaction of the list control together, then everything with the media player together, etc.. etc..) Or is there more that I can do to try to keep it organized?
Lastly, Visual Studio 2008 defaults to making wizard generated message handlers public. I would think that they should be protected in most cases. Is there a convention here that I'm not aware of, or should I indeed move the ones that other classes shouldn't be calling to protected?
Thanks again for any info!
|
|
|
|
|
Well, one way you could organize your code is by separating out functionality into different classes.
There are many ways to "break it up", but just separating out your business or application logic from the UI might be a good starting point.
- S
50 cups of coffee and you know it's on!
|
|
|
|
|
Well, 845 lines is not so much, my last programm was about 15000 (all together).
I like to separate functionalities a lot. Like a module/class to save datas, another for mathematical functions, another for GUI functions, another for "various" functions and so on.
But at the end, the only one that can make it right, is you. Because every programm is different and is a bit difficult to say something "standard"
x87Bliss wrote: i.e. everything having to do with interaction of the list control together, then everything with the media player together, etc.. etc..)
Of course making groups regarding functionalities is a great idea, but it may be a project where another aproach is better.
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
|
|
|
|
|
Besides just making classes for specific functionality, I also decided to start using '#pragma region' directives; so I can collapse groups of functions together. It's been very useful.
I barely know anything about pragma statements. In my case I don't have to worry about it, since I will be using Visual Studio always to compile this. I am just wondering what would happen if a different compiler saw '#pragma region' if it'd skip over it, or have an error?
|
|
|
|
|
x87Bliss wrote: I am just wondering what would happen if a different compiler saw '#pragma region' if it'd skip over it, or have an error?
"If the compiler finds a pragma it does not recognize, it issues a warning, but compilation continues" from msdn
or something like,
#if _MSC_VER > version
#pragma region Region_1
#endif
x87Bliss wrote: the class CPP file for it is growing large and hard to navigate (I have to use Ctrl+F to find most functions).
845 is not so lengthy, you can learn some shortcuts to speed up search. for instance you can use indexed search (ctrl + i) and type to search while typing. and to navigate to class and member in a file, the code view has scope and member field or class view you can select to navigate to the function, F12 a handy shortcut to navigate to the definition from its reference...
You may redesign the GUI into different layouts or split GUI into pages if you have tomany controls that will be annoying the user. If your GUI is neat, may be you group range of event handlers that is handled in different class implemented in different files like ON_CONTROL_RANGE, ON_COMMAND_RANGE, ...
|
|
|
|
|
Thanks That's some of the most useful info I've seen. The F12 will definitely come in handy.
|
|
|
|
|