[vc_row][vc_column][nz_btn text=”Software Resiliency” link=”http://casthighlight.wpengine.com/software-resiliency/” target=”_self” animate=”false” animation_type=”ghost” color=”black” size=”small” shape=”rounded” type=”normal” hover_normal=”fill” hover_ghost=”fill” icon=”icon-dashboard”][nz_btn text=”Code Reliability” target=”_self” animate=”false” animation_type=”ghost” color=”black” size=”small” shape=”rounded” type=”ghost” hover_normal=”fill” hover_ghost=”screen” link=”http://casthighlight.wpengine.com/category/product/indicators-methodology/code-insights/software-resiliency/code-reliability/” icon=”icon-code”][vc_empty_space height=”12px”][vc_row_inner][vc_column_inner width=”1/2″][vc_column_text pl=”0″ pr=”0″ pt=”0″ pb=”0″]
Why you should care
[/vc_column_text][vc_column_text pl=”0″ pr=”0″ pt=”0″ pb=”0″]Although Switch cases do not require ending breaks after the default statement, It is recommended to end every branch with a return, or break statement, for the following reasons –
Refactorability – Branches ending with break or end can be easily reordered without having their changing their functionality.
Consistency – Similar branches should have a consistent ending as it makes the code easier to read and understand.
Protection – Ending all switch branches with a break is a fantastic habit as it helps in detecting missing break statements which is crucial for debugging and troubleshooting[/vc_column_text][/vc_column_inner][vc_column_inner width=”1/2″][vc_column_text pl=”0″ pr=”0″ pt=”0″ pb=”0″]
Business Impacts
Ending switch branches with ‘breaks’ helps the code be more consistent, functional, and easier to debug. This allows developers in different teams navigate the code easier and be more productive in the development process. Lack of breaks makes the code difficult for other developers to understand.[/vc_column_text][nz_btn text=”Production Risk” target=”_self” animate=”false” animation_type=”ghost” color=”pink” size=”small” shape=”rounded” type=”normal” hover_normal=”opacity” hover_ghost=”fill” link=”http://casthighlight.wpengine.com/category/product/indicators-methodology/innovation/” icon=”icon-office”][nz_btn text=”Complexity” target=”_self” animate=”false” animation_type=”ghost” color=”mulled_wine” size=”small” shape=”rounded” type=”ghost” hover_normal=”opacity” hover_ghost=”fill” link=”http://casthighlight.wpengine.com/category/product/indicators-methodology/innovation/” icon=”icon-hard”][/vc_column_inner][/vc_row_inner][vc_empty_space height=”12px”][vc_empty_space height=”32px”][vc_single_image image=”5362″ img_link_large=”” img_link_target=”_self” img_size=”full” alignment=”none”][vc_row_inner][vc_column_inner width=”1/2″][vc_column_text pl=”0″ pr=”0″ pt=”0″ pb=”0″]
CAST recommendations
CAST recommends developers to build habits on making switch cases more readable by adding ending breaks where needed. This habit can be fostered by following a standardized guide internally set by the company which follows specific guidelines on writing code. Not all switch cases need ending breaks so long as the code is understandable without compromising its’ functionality.
The key points to follow regarding switch cases is that the best programs are built from consistent code that focuses on functionality first. Since many companies are shifting towards an agile-based environment, it is easy for development teams to get lost in the shuffle, especially with developers with their unique programming habits. While having a style guide helps, it is also important to have developers undergo training and be constantly updated on new technologies that are to be implemented in future projects.[/vc_column_text][/vc_column_inner][vc_column_inner width=”1/2″][vc_column_text pl=”0″ pr=”0″ pt=”0″ pb=”0″]
References
https://softwareengineering.stackexchange.com/questions/201777/break-on-default-case-in-switch
https://www.tutorialspoint.com/java/java_break_statement.htm[/vc_column_text][nz_btn text=”Style Guide” target=”_self” animate=”false” animation_type=”ghost” color=”turquoise” size=”small” shape=”rounded” type=”ghost” hover_normal=”opacity” hover_ghost=”fill” link=”https://github.com/Kristories/awesome-guidelines” icon=”icon-book”][/vc_column_inner][/vc_row_inner][vc_separator color=”grey” align=”align_center” style=”” border_width=”” el_width=””][vc_column_text pl=”0″ pr=”0″ pt=”0″ pb=”0″]
How we detect
[/vc_column_text][vc_column_text pl=”0″ pr=”0″ pt=”0″ pb=”0″]This code insight counts a violation each time the code misses a break which fails to end ‘case’ clauses
int main()
{
int i = 2;
switch(i)
{
case 0:
cout << “0” << endl;
// Violation
case 1:
// No violation: Empty clause
case 2:
cout << “1 or 2” << endl;
break; // No violation: A break
default:
cout << “Other” << endl;
// No violation: Last clause is default
}
}
It checks for a ‘switch’-clause that does not end with a ‘break’, ‘return’ or ‘throw’. This construct should be at the top level in the ‘switch’-clause. However, the rule is also triggered if the last ‘switch’-clause of a ‘switch’ statement is ‘default’, and does not explicitly end with ‘break’, ‘return’ or ‘throw’.[/vc_column_text][vc_row_inner][vc_column_inner width=”1/1″][vc_single_image image=”5362″ img_link_large=”” img_link_target=”_self” img_size=”full” alignment=”none”][vc_column_text pl=”0″ pr=”0″ pt=”0″ pb=”0″]
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/” target=”_self” icon=”icon-stats” animate=”false” animation_type=”ghost” color=”black” size=”small” shape=”rounded” type=”normal” hover_normal=”fill” hover_ghost=”fill”][nz_btn text=”How it works” link=”http://casthighlight.wpengine.com/how-it-works/” target=”_self” icon=”icon-cog” animate=”false” animation_type=”ghost” color=”black” size=”small” shape=”rounded” type=”ghost” hover_normal=”fill” hover_ghost=”fill”][/vc_column_inner][/vc_row_inner][/vc_column][/vc_row]
Leave a Reply