473,769 Members | 3,350 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why Is This Bad Code?

Hi, everyeone,

I recently stumbled on some code that someone else wrote that I don't like.
However, I'm having trouble articulating what the bad quality of the
following code is. The unnecessary use of indentation and else statements
seems to be counter-intuitive to me. However, I'm hoping for an argument
that is more formal than my intuition.

<quote>
// Make a function call based on each parameter not meeting certain
conditions.
if (a == 3)
error_code = 1;
else
{
if (b == 10)
error_code = 2;
else {
if (c == 7)
error_code = 3;
else
error_code = call_function(a ,b,c);
}
}

return error_code;
</quote>

Personally, I find all of the else statements distracting. Is there
anything that you don't like about the organization of this simple code?

Thanks,
Scott

--
Remove .nospam from my e-mail address to mail me.

http://www.e-scott.net
Jul 22 '05 #1
18 1809
"Scott Brady Drummonds" <sc************ **********@inte l.com> wrote in
message news:ck******** **@news01.intel .com...
Hi, everyeone,

I recently stumbled on some code that someone else wrote that I don't like. However, I'm having trouble articulating what the bad quality of the
following code is. The unnecessary use of indentation and else statements
seems to be counter-intuitive to me. However, I'm hoping for an argument
that is more formal than my intuition.

<quote>
// Make a function call based on each parameter not meeting certain
conditions.
if (a == 3)
error_code = 1;
else
{
if (b == 10)
error_code = 2;
else {
if (c == 7)
error_code = 3;
else
error_code = call_function(a ,b,c);
}
}

return error_code;
</quote>

Personally, I find all of the else statements distracting. Is there
anything that you don't like about the organization of this simple code?


I think it's better to reduce the indentation and write

if (a == 3)
error_code = 1;
else if (b == 10)
error_code = 2;
else if (c == 7)
error_code = 3;
else
error_code = call_function(a ,b,c);

For readability/maintainability reasons, I'd also add some braces
(if(...){}), but they aren't required.

--
David Hilsee
Jul 22 '05 #2
On Fri, 15 Oct 2004 16:05:55 -0700, Scott Brady Drummonds wrote:
Hi, everyeone,

I recently stumbled on some code that someone else wrote that I don't like.
However, I'm having trouble articulating what the bad quality of the
following code is. The unnecessary use of indentation and else statements
seems to be counter-intuitive to me. However, I'm hoping for an argument
that is more formal than my intuition.

<quote>
// Make a function call based on each parameter not meeting certain
conditions.
if (a == 3)
error_code = 1;
else
{
if (b == 10)
error_code = 2;
else {
if (c == 7)
error_code = 3;
else
error_code = call_function(a ,b,c);
}
}

return error_code;
</quote>

Personally, I find all of the else statements distracting. Is there
anything that you don't like about the organization of this simple code?
I don't see anything "wrong" with the code, but do we all understand the
programmer's intent here? If a is 3, then you don't even check any of the
others, and if a's not 3, then you check b, and so on.

I suppose he could been more concise:
if (a == 3)
error_code = 1; }
else if (b == 10)
error_code = 2;
else if (c == 7)
error_code = 3;
else
error_code = call_function(a ,b,c);


Cheers!
Rich

Jul 22 '05 #3
Scott Brady Drummonds wrote:
I recently stumbled on some code that someone else wrote that I don't like. However, I'm having trouble articulating what the bad quality of the
following code is. The unnecessary use of indentation and else statements
seems to be counter-intuitive to me. However, I'm hoping for an argument
that is more formal than my intuition.
Thank you for displaying greater than average sensitivity to low-quality
code.
<quote>
// Make a function call based on each parameter not meeting certain
conditions.
if (a == 3)
error_code = 1;
else
{
if (b == 10)
error_code = 2;
else {
if (c == 7)
error_code = 3;
else
error_code = call_function(a ,b,c);
}
}

return error_code;
</quote>

Personally, I find all of the else statements distracting.
If C++ supported an std::map that was slightly easier to initialize, we
could write something like this (represented in a language resembling Ruby):

map = { 3=>1, 10=>2, 7=>3 }
error_code = map.fetch(b, nil)
error_code = call_function(a ,b,c) if not error_code

