473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

c code refactoring or optimizing

Hi.
How can I find some ready tool or how can I find a proper way to
develope tool which will convert such code:

if (a>b+2+2)
{
f1();
f2(a*3*2);
} else
{
f1();
f3(a);
}

To something like that:

f1();
if (a>b+4)
f2(a*6)
else
f3(a);

In another words, this is what is done by modern compiler at
optimization phase, right? But I need to have C-source at the input
and C-source at the output.
Could please anyone point me to any ideas or ready works.

Mar 27 '07 #1
3 1947
Dennis Yurichev wrote:
Hi.
How can I find some ready tool or how can I find a proper way to
develope tool which will ...
By doing lots and lots of research followed by a moderate amount of
development time.
Mar 27 '07 #2
On Mar 27, 9:10 pm, "Dennis Yurichev" <Dennis.Yuric...@gmail.com>
wrote:
Hi.
How can I find some ready tool or how can I find a proper way to
develope tool which will convert such code:

if (a>b+2+2)
{
f1();
f2(a*3*2);} else

{
f1();
f3(a);

}

To something like that:

f1();
if (a>b+4)
f2(a*6)
else
f3(a);

In another words, this is what is done by modern compiler at
optimization phase, right? But I need to have C-source at the input
and C-source at the output.
Could please anyone point me to any ideas or ready works.
there exist reverse compilers that compile binary code back to C
source but ID names are not comprehendible.and you have to rename
identifiers yourself.

Mar 27 '07 #3

"Dennis Yurichev" <De*************@gmail.comwrote in message
news:11**********************@r56g2000hsd.googlegr oups.com...
Hi.
How can I find some ready tool or how can I find a proper way to
develope tool which will convert such code:

if (a>b+2+2)
{
f1();
f2(a*3*2);
} else
{
f1();
f3(a);
}

To something like that:

f1();
if (a>b+4)
f2(a*6)
else
f3(a);

In another words, this is what is done by modern compiler at
optimization phase, right? But I need to have C-source at the input
and C-source at the output.
Could please anyone point me to any ideas or ready works.
The first refactoring makes sene, moving f1() out of the if...else bodies
into mainline.

Changing b+2+2 to b+4 and a*3*2 to a*6 may or may not make sense.depending
on what those magic numbers mean. That is, b+2+2 may be clearer what is
going on than b+4. Better yet would make them: b+Rows+Space or whatever
those 2's meant. Without context it's impossible to tell, which is why
magic numbers are generally considered bad.

And a*3*2 maybe that means a 3x2 array? I don't know. They are magic
numbers. Better would be to track down the magic numbers and fix them.

But, hey, I think most of us are guilty of using magic numbers.

Looking at some of my own code at random I come across:

JVEC3 BitToRight = jGet_Position_Around_Center(
Client.Camera.X_Degrees - 90, 0.050f, -0.012f, Client.Camera.Position );
JVEC3 TempPosition = jGet_Position_Around_Center(
Client.Camera.Y_Degrees , 0.175f, -0.0f, jVec3(BitToRight.y, 0.0f, 0.0f));
BitToRight.y = TempPosition.x;
Client.RightGunPosition = jGet_Position_Around_Center(
Client.Camera.X_Degrees < 180.0f ? Client.Camera.X_Degrees + 180.0f :
Client.Camera.X_Degrees - 180.0f, cos(Client.Camera.Y_Degrees * radindeg) *
0.18f, -0.0f, BitToRight);

with the magic numbers 90, 0.050f, -0.012f, 0.175f, -0.0 (negative none the
less O.o), 180.0f 0.18f

Now the problem becomes, when I want to maintain this code, I have to figure
out what all those numbers mean. Which means I"m going to have to look up
the function calls themselves and see what their parameters are.

Just be careful when working with magic numbers.
Mar 27 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: | last post by:
Hi, Refactoring a winform causes problems if moving this form to a subdirectory in the same project. What is the workaround for this and will this be fixed in future? Thanks /BOB
67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
6
by: Dean Ware | last post by:
Hi, I am part way through developing a C++ application. I am developing it on my own and using VC++ 6.0. I am now at the stage where I wish to start tidying up my code. I have lots of...
14
by: joshc | last post by:
I'm writing some C to be used in an embedded environment and the code needs to be optimized. I have a question about optimizing compilers in general. I'm using GCC for the workstation and Diab...
15
by: CR | last post by:
I've noticed that the trend these days is to declare variables in the middle of code instead of at the top. What is the advantage of this? It seems like it makes it hard to reuse variables. Here...
7
by: Emma Burrows | last post by:
Hello all, I've been writing C# applications and web sites for some time, but I'm now planning to share my latest code with the world at www.codeproject.com. The code works fine, but I'd like...
4
by: shuisheng | last post by:
Dear All, I have a code developed by former employees. I extract some part of it as below: // definition of class CWNPrimitiveFace, it represent a face class CWNPrimitiveFace : public...
6
by: Dennis Yurichev | last post by:
Hi. How can I find some ready tool or how can I find a proper way to develope tool which will convert such code: if (a>b+2+2) { f1(); f2(a*3*2); } else {
3
by: mpjones2 | last post by:
I'm trying to organize some code I've written and was wondering if anyone can provide any suggestions on an equivalent to #Region that can be used within a function to collapse sections of code...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.