Replacing Nested Switch Statements with Objects
Posted By : Ishaan Madan | 25-Sep-2017
I came across a scenario in which I had to use nested switch statements to return values based on nested cases. Although easy to implement using nested switch cases was not convincing enough for me considering the performance.
My code looked like the below function:
The function converts array indexes into minutes based on hour parameter named "
function replaceMinuteIndexInMinutes(hourGranularity, index) {switch (hourGranularity) {case 1:{switch (index) {case 0:return 0;default:return null;}}case 2:{switch (index) {case 0:return 0;case 1:return 30;default:return null;}}case 4:{switch (index) {case 0:return 0;case 1:return 15;case 2:return 30;case 3:return 45;default:return null;}}default:{return 0;}}}
The above function looks great but can prove to be a performance nightmare, So, A better way to achieve the desired result using such static values is using object literals. The approach uses multidimensional arrays/objects which involves taking each case as an index/key and returning the value for the corresponding matrix formed. For Instance:
function replaceMinuteIndexInMinutes(hourGranularity, index) {let minuteIndexInMinutesObj = {1:{0: 0},2:{0: 0,1: 30,},4:{0: 0,1: 15,2: 30,3: 45,},}return minuteIndexInMinutesObj[hourGranularity][index]}
Thanks.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Ishaan Madan
Ishaan, a skilled technical project manager, excels at breaking down complex projects into manageable tasks. With a background in both technology and project management, he offers a unique perspective to every project he undertakes. His effective communication skills enable him to collaborate seamlessly with both technical and non-technical stakeholders, ensuring everyone is aligned towards shared objectives. He has hands-on experience in utilizing agile methodologies like Scrum and Kanban to drive project management and foster team collaboration. He leverages project management tools such as JIRA, Trello, and Clickup to monitor progress, manage tasks, and facilitate communication among team members and stakeholders. Moreover, his proficiency in full-stack development empowers him to comprehend the technical aspects of projects and provide guidance to developers when necessary. He demonstrates expertise in utilizing popular Python frameworks like Django and Flask, along with data analysis and manipulation libraries such as NumPy and Pandas. On the front-end, Ishaan adeptly employs JavaScript libraries like React and Angular to craft visually appealing and user-friendly interfaces. Additionally, he possesses proficiency in HTML, CSS, and JavaScript for designing responsive and mobile-friendly layouts.