However, in C++ the cost of setting up such a map may exceed the cost of the
chain of else statements.
Is there
anything that you don't like about the organization of this simple code?


Why does c only get checked if a and b fail? What happens if the programmer
tries to change c's behavior? Could a, b, & c live inside a polymorphic
object? If there were some other reason to abstract them, then eventually
you might get (in C++):

error_code = abc.convertErro rCode();

Then the results vary based on the derived type of abc. If the program
containing that code depends on many similar if statements, scattered here
and there, then maybe many of them would go away with a careful use of
polymorphism.

If not, take out some excess delimiters, to help the code look like a table:

if (a == 3) error_code = 1;
else if (b == 10) error_code = 2;
else if (c == 7) error_code = 3;
else
error_code = call_function(a ,b,c);

The cruft is still there, but (in a monospaced font) you can at least scan
the columns and instantly see what's going on.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #4
Phlip wrote:
If C++ supported an std::map that was slightly easier to initialize, we
could write something like this (represented in a language resembling Ruby):
map = { 3=>1, 10=>2, 7=>3 }
error_code = map.fetch(b, nil)
error_code = call_function(a ,b,c) if not error_code

However, in C++ the cost of setting up such a map may exceed the cost of the chain of else statements.


We would also introduce some extra stuff to fetch with a, b, or c, too!

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #5
Scott Brady Drummonds wrote:
I recently stumbled on some code that someone else wrote that I don't like.
However, I'm having trouble articulating
what the bad quality of the following code is.
The unnecessary use of indentation and else statements
seems to be counter-intuitive to me.
However, I'm hoping for an argument that is more formal than my intuition.

<quote>
// Make a function call based on each parameter not meeting certain
conditions.
if (a == 3)
error_code = 1;
else
{
if (b == 10)
error_code = 2;
else {
if (c == 7)
error_code = 3;
else
error_code = call_function(a ,b,c);
}
}

return error_code;
</quote>

Personally, I find all of the else statements distracting.
Is there anything that you don't like
about the organization of this simple code? cat f.cc

int call_function(i nt, int, int);
int f(int a, int b, int c) {

return ( 3 == a)? 1:
(10 == b)? 2:
( 7 == c)? 3: call_function(a , b, c);

}
Jul 22 '05 #6
E. Robert Tisdale wrote:
> cat f.cc

int call_function(i nt, int, int);
int f(int a, int b, int c) {

return ( 3 == a)? 1:
(10 == b)? 2:
( 7 == c)? 3: call_function(a , b, c);

}


Another quality of good code is how easily one can change it. Even if the
stack of else statements that we recommended were just as complex as this
chain of ternary operators, changing that statement seems higher risk.

OTOH proper respects for putting the constants on the left of the ==
operator - I forgot that one.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #7
In article <ck**********@n ews01.intel.com >,
"Scott Brady Drummonds" <sc************ **********@inte l.com> wrote:
Hi, everyeone,

I recently stumbled on some code that someone else wrote that I don't like.
However, I'm having trouble articulating what the bad quality of the
following code is. The unnecessary use of indentation and else statements
seems to be counter-intuitive to me. However, I'm hoping for an argument
that is more formal than my intuition.

<quote>
// Make a function call based on each parameter not meeting certain
conditions.
if (a == 3)
error_code = 1;
else
{
if (b == 10)
error_code = 2;
else {
if (c == 7)
error_code = 3;
else
error_code = call_function(a ,b,c);
}
}

return error_code;
</quote>

Personally, I find all of the else statements distracting. Is there
anything that you don't like about the organization of this simple code?


It implies that the 'if (a==3)' is a more important comparison that any
of the others, when in fact they are all on the same level...

if ( a == 3 )
error_code = 1;
else if ( b == 10 )
error_code = 2;
else if ( c == 7 )
error_code = 3;
else
error_code = call_function( a, b, c );

even this implies that the comparisons are more important than assigning
to error_code, which is also (probably) incorrect...

error_code = (a==3) ? 1: (b==10) ? 2: (c==7) ? 3: call_function(a , b, c);

Of course the use of the ?: operator may upset some people, they may
even consider this obfuscation; but it succeeds in making the most
important part of the code, the most obvious. Try reading each one out
loud and you will see that the last snippet is the most coherent.
Jul 22 '05 #8
On Fri, 15 Oct 2004 16:45:24 -0700, "E. Robert Tisdale"
<E.************ **@jpl.nasa.gov > wrote:
> cat f.cc

