How can I reduce the cyclomatic complexity of this CloseButtonDimensions
function?
export function isMobile():boolean {
return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|iemobile|Windows Phone/i.test(navigator.userAgent) );
}
get CloseButtonDimensions(): Size {
const IS_MOBILE: Boolean = isMobile();
let mediaType:String = this.options.tag.media_type;
let adWidth:number = this.options.tag.size.width;
let adHeight:number = this.options.tag.size.height;
let closeButtonHeight:number;
let closeButtonWidth:number;
if( (mediaType == "VIDEO") && !IS_MOBILE && ((adWidth >= 640 && adHeight >= 480) || (adWidth >= 480 && adHeight >= 360)) ) {
closeButtonWidth = 15;
closeButtonHeight = 15;
} else if( ((mediaType == "VIDEO") && (adWidth >= 300 && adHeight >= 250)) ||
((mediaType == "BANNER") && (adWidth == 300 && adHeight == 250)) ||
((mediaType == "BANNER") && !IS_MOBILE && (adWidth == 468 && adHeight == 60)) ) {
closeButtonWidth = 11;
closeButtonHeight = 11;
} else if( (mediaType == "VIDEO") && IS_MOBILE && (adWidth >= 320 && adHeight >= 240) ||
(mediaType == "BANNER") && !IS_MOBILE && (adWidth == 728 && adHeight == 90) ) {
closeButtonWidth = 13;
closeButtonHeight = 13;
} else if((mediaType == "BANNER") && !IS_MOBILE && (adWidth == 468 && adHeight == 60)) {
closeButtonWidth = 13;
closeButtonHeight = 13;
} else if( (mediaType == "BANNER") && IS_MOBILE && ((adWidth == 320 && adHeight == 50) || (adWidth == 300 && adHeight == 50)) ) {
closeButtonWidth = 8;
closeButtonHeight = 8;
}
return {
width: closeButtonWidth,
height: closeButtonHeight
}
}
mediaType
that I can get in the function are VIDEO
and BANNER
.
Ad dimensions (widthxheight
) for video are (these can be greater than or equal to):
- Desktop: "640x480", "480x360", "300x250"
- Mobile: "300x250", "320x240"
Ad dimensions (widthxheight
) for banner are (these will be exact not greater nor equal):
- Desktop: "728x90", "468x60", "300x250"
- Mobile: "300x250", "320x50", "300x50"
IS_MOBILE
will be true/false depending on the device.
Questions:
- How can I approach such complex
if
else
statements while coding? - How can I make code more readable especially for junior developers or when I make commits to repos so others can understand better?