473,397 Members | 2,033 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,397 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 1953
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.