<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Notfound</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Notfound"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Notfound"/>
	<updated>2026-06-04T11:50:19Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_18_301&amp;diff=30066</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 18 301</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_18_301&amp;diff=30066"/>
		<updated>2009-11-25T05:24:41Z</updated>

		<summary type="html">&lt;p&gt;Notfound: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Topic: Stable Dependencies Principle&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Package Design is a challenging area of Object Oriented Programming (OOP), an important part of Software Engineering. An inefficiently designed system can be a root cause of many problems, thus, it is vital to have a solid understanding of various design approaches. This article discusses Stable Dependencies Principle, one of a few key package design principles that software engineers should be aware of.&lt;br /&gt;
&lt;br /&gt;
== Definition of Package==&lt;br /&gt;
&lt;br /&gt;
In OOP, a package is one or more classes logically grouped into a single unit. According to [4], &amp;quot;classes often have dependencies on other classes, creating package dependencies relationships.&amp;quot; The below principle can interchangeably be applied to small packages of just one class, and to larger ones consisting of multiple classes.&lt;br /&gt;
&lt;br /&gt;
== Definition of Instability ==&lt;br /&gt;
&lt;br /&gt;
First, let's discuss why certain packages are considered stable, and others are not. According to [2], the instability of any package can be calculated using the following formula:&lt;br /&gt;
&lt;br /&gt;
 I (instability) = Ce / ( Ca + Ce )&lt;br /&gt;
&lt;br /&gt;
In this formula:&lt;br /&gt;
&lt;br /&gt;
Ce – the number of other modules that our package requires in order to operate correctly. In other words, this is an indicator of package ''independence''.&lt;br /&gt;
&lt;br /&gt;
Ca – the number of packages that can be viewed as “dependents“, i.e., “depend upon classes within the package”. This is an indicator of package ''responsibility''.&lt;br /&gt;
&lt;br /&gt;
Therefore, in order for a package to be completely independent and also stable, it should not be dependent upon other packages.&lt;br /&gt;
&lt;br /&gt;
For example, let’s look at the following component diagram:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig1.png]] &lt;br /&gt;
&lt;br /&gt;
The ''Wheel'' class in this example is shown to be not dependent on any other module. &lt;br /&gt;
&lt;br /&gt;
Using the above formula: &lt;br /&gt;
&lt;br /&gt;
 I(W) = 0 / ( 3 + 0 ) = 0&lt;br /&gt;
&lt;br /&gt;
I(W) has the instability of zero. In other words, even though the ''Wheel'' class is “responsible” for three classes (Ca=3), it is not dependent upon any of them (Ce = 0), thus resulting in a stable package. &lt;br /&gt;
&lt;br /&gt;
Respectively, ''instable'' or ''irresponsible'' packages would have the instability of 1, as shown below:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig2.png]] &lt;br /&gt;
 &lt;br /&gt;
In this case, the ''Car'' class does not have any dependent classes (Ca = 0), yet itself depends upon three classes – ''Wheel'', ''Engine'', and ''Suspension'' (Ce = 3).&lt;br /&gt;
&lt;br /&gt;
Let’s calculate the instability of the ''Car'' package:&lt;br /&gt;
&lt;br /&gt;
 I(C) = 3 / ( 0 + 3 ) = 3 / 3 = 1&lt;br /&gt;
&lt;br /&gt;
The instability of 1 indicates a high level of volatility of the ''Car'' module.&lt;br /&gt;
&lt;br /&gt;
== Stable Dependencies Principle ==&lt;br /&gt;
&lt;br /&gt;
The ''Stable Dependencies Principle'' (SDP) states that “a package should always depend upon packages that are more stable than it is.” Ideally, the direction of dependencies should go from high volatile to more stable packages.&lt;br /&gt;
&lt;br /&gt;
So how do we know whether a certain component design follows the SDP? Let's look at another example. The following is a more complex diagram outlining several ''Car'' components and their sub-components:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig3.png]]&lt;br /&gt;
&lt;br /&gt;
The instability of each of the modules would be calculated as follows:&lt;br /&gt;
&lt;br /&gt;
 I(C) = 3 / (0 + 3) = 1 (the least stable)&lt;br /&gt;
 I(W) = 2 / (1 + 2) = 0.66&lt;br /&gt;
 I(E) = 2 / (1 + 2) = 0.66&lt;br /&gt;
 I(Su) = 1 / (1 + 1) = 0.5&lt;br /&gt;
 I(T) = 0 / (1 + 0) = 0 (the most stable)&lt;br /&gt;
 I(R) = 0 / (1 + 0) = 0 &lt;br /&gt;
 I(Cyl) = 0 / (1 + 0) = 0&lt;br /&gt;
 I(O) = 0 / (1 + 0) = 0&lt;br /&gt;
 I(St) = 0 / (1 + 0) = 0&lt;br /&gt;
&lt;br /&gt;
After replacing each class with its corresponding instability value, the diagram would look as shown below:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig4.png]]&lt;br /&gt;
&lt;br /&gt;
In general, lower values result in more stable packages. In our case, all dependencies progress in the direction of stability, thus, according to the SDP, our design is proven to be effective.&lt;br /&gt;
&lt;br /&gt;
Another important consideration when it comes to stability is that more stable packages should not be changed often. On the other hand, a highly volatile class can be changed easily, without affecting other packages in the system.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
To summarize, the SDP is one of several package design principles. It helps design &amp;quot;systems with a high degree of reusability and testability&amp;quot; [1]. The principle states that &amp;quot;a package should always depend upon packages that are more stable than it is,&amp;quot; and this article demonstrates methods to identify which packages are more stable than others.&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
[1] The Tipping Point: Stability and Instability in OO Design, http://www.ddj.com/architect/184415285&lt;br /&gt;
&lt;br /&gt;
[2] On Package Design, https://prof.hti.bfh.ch/fileadmin/home/due1/uml_dp/udp_package_design_200906.pdf&lt;br /&gt;
&lt;br /&gt;
[3] What Makes A Good Object-Oriented Design?, http://ootips.org/ood-principles.html&lt;br /&gt;
&lt;br /&gt;
[4] Principles of Package Design, http://nandokakimoto.wordpress.com/2009/05/15/principles-of-package-design/&lt;br /&gt;
&lt;br /&gt;
[5] Stability, http://www.objectmentor.com/resources/articles/stability.pdf&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_18_301&amp;diff=30065</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 18 301</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_18_301&amp;diff=30065"/>
		<updated>2009-11-25T05:24:35Z</updated>

		<summary type="html">&lt;p&gt;Notfound: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Topic: Stable Dependencies Principlex&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Package Design is a challenging area of Object Oriented Programming (OOP), an important part of Software Engineering. An inefficiently designed system can be a root cause of many problems, thus, it is vital to have a solid understanding of various design approaches. This article discusses Stable Dependencies Principle, one of a few key package design principles that software engineers should be aware of.&lt;br /&gt;
&lt;br /&gt;
== Definition of Package==&lt;br /&gt;
&lt;br /&gt;
In OOP, a package is one or more classes logically grouped into a single unit. According to [4], &amp;quot;classes often have dependencies on other classes, creating package dependencies relationships.&amp;quot; The below principle can interchangeably be applied to small packages of just one class, and to larger ones consisting of multiple classes.&lt;br /&gt;
&lt;br /&gt;
== Definition of Instability ==&lt;br /&gt;
&lt;br /&gt;
First, let's discuss why certain packages are considered stable, and others are not. According to [2], the instability of any package can be calculated using the following formula:&lt;br /&gt;
&lt;br /&gt;
 I (instability) = Ce / ( Ca + Ce )&lt;br /&gt;
&lt;br /&gt;
In this formula:&lt;br /&gt;
&lt;br /&gt;
Ce – the number of other modules that our package requires in order to operate correctly. In other words, this is an indicator of package ''independence''.&lt;br /&gt;
&lt;br /&gt;
Ca – the number of packages that can be viewed as “dependents“, i.e., “depend upon classes within the package”. This is an indicator of package ''responsibility''.&lt;br /&gt;
&lt;br /&gt;
Therefore, in order for a package to be completely independent and also stable, it should not be dependent upon other packages.&lt;br /&gt;
&lt;br /&gt;
For example, let’s look at the following component diagram:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig1.png]] &lt;br /&gt;
&lt;br /&gt;
The ''Wheel'' class in this example is shown to be not dependent on any other module. &lt;br /&gt;
&lt;br /&gt;
Using the above formula: &lt;br /&gt;
&lt;br /&gt;
 I(W) = 0 / ( 3 + 0 ) = 0&lt;br /&gt;
&lt;br /&gt;
I(W) has the instability of zero. In other words, even though the ''Wheel'' class is “responsible” for three classes (Ca=3), it is not dependent upon any of them (Ce = 0), thus resulting in a stable package. &lt;br /&gt;
&lt;br /&gt;
Respectively, ''instable'' or ''irresponsible'' packages would have the instability of 1, as shown below:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig2.png]] &lt;br /&gt;
 &lt;br /&gt;
In this case, the ''Car'' class does not have any dependent classes (Ca = 0), yet itself depends upon three classes – ''Wheel'', ''Engine'', and ''Suspension'' (Ce = 3).&lt;br /&gt;
&lt;br /&gt;
Let’s calculate the instability of the ''Car'' package:&lt;br /&gt;
&lt;br /&gt;
 I(C) = 3 / ( 0 + 3 ) = 3 / 3 = 1&lt;br /&gt;
&lt;br /&gt;
The instability of 1 indicates a high level of volatility of the ''Car'' module.&lt;br /&gt;
&lt;br /&gt;
== Stable Dependencies Principle ==&lt;br /&gt;
&lt;br /&gt;
The ''Stable Dependencies Principle'' (SDP) states that “a package should always depend upon packages that are more stable than it is.” Ideally, the direction of dependencies should go from high volatile to more stable packages.&lt;br /&gt;
&lt;br /&gt;
So how do we know whether a certain component design follows the SDP? Let's look at another example. The following is a more complex diagram outlining several ''Car'' components and their sub-components:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig3.png]]&lt;br /&gt;
&lt;br /&gt;
The instability of each of the modules would be calculated as follows:&lt;br /&gt;
&lt;br /&gt;
 I(C) = 3 / (0 + 3) = 1 (the least stable)&lt;br /&gt;
 I(W) = 2 / (1 + 2) = 0.66&lt;br /&gt;
 I(E) = 2 / (1 + 2) = 0.66&lt;br /&gt;
 I(Su) = 1 / (1 + 1) = 0.5&lt;br /&gt;
 I(T) = 0 / (1 + 0) = 0 (the most stable)&lt;br /&gt;
 I(R) = 0 / (1 + 0) = 0 &lt;br /&gt;
 I(Cyl) = 0 / (1 + 0) = 0&lt;br /&gt;
 I(O) = 0 / (1 + 0) = 0&lt;br /&gt;
 I(St) = 0 / (1 + 0) = 0&lt;br /&gt;
