[vc_row][vc_column][nz_btn text=”Cost” link=”http://casthighlight.wpengine.com/category/product/indicators-methodology/cost/” icon=”icon-office” color=”green” shape=”rounded” hover_normal=”opacity”][nz_btn text=”Software Agility” link=”http://casthighlight.wpengine.com/software-resiliency/” icon=”icon-dashboard” color=”black” size=”medium” shape=”rounded”][nz_btn text=”Code Readability” link=”http://casthighlight.wpengine.com/category/product/indicators-methodology/code-insights/software-resiliency/code-reliability/” icon=”icon-code” color=”black” shape=”rounded” type=”ghost” hover_ghost=”screen”][vc_empty_space height=”12px”][vc_column_text]
Ternary operators in Java can make code unreadable
[/vc_column_text][vc_column_text]This code insight counts one violation each time a ternary operator is encountered.
Note : Do not confuse with question mark used as wildcard for unknow generic type.
bad
String data = (str.contains("A") ? "Str contains 'A'" : "Str doesn't contains 'A'");
good
if (str.contains("A")) {
data = "Str contains 'A'";
}
else {
data = "Str doesn't contains 'A'";
}
[/vc_column_text][vc_empty_space height=”12px”][vc_empty_space][vc_single_image image=”5362″ img_size=”full”][vc_row_inner][vc_column_inner width=”1/2″][vc_column_text]
Why you should care
[/vc_column_text][vc_column_text]
While the ternary operator is pleasingly compact, its use can make code more difficult to read. It should therefore be avoided in favor of the more verbose if/elsestructure.
[/vc_column_text][/vc_column_inner][vc_column_inner width=”1/2″][vc_column_text]
Business Impacts
It is recommended to avoid these in order to ensure the code is more readable and cost effective.
[/vc_column_text][nz_btn text=”Cost” link=”http://casthighlight.wpengine.com/category/product/indicators-methodology/innovation/” icon=”icon-office” color=”green” shape=”rounded” hover_normal=”opacity”][/vc_column_inner][/vc_row_inner][vc_row_inner][vc_column_inner width=”1/2″][vc_separator][vc_column_text]
CAST recommendations
[/vc_column_text][/vc_column_inner][vc_column_inner width=”1/2″][vc_separator][vc_column_text]
References
[/vc_column_text][/vc_column_inner][/vc_row_inner][vc_row_inner][vc_column_inner][vc_single_image image=”5362″ img_size=”full”][vc_column_text]
About CAST and Highlight’s Code Insights
Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.
[/vc_column_text][vc_empty_space height=”16px”][nz_btn text=”See features” link=”http://casthighlight.wpengine.com/outputs-analytics/” icon=”icon-stats” color=”black” shape=”rounded”][nz_btn text=”How it works” link=”http://casthighlight.wpengine.com/how-it-works/” icon=”icon-cog” color=”black” shape=”rounded” type=”ghost”][/vc_column_inner][/vc_row_inner][/vc_column][/vc_row][vc_row][vc_column][vc_column_text]
c
[/vc_column_text][/vc_column][/vc_row]
Leave a Reply