int call_function(i nt, int, int);
int f(int a, int b, int c) {

return ( 3 == a)? 1:
(10 == b)? 2:
( 7 == c)? 3: call_function(a , b, c);

}


If indeed we do simply return afterwards, you can say:

if (a == 3) then return 1;
if (b == 10) then return 2;
if (c == 7) then return 3;

return call_function(a , b, c);

Though I usually set up an error system and write stuff like:

my_runtime_asse rt(a <> 3, 1);
etc.

J.

Jul 22 '05 #9
On Sat, 16 Oct 2004 00:15:07 GMT, JXStern <JX************ **@gte.net>
wrote:
If indeed we do simply return afterwards, you can say:

if (a == 3) then return 1;
if (b == 10) then return 2;
if (c == 7) then return 3;

return call_function(a , b, c);
Maybe without the "then" will compile better.

J.

Though I usually set up an error system and write stuff like:

my_runtime_asse rt(a <> 3, 1);
etc.

J.


Jul 22 '05 #10

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

Similar topics

22
2692
by: Harold Crump | last post by:
Greetings, I have a PHP/MySQL application that I am deploying at a client's. I am fairly certain that they will steal my source code and re-sell to other companies. I would like to somehow protect the source code. Here are some of the options I have thought about.
0
1957
by: Rasmus Fogh | last post by:
Dear All, I need a way of writing strings or arbitrary Python code that will a) allow the strings to be read again unchanged (like repr) b) write multiline strings as multiline strings instead of escaping the \n's. A repr function that output triple-quoted strings with explicit (non-escaped) linebreaks would be perfect.
0
2444
by: Rasmus Fogh | last post by:
Someone raised the question of automatic code generation a few weeks back. And yes, we (CCPN) are using automatic Python code generation in a major way. Basically we are making data models in UML, and using automatic code generation to make Python APIs, XML I/O etc. (more below). We can be found at http://www.ccpn.ac.uk/index.html As a general point, automtic code generation would seem like a good idea in special cases where:
8
3954
by: Irmen de Jong | last post by:
What would be the best way, if any, to obtain the bytecode for a given loaded module? I can get the source: import inspect import os src = inspect.getsource(os) but there is no ispect.getbytecode() ;-)
5
1917
by: Sky Fly | last post by:
Hi, I know that when an .NET exe is run, the CLR loads the exe (along with dependent assemblies), compiles them to native code then runs the code. Assuming the assemblies are loaded from a remote inaccessible location, is it possible that during any of the stages of loading the exe into memory, a person with malicious intent could attach a debugger and serialise the exe and assemblies
242
13456
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any comments on past experience, research articles, comments on the matter would be much appreciated. I suspect something like C would be the best based on comments I received from the VB news group. Thanks for the help in advance James Cameron
3
6898
by: DPfan | last post by:
What's exactly the meaning of "code reuse" in C++? Why such kind of reuse have more advantages over the counterpart in other language like in C? How is "code reuse" realized in C++? By composition mainly? What're others? Thanks in advance for your comments!
1
2778
by: geek04 | last post by:
i'm using pro*c to precompile my c++ code which accesses oracle 9i database, i'm running a oracle 9i client on my system on compiling the c++ file (generated by pro*c)i'm getting following errors: E:\CODE\VariableRating.cpp(712) : error C2146: syntax error : missing ';' before identifier 'SQL' E:\CODE\VariableRating.cpp(712) : error C2501: 'EXEC' : missing storage-class or type specifiers
5
4069
by: ED | last post by:
I currently have vba code that ranks employees based on their average job time ordered by their region, zone, and job code. I currently have vba code that will cycle through a query and ranks each employee based on their region, zone, job code and avg job time. (See code below). My problem is that I do not know how to rank the ties. Right now if two people have the same avg time one will be ranked 3rd and the other ranked 4th. I would...
6
3125
by: Fuzzyman | last post by:
Hello all, I'm trying to extract the code object from a function, and exec it without explicitly passing parameters. The code object 'knows' it expects to receive paramaters. It's 'arg_count' attribute is readonly. How can I set the arg_count to 0, or pass parameters to the code object when I exec it ?
0
9424
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10051
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10000
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.