&lt;br /&gt;
After replacing each class with its corresponding instability value, the diagram would look as shown below:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig4.png]]&lt;br /&gt;
&lt;br /&gt;
In general, lower values result in more stable packages. In our case, all dependencies progress in the direction of stability, thus, according to the SDP, our design is proven to be effective.&lt;br /&gt;
&lt;br /&gt;
Another important consideration when it comes to stability is that more stable packages should not be changed often. On the other hand, a highly volatile class can be changed easily, without affecting other packages in the system.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
To summarize, the SDP is one of several package design principles. It helps design &amp;quot;systems with a high degree of reusability and testability&amp;quot; [1]. The principle states that &amp;quot;a package should always depend upon packages that are more stable than it is,&amp;quot; and this article demonstrates methods to identify which packages are more stable than others.&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
[1] The Tipping Point: Stability and Instability in OO Design, http://www.ddj.com/architect/184415285&lt;br /&gt;
&lt;br /&gt;
[2] On Package Design, https://prof.hti.bfh.ch/fileadmin/home/due1/uml_dp/udp_package_design_200906.pdf&lt;br /&gt;
&lt;br /&gt;
[3] What Makes A Good Object-Oriented Design?, http://ootips.org/ood-principles.html&lt;br /&gt;
&lt;br /&gt;
[4] Principles of Package Design, http://nandokakimoto.wordpress.com/2009/05/15/principles-of-package-design/&lt;br /&gt;
&lt;br /&gt;
[5] Stability, http://www.objectmentor.com/resources/articles/stability.pdf&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_18_301&amp;diff=30062</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 18 301</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_18_301&amp;diff=30062"/>
		<updated>2009-11-25T05:23:20Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Topic: Stable Dependencies Principle&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Package Design is a challenging area of Object Oriented Programming (OOP), an important part of Software Engineering. An inefficiently designed system can be a root cause of many problems, thus, it is vital to have a solid understanding of various design approaches. This article discusses Stable Dependencies Principle, one of a few key package design principles that software engineers should be aware of.&lt;br /&gt;
&lt;br /&gt;
== Definition of Package==&lt;br /&gt;
&lt;br /&gt;
In OOP, a package is one or more classes logically grouped into a single unit. According to [4], &amp;quot;classes often have dependencies on other classes, creating package dependencies relationships.&amp;quot; The below principle can interchangeably be applied to small packages of just one class, and to larger ones consisting of multiple classes.&lt;br /&gt;
&lt;br /&gt;
== Definition of Instability ==&lt;br /&gt;
&lt;br /&gt;
First, let's discuss why certain packages are considered stable, and others are not. According to [2], the instability of any package can be calculated using the following formula:&lt;br /&gt;
&lt;br /&gt;
 I (instability) = Ce / ( Ca + Ce )&lt;br /&gt;
&lt;br /&gt;
In this formula:&lt;br /&gt;
&lt;br /&gt;
Ce – the number of other modules that our package requires in order to operate correctly. In other words, this is an indicator of package ''independence''.&lt;br /&gt;
&lt;br /&gt;
Ca – the number of packages that can be viewed as “dependents“, i.e., “depend upon classes within the package”. This is an indicator of package ''responsibility''.&lt;br /&gt;
&lt;br /&gt;
Therefore, in order for a package to be completely independent and also stable, it should not be dependent upon other packages.&lt;br /&gt;
&lt;br /&gt;
For example, let’s look at the following component diagram:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig1.png]] &lt;br /&gt;
&lt;br /&gt;
The ''Wheel'' class in this example is shown to be not dependent on any other module. &lt;br /&gt;
&lt;br /&gt;
Using the above formula: &lt;br /&gt;
&lt;br /&gt;
 I(W) = 0 / ( 3 + 0 ) = 0&lt;br /&gt;
&lt;br /&gt;
I(W) has the instability of zero. In other words, even though the ''Wheel'' class is “responsible” for three classes (Ca=3), it is not dependent upon any of them (Ce = 0), thus resulting in a stable package. &lt;br /&gt;
&lt;br /&gt;
Respectively, ''instable'' or ''irresponsible'' packages would have the instability of 1, as shown below:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig2.png]] &lt;br /&gt;
 &lt;br /&gt;
In this case, the ''Car'' class does not have any dependent classes (Ca = 0), yet itself depends upon three classes – ''Wheel'', ''Engine'', and ''Suspension'' (Ce = 3).&lt;br /&gt;
&lt;br /&gt;
Let’s calculate the instability of the ''Car'' package:&lt;br /&gt;
&lt;br /&gt;
 I(C) = 3 / ( 0 + 3 ) = 3 / 3 = 1&lt;br /&gt;
&lt;br /&gt;
The instability of 1 indicates a high level of volatility of the ''Car'' module.&lt;br /&gt;
&lt;br /&gt;
== Stable Dependencies Principle ==&lt;br /&gt;
&lt;br /&gt;
The ''Stable Dependencies Principle'' (SDP) states that “a package should always depend upon packages that are more stable than it is.” Ideally, the direction of dependencies should go from high volatile to more stable packages.&lt;br /&gt;
&lt;br /&gt;
So how do we know whether a certain component design follows the SDP? Let's look at another example. The following is a more complex diagram outlining several ''Car'' components and their sub-components:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig3.png]]&lt;br /&gt;
&lt;br /&gt;
The instability of each of the modules would be calculated as follows:&lt;br /&gt;
&lt;br /&gt;
 I(C) = 3 / (0 + 3) = 1 (the least stable)&lt;br /&gt;
 I(W) = 2 / (1 + 2) = 0.66&lt;br /&gt;
 I(E) = 2 / (1 + 2) = 0.66&lt;br /&gt;
 I(Su) = 1 / (1 + 1) = 0.5&lt;br /&gt;
 I(T) = 0 / (1 + 0) = 0 (the most stable)&lt;br /&gt;
 I(R) = 0 / (1 + 0) = 0 &lt;br /&gt;
 I(Cyl) = 0 / (1 + 0) = 0&lt;br /&gt;
 I(O) = 0 / (1 + 0) = 0&lt;br /&gt;
 I(St) = 0 / (1 + 0) = 0&lt;br /&gt;
&lt;br /&gt;
After replacing each class with its corresponding instability value, the diagram would look as shown below:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig4.png]]&lt;br /&gt;
&lt;br /&gt;
In general, lower values result in more stable packages. In our case, all dependencies progress in the direction of stability, thus, according to the SDP, our design is proven to be effective.&lt;br /&gt;
&lt;br /&gt;
Another important consideration when it comes to stability is that more stable packages should not be changed often. On the other hand, a highly volatile class can be changed easily, without affecting other packages in the system.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
To summarize, the SDP is one of several package design principles. It helps design &amp;quot;systems with a high degree of reusability and testability&amp;quot; [1]. The principle states that &amp;quot;a package should always depend upon packages that are more stable than it is,&amp;quot; and this article demonstrates methods to identify which packages are more stable than others.&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
[1] The Tipping Point: Stability and Instability in OO Design, http://www.ddj.com/architect/184415285&lt;br /&gt;
&lt;br /&gt;
[2] On Package Design, https://prof.hti.bfh.ch/fileadmin/home/due1/uml_dp/udp_package_design_200906.pdf&lt;br /&gt;
&lt;br /&gt;
[3] What Makes A Good Object-Oriented Design?, http://ootips.org/ood-principles.html&lt;br /&gt;
&lt;br /&gt;
[4] Principles of Package Design, http://nandokakimoto.wordpress.com/2009/05/15/principles-of-package-design/&lt;br /&gt;
&lt;br /&gt;
[5] Stability, http://www.objectmentor.com/resources/articles/stability.pdf&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_18_301&amp;diff=30061</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 18 301</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_18_301&amp;diff=30061"/>
		<updated>2009-11-25T05:22:54Z</updated>

		<summary type="html">&lt;p&gt;Notfound: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Topic: Stable Dependencies Principle&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Package Design is a challenging area of Object Oriented Programming (OOP), an important part of Software Engineering. An inefficiently designed system can be a root cause of many problems, thus, it is vital to have a solid understanding of various design approaches. This article discusses Stable Dependencies Principle, one of a few key package design principles that software engineers should be aware of.&lt;br /&gt;
&lt;br /&gt;
== Definition of Package==&lt;br /&gt;
&lt;br /&gt;
In OOP, a package is one or more classes logically grouped into a single unit. According to [4], &amp;quot;classes often have dependencies on other classes, creating package dependencies relationships.&amp;quot; The below principle can interchangeably be applied to small packages of just one class, and to larger ones consisting of multiple classes.&lt;br /&gt;
&lt;br /&gt;
== Definition of Instability ==&lt;br /&gt;
&lt;br /&gt;
First, let's discuss why certain packages are considered stable, and others are not. According to [2], the instability of any package can be calculated using the following formula:&lt;br /&gt;
&lt;br /&gt;
 I (instability) = Ce / ( Ca + Ce )&lt;br /&gt;
&lt;br /&gt;
In this formula:&lt;br /&gt;
&lt;br /&gt;
Ce – the number of other modules that our package requires in order to operate correctly. In other words, this is an indicator of package ''independence''.&lt;br /&gt;
&lt;br /&gt;
Ca – the number of packages that can be viewed as “dependents“, i.e., “depend upon classes within the package”. This is an indicator of package ''responsibility''.&lt;br /&gt;
&lt;br /&gt;
Therefore, in order for a package to be completely independent and also stable, it should not be dependent upon other packages.&lt;br /&gt;
&lt;br /&gt;
For example, let’s look at the following component diagram:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig1.png]] &lt;br /&gt;
&lt;br /&gt;
The ''Wheel'' class in this example is shown to be not dependent on any other module. &lt;br /&gt;
&lt;br /&gt;
Using the above formula: &lt;br /&gt;
&lt;br /&gt;
 I(W) = 0 / ( 3 + 0 ) = 0&lt;br /&gt;
&lt;br /&gt;
I(W) has the instability of zero. In other words, even though the ''Wheel'' class is “responsible” for three classes (Ca=3), it is not dependent upon any of them (Ce = 0), thus resulting in a stable package. &lt;br /&gt;
&lt;br /&gt;
Respectively, ''instable'' or ''irresponsible'' packages would have the instability of 1, as shown below:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig2.png]] &lt;br /&gt;
 &lt;br /&gt;
In this case, the ''Car'' class does not have any dependent classes (Ca = 0), yet itself depends upon three classes – ''Wheel'', ''Engine'', and ''Suspension'' (Ce = 3).&lt;br /&gt;
&lt;br /&gt;
Let’s calculate the instability of the ''Car'' package:&lt;br /&gt;
&lt;br /&gt;
 I(C) = 3 / ( 0 + 3 ) = 3 / 3 = 1&lt;br /&gt;
&lt;br /&gt;
The instability of 1 indicates a high level of volatility of the ''Car'' module.&lt;br /&gt;
&lt;br /&gt;
== Stable Dependencies Principle ==&lt;br /&gt;
&lt;br /&gt;
The ''Stable Dependencies Principle'' (SDP) states that “a package should always depend upon packages that are more stable than it is.” Ideally, the direction of dependencies should go from high volatile to more stable packages.&lt;br /&gt;
&lt;br /&gt;
So how do we know whether a certain component design follows the SDP? Let's look at another example. The following is a more complex diagram outlining several ''Car'' components and their sub-components:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig3.png]]&lt;br /&gt;
&lt;br /&gt;
The instability of each of the modules would be calculated as follows:&lt;br /&gt;
&lt;br /&gt;
 I(C) = 3 / (0 + 3) = 1 (the least stable)&lt;br /&gt;
 I(W) = 2 / (1 + 2) = 0.66&lt;br /&gt;
 I(E) = 2 / (1 + 2) = 0.66&lt;br /&gt;
 I(Su) = 1 / (1 + 1) = 0.5&lt;br /&gt;
 I(T) = 0 / (1 + 0) = 0 (the most stable)&lt;br /&gt;
 I(R) = 0 / (1 + 0) = 0 &lt;br /&gt;
 I(Cyl) = 0 / (1 + 0) = 0&lt;br /&gt;
 I(O) = 0 / (1 + 0) = 0&lt;br /&gt;
 I(St) = 0 / (1 + 0) = 0&lt;br /&gt;
&lt;br /&gt;
After replacing each class with its corresponding instability value, the diagram would look as shown below:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig4.png]]&lt;br /&gt;
&lt;br /&gt;
In general, lower values result in more stable packages. In our case, all dependencies progress in the direction of stability, thus, according to the SDP, our design is proven to be effective.&lt;br /&gt;
&lt;br /&gt;
Another important consideration when it comes to stability is that more stable packages should not be changed often. On the other hand, a highly volatile class can be changed easily, without affecting other packages in the system.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
To summarize, the SDP is one of several package design principles. It helps design &amp;quot;systems with a high degree of reusability and testability.&amp;quot; [1]. The principle states that &amp;quot;a package should always depend upon packages that are more stable than it is,&amp;quot; and this article demonstrates methods to identify which packages are more stable than others.&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
[1] The Tipping Point: Stability and Instability in OO Design, http://www.ddj.com/architect/184415285&lt;br /&gt;
&lt;br /&gt;
[2] On Package Design, https://prof.hti.bfh.ch/fileadmin/home/due1/uml_dp/udp_package_design_200906.pdf&lt;br /&gt;
&lt;br /&gt;
[3] What Makes A Good Object-Oriented Design?, http://ootips.org/ood-principles.html&lt;br /&gt;
&lt;br /&gt;
[4] Principles of Package Design, http://nandokakimoto.wordpress.com/2009/05/15/principles-of-package-design/&lt;br /&gt;
&lt;br /&gt;
[5] Stability, http://www.objectmentor.com/resources/articles/stability.pdf&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_18_301&amp;diff=30060</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 18 301</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_18_301&amp;diff=30060"/>
		<updated>2009-11-25T05:20:40Z</updated>

		<summary type="html">&lt;p&gt;Notfound: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Topic: Stable Dependencies Principle&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Package Design is a challenging area of Object Oriented Programming (OOP), an important part of Software Engineering. An inefficiently designed system can be a root cause of many problems, thus, it is vital to have a solid understanding of various design approaches. This article discusses Stable Dependencies Principle, one of a few key package design principles that software engineers should be aware of.&lt;br /&gt;
&lt;br /&gt;
== Definition of Package==&lt;br /&gt;
&lt;br /&gt;
In OOP, a package is one or more classes logically grouped into a single unit. According to [4], &amp;quot;classes often have dependencies on other classes, creating package dependencies relationships.&amp;quot; The below principle can interchangeably be applied to small packages of just one class, and to larger ones consisting of multiple classes.&lt;br /&gt;
&lt;br /&gt;
== Definition of Instability ==&lt;br /&gt;
&lt;br /&gt;
First, let's discuss why certain packages are considered stable, and others are not. The instability of any package can be calculated using the following formula:&lt;br /&gt;
&lt;br /&gt;
 I (instability) = Ce / ( Ca + Ce )&lt;br /&gt;
&lt;br /&gt;
In this formula:&lt;br /&gt;
&lt;br /&gt;
Ce – the number of other modules that our package requires in order to operate correctly. In other words, this is an indicator of package ''independence''.&lt;br /&gt;
&lt;br /&gt;
Ca – the number of packages that can be viewed as “dependents“, i.e., “depend upon classes within the package”. This is an indicator of package ''responsibility''.&lt;br /&gt;
&lt;br /&gt;
Therefore, in order for a package to be completely independent and also stable, it should not be dependent upon other packages.&lt;br /&gt;
&lt;br /&gt;
For example, let’s look at the following component diagram:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig1.png]] &lt;br /&gt;
&lt;br /&gt;
The ''Wheel'' class in this example is shown to be not dependent on any other module. &lt;br /&gt;
&lt;br /&gt;
Using the above formula: &lt;br /&gt;
&lt;br /&gt;
 I(W) = 0 / ( 3 + 0 ) = 0&lt;br /&gt;
&lt;br /&gt;
I(W) has the instability of zero. In other words, even though the ''Wheel'' class is “responsible” for three classes (Ca=3), it is not dependent upon any of them (Ce = 0), thus resulting in a stable package. &lt;br /&gt;
&lt;br /&gt;
Respectively, ''instable'' or ''irresponsible'' packages would have the instability of 1, as shown below:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig2.png]] &lt;br /&gt;
 &lt;br /&gt;
In this case, the ''Car'' class does not have any dependent classes (Ca = 0), yet itself depends upon three classes – ''Wheel'', ''Engine'', and ''Suspension'' (Ce = 3).&lt;br /&gt;
&lt;br /&gt;
Let’s calculate the instability of the ''Car'' package:&lt;br /&gt;
&lt;br /&gt;
 I(C) = 3 / ( 0 + 3 ) = 3 / 3 = 1&lt;br /&gt;
&lt;br /&gt;
The instability of 1 indicates a high level of volatility of the ''Car'' module.&lt;br /&gt;
&lt;br /&gt;
== Stable Dependencies Principle ==&lt;br /&gt;
&lt;br /&gt;
The ''Stable Dependencies Principle'' (SDP) states that “a package should always depend upon packages that are more stable than it is.” Ideally, the direction of dependencies should go from high volatile to more stable packages.&lt;br /&gt;
&lt;br /&gt;
So how do we know whether a certain component design follows the SDP? Let's look at another example. The following is a more complex diagram outlining several ''Car'' components and their sub-components:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig3.png]]&lt;br /&gt;
&lt;br /&gt;
The instability of each of the modules would be calculated as follows:&lt;br /&gt;
&lt;br /&gt;
 I(C) = 3 / (0 + 3) = 1 (the least stable)&lt;br /&gt;
 I(W) = 2 / (1 + 2) = 0.66&lt;br /&gt;
 I(E) = 2 / (1 + 2) = 0.66&lt;br /&gt;
 I(Su) = 1 / (1 + 1) = 0.5&lt;br /&gt;
 I(T) = 0 / (1 + 0) = 0 (the most stable)&lt;br /&gt;
 I(R) = 0 / (1 + 0) = 0 &lt;br /&gt;
 I(Cyl) = 0 / (1 + 0) = 0&lt;br /&gt;
 I(O) = 0 / (1 + 0) = 0&lt;br /&gt;
 I(St) = 0 / (1 + 0) = 0&lt;br /&gt;
&lt;br /&gt;
After replacing each class with its corresponding instability value, the diagram would look as shown below:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig4.png]]&lt;br /&gt;
&lt;br /&gt;
In general, lower values result in more stable packages. In our case, all dependencies progress in the direction of stability, thus, according to the SDP, our design is proven to be effective.&lt;br /&gt;
&lt;br /&gt;
Another important consideration when it comes to stability is that more stable packages should not be changed often. On the other hand, a highly volatile class can be changed easily, without affecting other packages in the system.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
To summarize, the SDP is one of several package design principles. It helps design &amp;quot;systems with a high degree of reusability and testability.&amp;quot; [1]. The principle states that &amp;quot;a package should always depend upon packages that are more stable than it is,&amp;quot; and this article demonstrates methods to identify which packages are more stable than others.&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
[1] The Tipping Point: Stability and Instability in OO Design, http://www.ddj.com/architect/184415285&lt;br /&gt;
&lt;br /&gt;
[2] On Package Design, https://prof.hti.bfh.ch/fileadmin/home/due1/uml_dp/udp_package_design_200906.pdf&lt;br /&gt;
&lt;br /&gt;
[3] What Makes A Good Object-Oriented Design?, http://ootips.org/ood-principles.html&lt;br /&gt;
&lt;br /&gt;
[4] Principles of Package Design, http://nandokakimoto.wordpress.com/2009/05/15/principles-of-package-design/&lt;br /&gt;
&lt;br /&gt;
[5] Stability, http://www.objectmentor.com/resources/articles/stability.pdf&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_18_301&amp;diff=29858</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 18 301</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_18_301&amp;diff=29858"/>
		<updated>2009-11-23T01:16:41Z</updated>

		<summary type="html">&lt;p&gt;Notfound: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Topic: Stable Dependencies Principle&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Package Design is a challenging area of Object Oriented Programming (OOP), an important part of Software Engineering. An inefficiently designed system can be a root cause of many problems, thus, it is vital to have a solid understanding of various design approaches. This article discusses Stable Dependencies Principle, one of a few key package design principles that software engineers should be aware of.&lt;br /&gt;
&lt;br /&gt;
== Definition of Package==&lt;br /&gt;
&lt;br /&gt;
In OOP, a package is one or more classes logically grouped into a single unit. According to [4], &amp;quot;classes often have dependencies on other classes, creating package dependencies relationships.&amp;quot; The below principle can interchangeably be applied to small packages of just one class, and to larger ones consisting of multiple classes.&lt;br /&gt;
&lt;br /&gt;
== Definition of Instability ==&lt;br /&gt;
&lt;br /&gt;
First, let's discuss why certain packages are considered stable, and others are not. The instability of any package can be calculated using the following formula:&lt;br /&gt;
&lt;br /&gt;
 I (instability) = Ce / ( Ca + Ce )&lt;br /&gt;
&lt;br /&gt;
In this formula:&lt;br /&gt;
&lt;br /&gt;
Ce – the number of other modules that our package requires in order to operate correctly. In other words, this is an indicator of package ''independence''.&lt;br /&gt;
&lt;br /&gt;
Ca – the number of packages that can be viewed as “dependents“, i.e., “depend upon classes within the package”. This is an indicator of package ''responsibility''.&lt;br /&gt;
&lt;br /&gt;
Therefore, in order for a package to be completely independent and also stable, it should not be dependent upon other packages.&lt;br /&gt;
&lt;br /&gt;
For example, let’s look at the following component diagram:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig1.png]] &lt;br /&gt;
&lt;br /&gt;
The ''Wheel'' class in this example is shown to be not dependent on any other module. &lt;br /&gt;
&lt;br /&gt;
Using the above formula: &lt;br /&gt;
&lt;br /&gt;
 I(W) = 0 / ( 3 + 0 ) = 0&lt;br /&gt;
&lt;br /&gt;
I(W) has the instability of zero. In other words, even though the ''Wheel'' class is “responsible” for three classes (Ca=3), it is not dependent upon any of them (Ce = 0), thus resulting in a stable package. &lt;br /&gt;
&lt;br /&gt;
Respectively, ''instable'' or ''irresponsible'' packages would have the instability of 1, as shown below:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig2.png]] &lt;br /&gt;
 &lt;br /&gt;
In this case, the ''Car'' class does not have any dependent classes (Ca = 0), yet itself depends upon three classes – ''Wheel'', ''Engine'', and ''Suspension'' (Ce = 3).&lt;br /&gt;
&lt;br /&gt;
Let’s calculate the instability of the ''Car'' package:&lt;br /&gt;
&lt;br /&gt;
 I(C) = 3 / ( 0 + 3 ) = 3 / 3 = 1&lt;br /&gt;
&lt;br /&gt;
The instability of 1 indicates a high level of volatility of the ''Car'' module.&lt;br /&gt;
&lt;br /&gt;
== Stable Dependencies Principle ==&lt;br /&gt;
&lt;br /&gt;
The ''Stable Dependencies Principle'' (SDP) states that “a package should always depend upon packages that are more stable than it is.” Ideally, the direction of dependencies should go from high volatile to more stable packages.&lt;br /&gt;
&lt;br /&gt;
So how do we know whether a certain component design follows the SDP? Let's look at another example. The following is a more complex diagram outlining several ''Car'' components and their sub-components:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig3.png]]&lt;br /&gt;
&lt;br /&gt;
The instability of each of the modules would be calculated as follows:&lt;br /&gt;
&lt;br /&gt;
 I(C) = 3 / (0 + 3) = 1 (the least stable)&lt;br /&gt;
 I(W) = 2 / (1 + 2) = 0.66&lt;br /&gt;
 I(E) = 2 / (1 + 2) = 0.66&lt;br /&gt;
 I(Su) = 1 / (1 + 1) = 0.5&lt;br /&gt;
 I(T) = 0 / (1 + 0) = 0 (the most stable)&lt;br /&gt;
 I(R) = 0 / (1 + 0) = 0 &lt;br /&gt;
 I(Cyl) = 0 / (1 + 0) = 0&lt;br /&gt;
 I(O) = 0 / (1 + 0) = 0&lt;br /&gt;
 I(St) = 0 / (1 + 0) = 0&lt;br /&gt;
&lt;br /&gt;
After replacing each class with its corresponding instability value, the diagram would look as shown below:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig4.png]]&lt;br /&gt;
&lt;br /&gt;
In general, lower values result in more stable packages. In our case, all dependencies progress in the direction of stability, thus, according to the SDP, our design is proven to be effective.&lt;br /&gt;
&lt;br /&gt;
Another important consideration when it comes to stability is that more stable packages should not be changed often. On the other hand, a highly volatile class can be changed easily, without affecting other packages in the system.&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
[1] The Tipping Point: Stability and Instability in OO Design, http://www.ddj.com/architect/184415285&lt;br /&gt;
&lt;br /&gt;
[2] On Package Design, https://prof.hti.bfh.ch/fileadmin/home/due1/uml_dp/udp_package_design_200906.pdf&lt;br /&gt;
&lt;br /&gt;
[3] What Makes A Good Object-Oriented Design?, http://ootips.org/ood-principles.html&lt;br /&gt;
&lt;br /&gt;
[4] Principles of Package Design, http://nandokakimoto.wordpress.com/2009/05/15/principles-of-package-design/&lt;br /&gt;
&lt;br /&gt;
[5] Stability, http://www.objectmentor.com/resources/articles/stability.pdf&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_18_301&amp;diff=29792</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 18 301</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_18_301&amp;diff=29792"/>
		<updated>2009-11-20T21:06:41Z</updated>

		<summary type="html">&lt;p&gt;Notfound: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Topic: Stable Dependencies Principle&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Package Design is a challenging area of Object Oriented Programming (OOP), an important part of Software Engineering. An inefficiently designed system can be a root cause of many problems, thus, it is vital to have a solid understanding of various design approaches. This article discusses Stable Dependencies Principle, one of a few key package design principles that software engineers should be aware of.&lt;br /&gt;
&lt;br /&gt;
== Definition of Package==&lt;br /&gt;
&lt;br /&gt;
In OOP, a package is one or more classes logically grouped into a single unit. According to [4], &amp;quot;classes often have dependencies on other classes, creating package dependencies relationships.&amp;quot; The below principle can interchangeably be applied to small packages of just one class, and to larger ones consisting of multiple classes.&lt;br /&gt;
&lt;br /&gt;
== Definition of Instability ==&lt;br /&gt;
&lt;br /&gt;
First, let's discuss why certain packages are considered stable, and others are not. The instability of any package can be calculated using the following formula:&lt;br /&gt;
&lt;br /&gt;
 I (instability) = Ce / ( Ca + Ce )&lt;br /&gt;
&lt;br /&gt;
In this formula:&lt;br /&gt;
&lt;br /&gt;
Ce – the number of other modules that our package requires in order to operate correctly. In other words, this is an indicator of package ''independence''.&lt;br /&gt;
&lt;br /&gt;
Ca – the number of packages that can be viewed as “dependents“, i.e., “depend upon classes within the package”. This is an indicator of package ''responsibility''.&lt;br /&gt;
&lt;br /&gt;
Therefore, in order for a package to be completely independent and also stable, it should not be dependent upon other packages.&lt;br /&gt;
&lt;br /&gt;
For example, let’s look at the following component diagram:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig1.png]] &lt;br /&gt;
&lt;br /&gt;
The ''Wheel'' class in this example is shown to be not dependent on any other module. &lt;br /&gt;
&lt;br /&gt;
Using the above formula: &lt;br /&gt;
&lt;br /&gt;
 I(W) = 0 / ( 3 + 0 ) = 0&lt;br /&gt;
&lt;br /&gt;
I(W) has the instability of zero. In other words, even though the ''Wheel'' class is “responsible” for three classes (Ca=3), it is not dependent upon any of them (Ce = 0), thus resulting in a stable package. &lt;br /&gt;
&lt;br /&gt;
Respectively, ''instable'' or ''irresponsible'' packages would have the instability of 1, as shown below:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig2.png]] &lt;br /&gt;
 &lt;br /&gt;
In this case, the ''Car'' class does not have any dependent classes (Ca = 0), yet itself depends upon three classes – ''Wheel'', ''Engine'', and ''Suspension'' (Ce = 3).&lt;br /&gt;
&lt;br /&gt;
Let’s calculate the instability of the ''Car'' package:&lt;br /&gt;
&lt;br /&gt;
 I(C) = 3 / ( 0 + 3 ) = 3 / 3 = 1&lt;br /&gt;
&lt;br /&gt;
The instability of 1 indicates a high level of volatility of the ''Car'' module.&lt;br /&gt;
&lt;br /&gt;
== Stable Dependencies Principle ==&lt;br /&gt;
&lt;br /&gt;
The ''Stable Dependencies Principle'' (SDP) states that “a package should always depend upon packages that are more stable than it is.” Ideally, the direction of dependencies should go from high volatile to more stable packages.&lt;br /&gt;
&lt;br /&gt;
So how do we know whether a certain component design follows the SDP? Let's look at another example. The following is a more complex diagram outlining several ''Car'' components and their sub-components:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig3.png]]&lt;br /&gt;
&lt;br /&gt;
The instability of each of the modules would be calculated as follows:&lt;br /&gt;
&lt;br /&gt;
 I(C) = 3 / (0 + 3) = 1 (the least stable)&lt;br /&gt;
 I(W) = 2 / (1 + 2) = 0.66&lt;br /&gt;
 I(E) = 2 / (1 + 2) = 0.66&lt;br /&gt;
 I(Su) = 1 / (1 + 1) = 0.5&lt;br /&gt;
 I(T) = 0 / (1 + 0) = 0 (the most stable)&lt;br /&gt;
 I(R) = 0 / (1 + 0) = 0 &lt;br /&gt;
 I(Cyl) = 0 / (1 + 0) = 0&lt;br /&gt;
 I(O) = 0 / (1 + 0) = 0&lt;br /&gt;
 I(St) = 0 / (1 + 0) = 0&lt;br /&gt;
&lt;br /&gt;
After replacing each class with its corresponding instability value, the diagram would look as follows:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig4.png]]&lt;br /&gt;
&lt;br /&gt;
The lower the value, the more stable the package is considered to be. In our case, all dependencies progress in the direction of stability, thus, according to the SDP, our design is proven to effective.&lt;br /&gt;
&lt;br /&gt;
Another important consideration when it comes to stability is that more stable packages should not be changed often. On the other hand, a highly volatile class can be changed easily, without affecting other packages in the system.&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
[1] The Tipping Point: Stability and Instability in OO Design, http://www.ddj.com/architect/184415285&lt;br /&gt;
&lt;br /&gt;
[2] On Package Design, https://prof.hti.bfh.ch/fileadmin/home/due1/uml_dp/udp_package_design_200906.pdf&lt;br /&gt;
&lt;br /&gt;
[3] What Makes A Good Object-Oriented Design?, http://ootips.org/ood-principles.html&lt;br /&gt;
&lt;br /&gt;
[4] Principles of Package Design, http://nandokakimoto.wordpress.com/2009/05/15/principles-of-package-design/&lt;br /&gt;
&lt;br /&gt;
[5] Stability, http://www.objectmentor.com/resources/articles/stability.pdf&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_18_301&amp;diff=29791</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 18 301</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_18_301&amp;diff=29791"/>
		<updated>2009-11-20T21:06:33Z</updated>

		<summary type="html">&lt;p&gt;Notfound: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Topic: Stable Dependencies Principle&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Package Design is a challenging area of Object Oriented Programming (OOP), an important part of Software Engineering. An inefficiently designed system can be a root cause of many problems, thus, it is vital to have a solid understanding of various design approaches. This article discusses Stable Dependencies Principle, one of a few key package design principles that software engineers should be aware of.&lt;br /&gt;
&lt;br /&gt;
== Definition of Package==&lt;br /&gt;
&lt;br /&gt;
In OOP, a package is one or more classes logically grouped into a single unit. According to [4], &amp;quot;classes often have dependencies on other classes, creating package dependencies relationships.&amp;quot; The below principle can interchangeably be applied to small packages of just one class, and to larger ones consisting of multiple classes.&lt;br /&gt;
&lt;br /&gt;
== Definition of Instability ==&lt;br /&gt;
&lt;br /&gt;
First, let's discuss why certain packages are considered stable, and others are not. The instability of any package can be calculated using the following formula:&lt;br /&gt;
&lt;br /&gt;
 I (instability) = Ce / ( Ca + Ce )&lt;br /&gt;
&lt;br /&gt;
In this formula:&lt;br /&gt;
&lt;br /&gt;
Ce – the number of other modules that our package requires in order to operate correctly. In other words, this is an indicator of package ''independence''.&lt;br /&gt;
&lt;br /&gt;
Ca – the number of packages that can be viewed as “dependents“, i.e., “depend upon classes within the package”. This is an indicator of package ''responsibility''.&lt;br /&gt;
&lt;br /&gt;
Therefore, in order for a package to be completely independent and also stable, it should not be dependent upon other packages.&lt;br /&gt;
&lt;br /&gt;
For example, let’s look at the following component diagram:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig1.png]] &lt;br /&gt;
&lt;br /&gt;
The ''Wheel'' class in this example is shown to be not dependent on any other module. &lt;br /&gt;
&lt;br /&gt;
Using the above formula: &lt;br /&gt;
&lt;br /&gt;
 I(W) = 0 / ( 3 + 0 ) = 0&lt;br /&gt;
&lt;br /&gt;
I(W) has the instability of zero. In other words, even though the ''Wheel'' class is “responsible” for three classes (Ca=3), it is not dependent upon any of them (Ce = 0), thus resulting in a stable package. &lt;br /&gt;
&lt;br /&gt;
Respectively, ''instable'' or ''irresponsible'' packages would have the instability of 1, as shown below:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig2.png]] &lt;br /&gt;
 &lt;br /&gt;
In this case, the ''Car'' class does not have any dependent classes (Ca = 0), yet itself depends upon three classes – ''Wheel'', ''Engine'', and ''Suspension'' (Ce = 3).&lt;br /&gt;
&lt;br /&gt;
Let’s calculate the instability of the ''Car'' package:&lt;br /&gt;
&lt;br /&gt;
 I(C) = 3 / ( 0 + 3 ) = 3 / 3 = 1&lt;br /&gt;
&lt;br /&gt;
The instability of 1 indicates a high level of volatility of the ''Car'' module.&lt;br /&gt;
&lt;br /&gt;
== Stable Dependencies Principle ==&lt;br /&gt;
&lt;br /&gt;
The ''Stable Dependencies Principle'' (SDP) states that “a package should always depend upon packages that are more stable than it is.” Ideally, the direction of dependencies should go from high volatile to more stable packages.&lt;br /&gt;
&lt;br /&gt;
So how do we know whether a certain component design follows the SDP? Let's look at another example. The following is a more complex diagram outlining several ''Car'' components and their sub-components:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig3.png]]&lt;br /&gt;
&lt;br /&gt;
The instability of each of the modules would be calculated as follows:&lt;br /&gt;
&lt;br /&gt;
 I(C) = 3 / (0 + 3) = 1 (the least stable)&lt;br /&gt;
 I(W) = 2 / (1 + 2) = 0.66&lt;br /&gt;
 I(E) = 2 / (1 + 2) = 0.66&lt;br /&gt;
 I(Su) = 1 / (1 + 1) = 0.5&lt;br /&gt;
 I(T) = 0 / (1 + 0) = 0 (the most stable)&lt;br /&gt;
 I(R) = 0 / (1 + 0) = 0 &lt;br /&gt;
 I(Cyl) = 0 / (1 + 0) = 0&lt;br /&gt;
 I(O) = 0 / (1 + 0) = 0&lt;br /&gt;
 I(St) = 0 / (1 + 0) = 0&lt;br /&gt;
&lt;br /&gt;
After replacing each class with its corresponding instability value, the diagram would look as follows:&lt;br /&gt;
&lt;br /&gt;
[[Image:fig4.png]]&lt;br /&gt;
&lt;br /&gt;
The lower the value, the more stable the package is considered to be. In our case, all dependencies progress in the direction of stability, thus, according to the SDP, our design is proven to effective.&lt;br /&gt;
&lt;br /&gt;
Another important consideration when it comes to stability is that more stable packages should not be changed often. On the other hand, a highly volatile class can be changed easily, without affecting other packages in the system.&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
[1] The Tipping Point: Stability and Instability in OO Design, http://www.ddj.com/architect/184415285&lt;br /&gt;
&lt;br /&gt;
[2] On Package Design, https://prof.hti.bfh.ch/fileadmin/home/due1/uml_dp/udp_package_design_200906.pdf&lt;br /&gt;
&lt;br /&gt;
[3] What Makes A Good Object-Oriented Design?, http://ootips.org/ood-principles.html&lt;br /&gt;
&lt;br /&gt;
[4] Principles of Package Design, http://nandokakimoto.wordpress.com/2009/05/15/principles-of-package-design/&lt;br /&gt;
&lt;br /&gt;
[5] Stability, http://www.objectmentor.com/resources/articles/stability.pdf&lt;br /&gt;
c&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26338</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26338"/>
		<updated>2009-10-15T04:04:31Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, will talk about pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 What kind of vehicle do you have?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;SportsCar&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
&lt;br /&gt;
The updated code of the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would look as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  #Expects Car, Truck, SportsCar, or Bicycle, case sensitive&lt;br /&gt;
  type = gets             &lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  elsif my_car.class == SportsCar    # new class&lt;br /&gt;
    puts &amp;quot;I like 93 octane gasoline&amp;quot; # new output&lt;br /&gt;
  elsif my_car.class == Bicycle      # new class&lt;br /&gt;
    puts &amp;quot;I don't need any fuel&amp;quot;     # new output&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism]. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all [http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29 child classes]. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 What kind of vehicle do you have?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new [http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29 child classes] (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
For example, the new class could be defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SportsCar &amp;lt; Vehicle  # this is a new class&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a sports car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 93 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the most important part is that the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would remain absolutely unchanged, yet fully support the new addition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car, Truck, or SportsCar (newly defined), case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism] is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another advantage is that we let &amp;quot;an [http://en.wikipedia.org/wiki/Object_%28computer_science%29 object] figure out how it should behave&amp;quot; [5]. With that in mind, methods should be designed so that they are fully responsible for manipulating object's data which should normally be declared as &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. &lt;br /&gt;
&lt;br /&gt;
Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
Finally, implementing polymorphism in classes takes a lot of preparation and requires more time to be spent up front. Ultimately, the majority of the design decisions are made very early in the [http://en.wikipedia.org/wiki/Development_life_cycle development life cycle]. That requires a great deal of understanding of the issue that is being solved.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving [http://en.wikipedia.org/wiki/Software_maintenance maintainability] and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;br /&gt;
&lt;br /&gt;
[5] Polymorphism and Interfaces, http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26336</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26336"/>
		<updated>2009-10-15T04:03:15Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Pros */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, will talk about pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 What kind of vehicle do you have?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;SportsCar&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
&lt;br /&gt;
The updated code of the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would look as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  #Expects Car, Truck, SportsCar, or Bicycle, case sensitive&lt;br /&gt;
  type = gets             &lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  elsif my_car.class == SportsCar    # new class&lt;br /&gt;
    puts &amp;quot;I like 93 octane gasoline&amp;quot; # new output&lt;br /&gt;
  elsif my_car.class == Bicycle      # new class&lt;br /&gt;
    puts &amp;quot;I don't need any fuel&amp;quot;     # new output&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism]. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all [http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29 child classes]. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 What kind of vehicle do you have?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new [http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29 child classes] (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
For example, the new class could be defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SportsCar &amp;lt; Vehicle  # this is a new class&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a sports car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 93 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the most important part is that the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would remain absolutely unchanged, yet fully support the new addition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car, Truck, or SportsCar (newly defined), case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism] is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another advantage is that we let &amp;quot;an [http://en.wikipedia.org/wiki/Object_%28computer_science%29 object] figure out how it should behave&amp;quot; [5]. With that in mind, methods should be designed so that they are fully responsible for manipulating object's data which should normally be declared as &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. &lt;br /&gt;
&lt;br /&gt;
Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
Finally, implementing polymorphism in classes takes a lot of preparation and requires more time to be spent up front. Ultimately, the majority of the design decisions are made very early in the [http://en.wikipedia.org/wiki/Development_life_cycle development life cycle]. That requires a great deal of understanding of the issue that is being solved.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;br /&gt;
&lt;br /&gt;
[5] Polymorphism and Interfaces, http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26335</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26335"/>
		<updated>2009-10-15T04:00:47Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, will talk about pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 What kind of vehicle do you have?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;SportsCar&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
&lt;br /&gt;
The updated code of the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would look as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  #Expects Car, Truck, SportsCar, or Bicycle, case sensitive&lt;br /&gt;
  type = gets             &lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  elsif my_car.class == SportsCar    # new class&lt;br /&gt;
    puts &amp;quot;I like 93 octane gasoline&amp;quot; # new output&lt;br /&gt;
  elsif my_car.class == Bicycle      # new class&lt;br /&gt;
    puts &amp;quot;I don't need any fuel&amp;quot;     # new output&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism]. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all [http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29 child classes]. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 What kind of vehicle do you have?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new [http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29 child classes] (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
For example, the new class could be defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SportsCar &amp;lt; Vehicle  # this is a new class&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a sports car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 93 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the most important part is that the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would remain absolutely unchanged, yet fully support the new addition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car, Truck, or SportsCar (newly defined), case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism] is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another advantage is that we let &amp;quot;an object figure out how it should behave&amp;quot; [5]. With that in mind, methods should be designed so that they are fully responsible for manipulating object's data which should normally be declared as &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. &lt;br /&gt;
&lt;br /&gt;
Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
Finally, implementing polymorphism in classes takes a lot of preparation and requires more time to be spent up front. Ultimately, the majority of the design decisions are made very early in the [http://en.wikipedia.org/wiki/Development_life_cycle development life cycle]. That requires a great deal of understanding of the issue that is being solved.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;br /&gt;
&lt;br /&gt;
[5] Polymorphism and Interfaces, http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26334</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26334"/>
		<updated>2009-10-15T03:59:39Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, will talk about pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 What kind of vehicle do you have?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;SportsCar&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
&lt;br /&gt;
The updated code of the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would look as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  #Expects Car, Truck, SportsCar, or Bicycle, case sensitive&lt;br /&gt;
  type = gets             &lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  elsif my_car.class == SportsCar    # new class&lt;br /&gt;
    puts &amp;quot;I like 93 octane gasoline&amp;quot; # new output&lt;br /&gt;
  elsif my_car.class == Bicycle      # new class&lt;br /&gt;
    puts &amp;quot;I don't need any fuel&amp;quot;     # new output&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism]. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all [http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29 child classes]. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 What kind of vehicle do you have?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
For example, the new class could be defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SportsCar &amp;lt; Vehicle  # this is a new class&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a sports car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 93 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the most important part is that the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would remain absolutely unchanged, yet fully support the new addition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car, Truck, or SportsCar (newly defined), case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism] is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another advantage is that we let &amp;quot;an object figure out how it should behave&amp;quot; [5]. With that in mind, methods should be designed so that they are fully responsible for manipulating object's data which should normally be declared as &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. &lt;br /&gt;
&lt;br /&gt;
Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
Finally, implementing polymorphism in classes takes a lot of preparation and requires more time to be spent up front. Ultimately, the majority of the design decisions are made very early in the [http://en.wikipedia.org/wiki/Development_life_cycle development life cycle]. That requires a great deal of understanding of the issue that is being solved.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;br /&gt;
&lt;br /&gt;
[5] Polymorphism and Interfaces, http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26333</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26333"/>
		<updated>2009-10-15T03:58:00Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Pros */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, will talk about pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 What kind of vehicle do you have?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;SportsCar&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
&lt;br /&gt;
The updated code of the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would look as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  #Expects Car, Truck, SportsCar, or Bicycle, case sensitive&lt;br /&gt;
  type = gets             &lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  elsif my_car.class == SportsCar    # new class&lt;br /&gt;
    puts &amp;quot;I like 93 octane gasoline&amp;quot; # new output&lt;br /&gt;
  elsif my_car.class == Bicycle      # new class&lt;br /&gt;
    puts &amp;quot;I don't need any fuel&amp;quot;     # new output&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism]. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 What kind of vehicle do you have?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
For example, the new class could be defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SportsCar &amp;lt; Vehicle  # this is a new class&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a sports car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 93 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the most important part is that the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would remain absolutely unchanged, yet fully support the new addition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car, Truck, or SportsCar (newly defined), case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism] is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another advantage is that we let &amp;quot;an object figure out how it should behave&amp;quot; [5]. With that in mind, methods should be designed so that they are fully responsible for manipulating object's data which should normally be declared as &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. &lt;br /&gt;
&lt;br /&gt;
Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
Finally, implementing polymorphism in classes takes a lot of preparation and requires more time to be spent up front. Ultimately, the majority of the design decisions are made very early in the [http://en.wikipedia.org/wiki/Development_life_cycle development life cycle]. That requires a great deal of understanding of the issue that is being solved.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;br /&gt;
&lt;br /&gt;
[5] Polymorphism and Interfaces, http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26332</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26332"/>
		<updated>2009-10-15T03:57:31Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Cons */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, will talk about pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 What kind of vehicle do you have?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;SportsCar&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
&lt;br /&gt;
The updated code of the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would look as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  #Expects Car, Truck, SportsCar, or Bicycle, case sensitive&lt;br /&gt;
  type = gets             &lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  elsif my_car.class == SportsCar    # new class&lt;br /&gt;
    puts &amp;quot;I like 93 octane gasoline&amp;quot; # new output&lt;br /&gt;
  elsif my_car.class == Bicycle      # new class&lt;br /&gt;
    puts &amp;quot;I don't need any fuel&amp;quot;     # new output&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism]. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 What kind of vehicle do you have?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
For example, the new class could be defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SportsCar &amp;lt; Vehicle  # this is a new class&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a sports car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 93 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the most important part is that the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would remain absolutely unchanged, yet fully support the new addition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car, Truck, or SportsCar (newly defined), case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another advantage is that we let &amp;quot;an object figure out how it should behave&amp;quot; [5]. With that in mind, methods should be designed so that they are fully responsible for manipulating object's data which should normally be declared as &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. &lt;br /&gt;
&lt;br /&gt;
Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
Finally, implementing polymorphism in classes takes a lot of preparation and requires more time to be spent up front. Ultimately, the majority of the design decisions are made very early in the [http://en.wikipedia.org/wiki/Development_life_cycle development life cycle]. That requires a great deal of understanding of the issue that is being solved.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;br /&gt;
&lt;br /&gt;
[5] Polymorphism and Interfaces, http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26331</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26331"/>
		<updated>2009-10-15T03:56:19Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, will talk about pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 What kind of vehicle do you have?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;SportsCar&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
&lt;br /&gt;
The updated code of the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would look as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  #Expects Car, Truck, SportsCar, or Bicycle, case sensitive&lt;br /&gt;
  type = gets             &lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  elsif my_car.class == SportsCar    # new class&lt;br /&gt;
    puts &amp;quot;I like 93 octane gasoline&amp;quot; # new output&lt;br /&gt;
  elsif my_car.class == Bicycle      # new class&lt;br /&gt;
    puts &amp;quot;I don't need any fuel&amp;quot;     # new output&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism]. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 What kind of vehicle do you have?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
For example, the new class could be defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SportsCar &amp;lt; Vehicle  # this is a new class&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a sports car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 93 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the most important part is that the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would remain absolutely unchanged, yet fully support the new addition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car, Truck, or SportsCar (newly defined), case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another advantage is that we let &amp;quot;an object figure out how it should behave&amp;quot; [5]. With that in mind, methods should be designed so that they are fully responsible for manipulating object's data which should normally be declared as &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. &lt;br /&gt;
&lt;br /&gt;
Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
Finally, implementing polymorphism in classes takes a lot of preparation and requires more time to be spent up front. Ultimately, the majority of the design decisions are made very early in the development life cycle. That requires a great deal of understanding of the issue that is being solved.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;br /&gt;
&lt;br /&gt;
[5] Polymorphism and Interfaces, http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26329</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26329"/>
		<updated>2009-10-15T03:50:48Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, will talk about pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 What kind of vehicle do you have?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;SportsCar&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
&lt;br /&gt;
The updated code of the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would look as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  #Expects Car, Truck, SportsCar, or Bicycle, case sensitive&lt;br /&gt;
  type = gets             &lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  elsif my_car.class == SportsCar    # new class&lt;br /&gt;
    puts &amp;quot;I like 93 octane gasoline&amp;quot; # new output&lt;br /&gt;
  elsif my_car.class == Bicycle      # new class&lt;br /&gt;
    puts &amp;quot;I don't need any fuel&amp;quot;     # new output&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 What kind of vehicle do you have?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
For example, the new class could be defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SportsCar &amp;lt; Vehicle  # this is a new class&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a sports car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 93 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the most important part is that the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would remain absolutely unchanged, yet fully support the new addition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car, Truck, or SportsCar (newly defined), case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another advantage is that we let &amp;quot;an object figure out how it should behave&amp;quot; [5]. With that in mind, methods should be designed so that they are fully responsible for manipulating object's data which should normally be declared as &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. &lt;br /&gt;
&lt;br /&gt;
Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
Finally, implementing polymorphism in classes takes a lot of preparation and requires more time to be spent up front. Ultimately, the majority of the design decisions are made very early in the development life cycle. That requires a great deal of understanding of the issue that is being solved.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;br /&gt;
&lt;br /&gt;
[5] Polymorphism and Interfaces, http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26327</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26327"/>
		<updated>2009-10-15T03:48:01Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, will talk about pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 What kind of vehicle do you have?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;SportsCar&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
&lt;br /&gt;
The updated code of the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would look as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  #Expects Car, Truck, SportsCar, or Bicycle, case sensitive&lt;br /&gt;
  type = gets             &lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  elsif my_car.class == SportsCar    # new class&lt;br /&gt;
    puts &amp;quot;I like 93 octane gasoline&amp;quot; # new output&lt;br /&gt;
  elsif my_car.class == Bicycle      # new class&lt;br /&gt;
    puts &amp;quot;I don't need any fuel&amp;quot;     # new output&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 What kind of vehicle do you have?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
For example, the new class could be defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SportsCar &amp;lt; Vehicle  # this is a new class&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a sports car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 93 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the most important part is that the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would remain absolutely unchanged, yet fully support the new addition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car, Truck, or SportsCar, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another advantage is that we let &amp;quot;an object figure out how it should behave&amp;quot; [5]. With that in mind, methods should be designed so that they are fully responsible for manipulating object's data which should normally be declared as &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. &lt;br /&gt;
&lt;br /&gt;
Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
Finally, implementing polymorphism in classes takes a lot of preparation and requires more time to be spent up front. Ultimately, the majority of the design decisions are made very early in the development life cycle. That requires a great deal of understanding of the issue that is being solved.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;br /&gt;
&lt;br /&gt;
[5] Polymorphism and Interfaces, http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26326</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26326"/>
		<updated>2009-10-15T03:46:44Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, will talk about pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;SportsCar&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
&lt;br /&gt;
The updated code of the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would look as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  #Expects Car or Truck, case sensitive&lt;br /&gt;
  type = gets             &lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  elsif my_car.class == SportsCar    # new class&lt;br /&gt;
    puts &amp;quot;I like 93 octane gasoline&amp;quot; # new output&lt;br /&gt;
  elsif my_car.class == Bicycle      # new class&lt;br /&gt;
    puts &amp;quot;I don't need any fuel&amp;quot;     # new output&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 What kind of vehicle do you have?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
For example, the new class could be defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SportsCar &amp;lt; Vehicle  # this is a new class&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a sports car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 93 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the most important part is that the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would remain absolutely unchanged, yet fully support the new addition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;What kind of vehicle do you have?&amp;quot; &lt;br /&gt;
  type = gets # expects Car, Truck, or SportsCar, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another advantage is that we let &amp;quot;an object figure out how it should behave&amp;quot; [5]. With that in mind, methods should be designed so that they are fully responsible for manipulating object's data which should normally be declared as &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. &lt;br /&gt;
&lt;br /&gt;
Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
Finally, implementing polymorphism in classes takes a lot of preparation and requires more time to be spent up front. Ultimately, the majority of the design decisions are made very early in the development life cycle. That requires a great deal of understanding of the issue that is being solved.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;br /&gt;
&lt;br /&gt;
[5] Polymorphism and Interfaces, http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26325</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26325"/>
		<updated>2009-10-15T03:44:41Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, will talk about pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;SportsCar&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
&lt;br /&gt;
The updated code of the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would look as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  #Expects Car or Truck, case sensitive&lt;br /&gt;
  type = gets             &lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  elsif my_car.class == SportsCar    # new class&lt;br /&gt;
    puts &amp;quot;I like 93 octane gasoline&amp;quot; # new output&lt;br /&gt;
  elsif my_car.class == Bicycle      # new class&lt;br /&gt;
    puts &amp;quot;I don't need any fuel&amp;quot;     # new output&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
For example, the new class could be defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SportsCar &amp;lt; Vehicle  # this is a new class&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a sports car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 93 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the most important part is that the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would remain absolutely unchanged, yet fully support the new addition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another advantage is that we let &amp;quot;an object figure out how it should behave&amp;quot; [5]. With that in mind, methods should be designed so that they are fully responsible for manipulating object's data which should normally be declared as &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. &lt;br /&gt;
&lt;br /&gt;
Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
Finally, implementing polymorphism in classes takes a lot of preparation and requires more time to be spent up front. Ultimately, the majority of the design decisions are made very early in the development life cycle. That requires a great deal of understanding of the issue that is being solved.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;br /&gt;
&lt;br /&gt;
[5] Polymorphism and Interfaces, http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26324</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26324"/>
		<updated>2009-10-15T03:42:36Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, will talk about pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;SportsCar&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
&lt;br /&gt;
The updated code of the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would look as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  #Expects Car or Truck, case sensitive&lt;br /&gt;
  type = gets             &lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  elsif my_car.class == SportsCar    # new class&lt;br /&gt;
    puts &amp;quot;I like 93 octane gasoline&amp;quot; # new output&lt;br /&gt;
  elsif my_car.class == Bicycle      # new class&lt;br /&gt;
    puts &amp;quot;I don't need any fuel&amp;quot;     # new output&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
For example, the new class could be defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SportsCar &amp;lt; Vehicle  # this is a new class&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a sports car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 93 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the most import part is that the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would remain absolutely unchanged, yet fully support the new addition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another advantage is that we let &amp;quot;an object figure out how it should behave&amp;quot; [5]. With that in mind, methods should be designed so that they are fully responsible for manipulating object's data which should normally be declared as &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. &lt;br /&gt;
&lt;br /&gt;
Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
Finally, implementing polymorphism in classes takes a lot of preparation and requires more time to be spent up front. Ultimately, the majority of the design decisions are made very early in the development life cycle. That requires a great deal of understanding of the issue that is being solved.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;br /&gt;
&lt;br /&gt;
[5] Polymorphism and Interfaces, http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26323</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26323"/>
		<updated>2009-10-15T03:42:02Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, will talk about pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;SportsCar&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
&lt;br /&gt;
The updated code of the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would look as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  #Expects Car or Truck, case sensitive&lt;br /&gt;
  type = gets             &lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  elsif my_car.class == SportsCar    # new class&lt;br /&gt;
    puts &amp;quot;I like 93 octane gasoline&amp;quot; # new output&lt;br /&gt;
  elsif my_car.class == Bicycle      # new class&lt;br /&gt;
    puts &amp;quot;I don't need any fuel&amp;quot;     # new output&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
For example, the new class could be defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SportsCar &amp;lt; Vehicle  # this is a new class&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a sports car&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 93 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the most import part is that the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would remain absolutely unchanged, yet fully support the new addition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another advantage is that we let &amp;quot;an object figure out how it should behave&amp;quot; [5]. With that in mind, methods should be designed so that they are fully responsible for manipulating object's data which should normally be declared as &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. &lt;br /&gt;
&lt;br /&gt;
Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
Finally, implementing polymorphism in classes takes a lot of preparation and requires more time to be spent up front. Ultimately, the majority of the design decisions are made very early in the development life cycle. That requires a great deal of understanding of the issue that is being solved.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;br /&gt;
&lt;br /&gt;
[5] Polymorphism and Interfaces, http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26321</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26321"/>
		<updated>2009-10-15T03:39:44Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, will talk about pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;SportsCar&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
&lt;br /&gt;
The updated code of the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would look as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  #Expects Car or Truck, case sensitive&lt;br /&gt;
  type = gets             &lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  elsif my_car.class == SportsCar    # new class&lt;br /&gt;
    puts &amp;quot;I like 93 octane gasoline&amp;quot; # new output&lt;br /&gt;
  elsif my_car.class == Bicycle      # new class&lt;br /&gt;
    puts &amp;quot;I don't need any fuel&amp;quot;     # new output&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
That is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SportsCar &amp;lt; Vehicle  # this is a new class&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a sports car&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 93 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would remain absolutely unchanged, yet fully support the new addition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another advantage is that we let &amp;quot;an object figure out how it should behave&amp;quot; [5]. With that in mind, methods should be designed so that they are fully responsible for manipulating object's data which should normally be declared as &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. &lt;br /&gt;
&lt;br /&gt;
Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
Finally, implementing polymorphism in classes takes a lot of preparation and requires more time to be spent up front. Ultimately, the majority of the design decisions are made very early in the development life cycle. That requires a great deal of understanding of the issue that is being solved.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;br /&gt;
&lt;br /&gt;
[5] Polymorphism and Interfaces, http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26319</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26319"/>
		<updated>2009-10-15T03:36:06Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, will talk about pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;SportsCar&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
&lt;br /&gt;
The updated code of the &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; class would look as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  #Expects Car or Truck, case sensitive&lt;br /&gt;
  type = gets             &lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  elsif my_car.class == SportsCar    # new class&lt;br /&gt;
    puts &amp;quot;I like 93 octane gasoline&amp;quot; # new output&lt;br /&gt;
  elsif my_car.class == Bicycle      # new class&lt;br /&gt;
    puts &amp;quot;I don't need any fuel&amp;quot;     # new output&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another advantage is that we let &amp;quot;an object figure out how it should behave&amp;quot; [5]. With that in mind, methods should be designed so that they are fully responsible for manipulating object's data which should normally be declared as &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. &lt;br /&gt;
&lt;br /&gt;
Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
Finally, implementing polymorphism in classes takes a lot of preparation and requires more time to be spent up front. Ultimately, the majority of the design decisions are made very early in the development life cycle. That requires a great deal of understanding of the issue that is being solved.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;br /&gt;
&lt;br /&gt;
[5] Polymorphism and Interfaces, http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26317</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26317"/>
		<updated>2009-10-15T03:35:04Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, will talk about pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;SportsCar&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
&lt;br /&gt;
The updated code would look as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  #Expects Car or Truck, case sensitive&lt;br /&gt;
  type = gets             &lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  elsif my_car.class == SportsCar    # new class&lt;br /&gt;
    puts &amp;quot;I like 93 octane gasoline&amp;quot; # new output&lt;br /&gt;
  elsif my_car.class == Bicycle      # new class&lt;br /&gt;
    puts &amp;quot;I don't need any fuel&amp;quot;     # new output&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another advantage is that we let &amp;quot;an object figure out how it should behave&amp;quot; [5]. With that in mind, methods should be designed so that they are fully responsible for manipulating object's data which should normally be declared as &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. &lt;br /&gt;
&lt;br /&gt;
Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
Finally, implementing polymorphism in classes takes a lot of preparation and requires more time to be spent up front. Ultimately, the majority of the design decisions are made very early in the development life cycle. That requires a great deal of understanding of the issue that is being solved.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;br /&gt;
&lt;br /&gt;
[5] Polymorphism and Interfaces, http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26303</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26303"/>
		<updated>2009-10-15T03:18:14Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, will talk about pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;Motorcycle&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
&lt;br /&gt;
The updated code would look as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  #Expects Car or Truck, case sensitive&lt;br /&gt;
  type = gets             &lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  elsif my_car.class == SportsCar    # new class&lt;br /&gt;
    puts &amp;quot;I like 93 octane gasoline&amp;quot; # new output&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another advantage is that we let &amp;quot;an object figure out how it should behave&amp;quot; [5]. With that in mind, methods should be designed so that they are fully responsible for manipulating object's data which should normally be declared as &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. &lt;br /&gt;
&lt;br /&gt;
Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
Finally, implementing polymorphism in classes takes a lot of preparation and requires more time to be spent up front. Ultimately, the majority of the design decisions are made very early in the development life cycle. That requires a great deal of understanding of the issue that is being solved.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;br /&gt;
&lt;br /&gt;
[5] Polymorphism and Interfaces, http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26302</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26302"/>
		<updated>2009-10-15T03:17:39Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, will talk about pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;Motorcycle&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
&lt;br /&gt;
The updated code would look as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  #Expects Car or Truck, case sensitive&lt;br /&gt;
  type = gets             &lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  elsif my_car.class == SportsCar    # new class&lt;br /&gt;
    puts &amp;quot;I like 93 octane gasoline&amp;quot; # new output&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another advantage is that we let &amp;quot;an object figure out how it should behave&amp;quot; [5]. With that in mind, methods should be designed so that they are fully responsible for manipulating object's data which should normally be declared as &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. &lt;br /&gt;
&lt;br /&gt;
Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
Finally, implementing polymorphism in classes takes a lot of preparation and requires more time to be spent up front. Ultimately, the majority of the design decisions are made very early in the development life cycle. That requires a great deal of understanding of the issue that is being solved.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;br /&gt;
&lt;br /&gt;
[5] Polymorphism and Interfaces, http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26300</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26300"/>
		<updated>2009-10-15T03:17:06Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, will talk about pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;Motorcycle&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
&lt;br /&gt;
The updated code would look as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  #Expects Car or Truck, case sensitive&lt;br /&gt;
  type = gets             &lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  elsif my_car.class == SportsCar    # new class&lt;br /&gt;
    puts &amp;quot;I like 93 octane gasoline&amp;quot; # new output&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another advantage is that we let &amp;quot;an object figure out how it should behave&amp;quot; [5]. With that in mind, methods should be designed so that they are fully responsible for manipulating object's data which should normally be declared as &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. &lt;br /&gt;
&lt;br /&gt;
Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
Finally, implementing polymorphism in classes takes a lot of preparation and requires more time to be spent up front. Ultimately, the majority of the design decisions are made very early in the development life cycle. That requires a great deal of understanding of the issue that is being solved.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;br /&gt;
&lt;br /&gt;
[5] Polymorphism and Interfaces, http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26296</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26296"/>
		<updated>2009-10-15T03:06:33Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Pros */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, will talk about pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;Motorcycle&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement (or, better to replace it with a more appropriate conditional, such as &amp;lt;code&amp;gt;Case&amp;lt;/code&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another advantage is that we let &amp;quot;an object figure out how it should behave&amp;quot; [5]. With that in mind, methods should be designed so that they are fully responsible for manipulating object's data which should normally be declared as &amp;lt;code&amp;gt;private&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. &lt;br /&gt;
&lt;br /&gt;
Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
Finally, implementing polymorphism in classes takes a lot of preparation and requires more time to be spent up front. Ultimately, the majority of the design decisions are made very early in the development life cycle. That requires a great deal of understanding of the issue that is being solved.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;br /&gt;
&lt;br /&gt;
[5] Polymorphism and Interfaces, http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26294</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26294"/>
		<updated>2009-10-15T03:04:54Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Cons */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, will talk about pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;Motorcycle&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement (or, better to replace it with a more appropriate conditional, such as &amp;lt;code&amp;gt;Case&amp;lt;/code&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another advantage is that we let &amp;quot;an object figure out how it should behave&amp;quot; [5]. With that in mind, methods should take full responsibility for manipulating object's data&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. &lt;br /&gt;
&lt;br /&gt;
Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
Finally, implementing polymorphism in classes takes a lot of preparation and requires more time to be spent up front. Ultimately, the majority of the design decisions are made very early in the development life cycle. That requires a great deal of understanding of the issue that is being solved.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;br /&gt;
&lt;br /&gt;
[5] Polymorphism and Interfaces, http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26293</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26293"/>
		<updated>2009-10-15T03:04:09Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Cons */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, will talk about pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;Motorcycle&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement (or, better to replace it with a more appropriate conditional, such as &amp;lt;code&amp;gt;Case&amp;lt;/code&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another advantage is that we let &amp;quot;an object figure out how it should behave&amp;quot; [5]. With that in mind, methods should take full responsibility for manipulating object's data&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. &lt;br /&gt;
&lt;br /&gt;
Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
Finally, implementing polymorphism in classes takes a lot of preparation and requires more time to be spent up front. Ultimately, the majority of the design decisions are made very early in the development life cycle, and requires a great deal of understanding of the issue that is being solved.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;br /&gt;
&lt;br /&gt;
[5] Polymorphism and Interfaces, http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26291</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26291"/>
		<updated>2009-10-15T02:56:37Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Links */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, will talk about pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;Motorcycle&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement (or, better to replace it with a more appropriate conditional, such as &amp;lt;code&amp;gt;Case&amp;lt;/code&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another advantage is that we let &amp;quot;an object figure out how it should behave&amp;quot; [5]. With that in mind, methods should take full responsibility for manipulating object's data&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;br /&gt;
&lt;br /&gt;
[5] Polymorphism and Interfaces, http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26289</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26289"/>
		<updated>2009-10-15T02:56:11Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Links */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, will talk about pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;Motorcycle&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement (or, better to replace it with a more appropriate conditional, such as &amp;lt;code&amp;gt;Case&amp;lt;/code&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another advantage is that we let &amp;quot;an object figure out how it should behave&amp;quot; [5]. With that in mind, methods should take full responsibility for manipulating object's data&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;br /&gt;
&lt;br /&gt;
[5] http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26288</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=26288"/>
		<updated>2009-10-15T02:55:50Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Pros */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, will talk about pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;Motorcycle&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement (or, better to replace it with a more appropriate conditional, such as &amp;lt;code&amp;gt;Case&amp;lt;/code&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another advantage is that we let &amp;quot;an object figure out how it should behave&amp;quot; [5]. With that in mind, methods should take full responsibility for manipulating object's data&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25538</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25538"/>
		<updated>2009-10-10T03:27:13Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, will talk about pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;Motorcycle&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement (or, better to replace it with a more appropriate conditional, such as &amp;lt;code&amp;gt;Case&amp;lt;/code&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25521</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25521"/>
		<updated>2009-10-10T03:17:16Z</updated>

		<summary type="html">&lt;p&gt;Notfound: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statements considered harmful, and can be replaced by uses of polymorphism.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;Motorcycle&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement (or, better to replace it with a more appropriate conditional, such as &amp;lt;code&amp;gt;Case&amp;lt;/code&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25520</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25520"/>
		<updated>2009-10-10T03:15:58Z</updated>

		<summary type="html">&lt;p&gt;Notfound: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;b&amp;gt;Topic: If-statement Considered Harmful.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;Motorcycle&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement (or, better to replace it with a more appropriate conditional, such as &amp;lt;code&amp;gt;Case&amp;lt;/code&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25519</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25519"/>
		<updated>2009-10-10T03:15:39Z</updated>

		<summary type="html">&lt;p&gt;Notfound: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;center&amp;gt;Topic: If-statement Considered Harmful.&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;Motorcycle&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement (or, better to replace it with a more appropriate conditional, such as &amp;lt;code&amp;gt;Case&amp;lt;/code&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25518</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25518"/>
		<updated>2009-10-10T03:14:59Z</updated>

		<summary type="html">&lt;p&gt;Notfound: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Topic: If-statement Considered Harmful.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;Motorcycle&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement (or, better to replace it with a more appropriate conditional, such as &amp;lt;code&amp;gt;Case&amp;lt;/code&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25515</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25515"/>
		<updated>2009-10-10T03:13:35Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, &amp;lt;code&amp;gt;Motorcycle&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement (or, better to replace it with a more appropriate conditional, such as &amp;lt;code&amp;gt;Case&amp;lt;/code&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25513</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25513"/>
		<updated>2009-10-10T03:12:55Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new vehicle types, &amp;lt;code&amp;gt;Motorcycle&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement (or, better to replace it with a more appropriate conditional, such as &amp;lt;code&amp;gt;Case&amp;lt;/code&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
It is important to point out that the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25508</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25508"/>
		<updated>2009-10-10T03:11:30Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new vehicle types, &amp;lt;code&amp;gt;Motorcycle&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement (or, better to replace it with a more appropriate conditional, such as &amp;lt;code&amp;gt;Case&amp;lt;/code&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's [http://en.wikipedia.org/wiki/Refactor refactor] our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
However, the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25506</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25506"/>
		<updated>2009-10-10T03:09:16Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, pros and cons of polymorphism, and will demonstrate some code examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new vehicle types, &amp;lt;code&amp;gt;Motorcycle&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement (or, better to replace it with a more appropriate conditional, such as &amp;lt;code&amp;gt;Case&amp;lt;/code&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's refactor our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
However, the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25505</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25505"/>
		<updated>2009-10-10T03:08:59Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, pros and cons of polymorphism, and will demonstrate some examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new vehicle types, &amp;lt;code&amp;gt;Motorcycle&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement (or, better to replace it with a more appropriate conditional, such as &amp;lt;code&amp;gt;Case&amp;lt;/code&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's refactor our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
However, the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25503</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25503"/>
		<updated>2009-10-10T03:08:40Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism].&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, pros and cons of polymorphism , and will demonstrate some examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new vehicle types, &amp;lt;code&amp;gt;Motorcycle&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement (or, better to replace it with a more appropriate conditional, such as &amp;lt;code&amp;gt;Case&amp;lt;/code&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's refactor our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
However, the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25502</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25502"/>
		<updated>2009-10-10T03:07:49Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of polymorphism.&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, pros and cons of polymorphism , and will demonstrate some examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new vehicle types, &amp;lt;code&amp;gt;Motorcycle&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement (or, better to replace it with a more appropriate conditional, such as &amp;lt;code&amp;gt;Case&amp;lt;/code&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's refactor our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
However, the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25501</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25501"/>
		<updated>2009-10-10T03:06:34Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in [http://en.wikipedia.org/wiki/FORTRAN FORTRAN] in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of polymorphism.&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, pros and cons of both, and will demonstrate some examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new vehicle types, &amp;lt;code&amp;gt;Motorcycle&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement (or, better to replace it with a more appropriate conditional, such as &amp;lt;code&amp;gt;Case&amp;lt;/code&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's refactor our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
However, the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25498</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25498"/>
		<updated>2009-10-10T03:05:50Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Links */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in FORTRAN in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of polymorphism.&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, pros and cons of both, and will demonstrate some examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new vehicle types, &amp;lt;code&amp;gt;Motorcycle&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement (or, better to replace it with a more appropriate conditional, such as &amp;lt;code&amp;gt;Case&amp;lt;/code&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's refactor our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
However, the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;br /&gt;
&lt;br /&gt;
[4] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25493</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25493"/>
		<updated>2009-10-10T03:03:44Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Cons */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in FORTRAN in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of polymorphism.&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, pros and cons of both, and will demonstrate some examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new vehicle types, &amp;lt;code&amp;gt;Motorcycle&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement (or, better to replace it with a more appropriate conditional, such as &amp;lt;code&amp;gt;Case&amp;lt;/code&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's refactor our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
However, the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
One of the cons of polymorphism is that the &amp;quot;behaviors&amp;quot; are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. Another downside is that adding new methods/classes increases the complexity and the size of the code. It is &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25492</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25492"/>
		<updated>2009-10-10T03:02:32Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Cons */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in FORTRAN in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of polymorphism.&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, pros and cons of both, and will demonstrate some examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new vehicle types, &amp;lt;code&amp;gt;Motorcycle&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement (or, better to replace it with a more appropriate conditional, such as &amp;lt;code&amp;gt;Case&amp;lt;/code&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's refactor our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
However, the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
Among the cons is that the behaviors are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. Another downside is that adding new methods/classes increases the complexity and the size of the code. It is also &amp;quot;potentially harder to&lt;br /&gt;
understand&amp;quot; [3].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25491</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 4 va</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_4_va&amp;diff=25491"/>
		<updated>2009-10-10T03:01:58Z</updated>

		<summary type="html">&lt;p&gt;Notfound: /* Pros and Cons of Polymorphism */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in FORTRAN in a form of an [http://en.wikipedia.org/wiki/Arithmetic_IF arithmetic &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] back in 1957 [1]. The logic behind this language element is simple. The control of the program is redirected to one of three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language feature had become obsolete and was replaced by a [http://en.wikipedia.org/wiki/Conditional_%28programming%29 logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement] (if-then-else), the one that is still widely used to this day.&lt;br /&gt;
&lt;br /&gt;
The logical &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is very familiar to most programmers as it is one of the first language concepts they learn. Despite that, it is a cause of many code errors. Since the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement is more suitable for [http://en.wikipedia.org/wiki/Structured_programming structured programming], most today's [http://en.wikipedia.org/wiki/Object-oriented object-oriented] languages have more sophisticated mechanisms to replace it. One of such approaches is a use of polymorphism.&lt;br /&gt;
&lt;br /&gt;
This paper will discuss ways in which a polymorphism technique can be applied to replace the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; control structures, pros and cons of both, and will demonstrate some examples written in [http://en.wikipedia.org/wiki/Ruby_language Ruby language].&lt;br /&gt;
&lt;br /&gt;
== What is wrong with using &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements? ==&lt;br /&gt;
&lt;br /&gt;
Most of the time, a correctly written &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement would not cause any harm to one's code. It, however, possesses a potential to be error prone, as demonstrated by the following Ruby code: &lt;br /&gt;
 &lt;br /&gt;
 i = 1 # or any other number&lt;br /&gt;
 &lt;br /&gt;
 if i &amp;gt;= 0&lt;br /&gt;
   puts &amp;quot;greater or equal to zero&amp;quot;&lt;br /&gt;
 elsif i &amp;lt; 0&lt;br /&gt;
   puts &amp;quot;less than zero&amp;quot;&lt;br /&gt;
 else&lt;br /&gt;
   puts &amp;quot;will never run!!!!&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
As you can see from this example, the condition for the else part of the statement will never be met. This example does not suggest that a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements should be avoided. It simply demonstrates a potential in conditional statements to be error prone.&lt;br /&gt;
&lt;br /&gt;
== Example with &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements ==&lt;br /&gt;
&lt;br /&gt;
Let's consider a simple code that would illustrate a use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement in an object-oriented language. First, we define a parent class, &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt;, and two child classes - &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Truck&amp;lt;/code&amp;gt;. We would then ask a user to choose which of the two classes they would like to initialize. The &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement performs a dynamic type checking, and then depending on the user's selection, suggests a type of fuel that would work with the chosen vehicle.&lt;br /&gt;
&lt;br /&gt;
Below is a Ruby code that implements the above utilizing the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a commercial truck&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
&lt;br /&gt;
  if my_car.class == Car&lt;br /&gt;
    puts &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  elsif my_car.class == Truck&lt;br /&gt;
    puts &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we respond with &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt;, the output would look as follows:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles &lt;br /&gt;
grows further. For instance, let's imagine we decided to add two new vehicle types, &amp;lt;code&amp;gt;Motorcycle&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Bicycle&amp;lt;/code&amp;gt;. In order to support this addition, we would need to add two new classes, as well as to modify the &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statement (or, better to replace it with a more appropriate conditional, such as &amp;lt;code&amp;gt;Case&amp;lt;/code&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
== Example of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; replaced by polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Now, let's refactor our code by implementing the same example using polymorphism. The first modification is to introduce a method named &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; in all child classes. Similar to the earlier example, the caller of the method has no way of knowing which class will be chosen as it would be a run-time decision.&lt;br /&gt;
&lt;br /&gt;
Again, we will use Ruby code to demonstrate the new approach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    puts &amp;quot;I am a vehicle&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Car &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a passenger car&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like 87 octane gasoline&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Truck &amp;lt; Vehicle&lt;br /&gt;
  def initialize&lt;br /&gt;
    super&lt;br /&gt;
    puts &amp;quot;I am a truck&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  def get_type_of_fuel&lt;br /&gt;
    &amp;quot;I like diesel&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Driver&lt;br /&gt;
  puts &amp;quot;Do you have a Car or a Truck?&amp;quot; &lt;br /&gt;
  type = gets # expects Car or Truck, case sensitive&lt;br /&gt;
  my_car = eval(type).new&lt;br /&gt;
  puts my_car.get_type_of_fuel  &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;code&amp;gt;Car&amp;lt;/code&amp;gt; is selected, the output would remain the same:&lt;br /&gt;
&lt;br /&gt;
 Do you have a Car or a Truck?&lt;br /&gt;
 Car&lt;br /&gt;
 I am a vehicle&lt;br /&gt;
 I am a passenger car&lt;br /&gt;
 I like 87 octane gasoline&lt;br /&gt;
&lt;br /&gt;
However, the maintainability of the application has improved. For instance, as we add new &amp;lt;code&amp;gt;Vehicle&amp;lt;/code&amp;gt; types, the class &amp;lt;code&amp;gt;Driver&amp;lt;/code&amp;gt; would remain completely intact. Thus, the only change would be to add new child classes (and they must contain a &amp;lt;code&amp;gt;get_type_of_fuel&amp;lt;/code&amp;gt; method).&lt;br /&gt;
&lt;br /&gt;
== Pros and Cons of Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
=== Pros ===&lt;br /&gt;
According to [3], one of the biggest advantages of polymorphism is that new class &amp;quot;features&amp;quot; can be added incrementally. Additionally, these separate class &amp;quot;behaviors&amp;quot; are coded independently from each other, &amp;quot;thus minimizing the possible interference.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=== Cons ===&lt;br /&gt;
Among the cons is that the behaviors are spread among multiple classes (abstractions), as opposed to having everything in one conditional statement. Another downside is that adding new methods/classes increases the complexity and the size of the code. It is also &amp;quot;potentially harder to&lt;br /&gt;
understand.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As shown in this article, the use of &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; statements can be a cause of uncertainty. In many cases, &amp;lt;code&amp;gt;If&amp;lt;/code&amp;gt; and other conditionals can be successfully transformed to polymorphism, also improving maintainability and flexibility of the application [3].&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
[1] Arithmetic IF, http://en.wikipedia.org/wiki/Arithmetic_IF&lt;br /&gt;
&lt;br /&gt;
[2] Conditional statements, http://en.wikipedia.org/wiki/If_statement&lt;br /&gt;
&lt;br /&gt;
[3] Transform Conditionals to Polymorphism, http://scg.unibe.ch/archive/papers/Duca00cTransform.pdf&lt;/div&gt;</summary>
		<author><name>Notfound</name></author>
	</entry>
</feed>