+ Start a Discussion
SFDC Apex DevSFDC Apex Dev 

Need to optimize the below apex code

Hi Everyone,

I want to optimize the below apex code, but I'm facing technical chalenge. I want to add the repetetive code used in if and else block in some function so that I can call that in the if else block instead of adding the repetetive code.

Highlighting the code below which is repeated and has to be added to some function which can be called in place of that and can able to reduce the line of code and optimize it.
public class void newClass{	
	@RemoteAction
	global static ccrz.cc_RemoteActionResult newMethod(final ccrz.cc_RemoteActionContext ctx,string a, string locale){
		ccrz.cc_RemoteActionResult res = new ccrz.cc_RemoteActionResult();
		Boolean guestUsr = false;
		String USER_LOCALE;
		String User_Locale_Search;
		if(String.isBlank(locale)){
			USER_LOCALE = UserInfo.getLocale();
			if(String.isBlank(USER_LOCALE)){
				USER_LOCALE = 'en_US';
			}
		}else{
			USER_LOCALE = locale;
		}
		if(UserInfo.getUserType() == 'Guest') {
			if(!String.isBlank(locale)){
				USER_LOCALE = locale;
				User_Locale_Search = '%'+ locale +'%';
			}else{
				USER_LOCALE = 'en_US';
			}
			guestUsr = true;
		}
		
		try{
			Id recordType = Schema.SObjectType.O_C_R__c.getRecordTypeInfosByName().get('Nav').getRecordTypeId();
			O_M_P__c oModelProd = getoTaxonomyId(sku);
			O_C_R__c o = [select id, P_T__c,C_T__c,Ch_Ca__c,Par_Ca__r.Name, Relationship_Type__c,RecordTypeId from O_C_R__c where Ch_Ca__r.Name=: oModelProd.O_Cat_P__r.Name and Relationship_Type__c = 'sn_p5' and RecordTypeId=:recordType ];
			O_C_R__c p = [select id, P_T__c,C_T__c,Ch_Ca__c,Par_Ca__c,Par_Ca__r.Name,RecordTypeId,Relationship_Type__c from O_C_R__c where Ch_Ca__r.Name =: o.Par_Ca__r.Name and  RecordTypeId = :recordType and Relationship_Type__c= 'sn_q4'];
			O_C_R__c q = [select id, P_T__c,C_T__c,Ch_Ca__c,Par_Ca__c,Par_Ca__r.Name,RecordTypeId,Relationship_Type__c  from O_C_R__c where Ch_Ca__r.Name =: p.Par_Ca__r.Name and  RecordTypeId = :recordType and Relationship_Type__c = 'sn_r3'];
			O_C_R__c r = [select id, P_T__c,C_T__c,Ch_Ca__c,Par_Ca__c, Par_Ca__r.Name from O_C_R__c where Ch_Ca__r.Name =: q.Par_Ca__r.Name];
			
			details addNewDetails = new details();
			if(guestUsr == false){
				if(USER_LOCALE == 'en_US'){
					if (oModelProd!= null){
						addNewDetails.customName = oModelProd.C_Pro__r.Custom__c;
					}
					if(q!=null){
						addNewDetails.rName = q.P_T__c;
						addNewDetails.rid = q.Par_Ca__c;
						addNewDetails.qName = q.C_T__c;
						addNewDetails.qid = q.Ch_Ca__c;
					}
					if(p != null){
						addNewDetails.pName =  p.C_T__c;
						addNewDetails.pid =  p.Ch_Ca__c;
					}
					if(o != null){
						addNewDetails.oName =  o.C_T__c;
						addNewDetails.oid =  o.Ch_Ca__c;
					}
					if(r!= null){
						addNewDetails.l1Name = r.P_T__c;
					}
				}else{
					if (oModelProd!= null){
						addNewDetails.customName = oModelProd.C_Pro__r.Custom__c;
					}
					if(o != null){
						O_Cat_P_18N__c i18NCatalog = [select id,Title__c,O_Cat_P__c,O_Cat_P__r.Title__c,name from O_Cat_P_18N__c where O_Cat_P__c =: o.Ch_Ca__c and Locale__c =: USER_LOCALE];
						addNewDetails.oName = i18NCatalog.Title__c ;
						addNewDetails.oid =  o.Ch_Ca__c;
						addNewDetails.oNameURL = o.C_T__c;
					}
					if(p != null){
						O_Cat_P_18N__c i18NCatalog = [select id,Title__c,O_Cat_P__c,O_Cat_P__r.Title__c,name from O_Cat_P_18N__c where O_Cat_P__c =: p.Ch_Ca__c and Locale__c =: USER_LOCALE];
						addNewDetails.pName = i18NCatalog.Title__c ;
						addNewDetails.pid =  p.Ch_Ca__c;
						addNewDetails.pNameURL =  p.C_T__c;
					}
					if(q!=null){
						O_Cat_P_18N__c i18NCatalog = [select id,Title__c,O_Cat_P__c,O_Cat_P__r.Title__c,name from O_Cat_P_18N__c where O_Cat_P__c =: q.Ch_Ca__c and Locale__c =: USER_LOCALE];
						O_Cat_P_18N__c i18NCatalog1 = [select id,Title__c,O_Cat_P__c,O_Cat_P__r.Title__c,name from O_Cat_P_18N__c where O_Cat_P__c =: q.Par_Ca__c and Locale__c =: USER_LOCALE];
						addNewDetails.qName = i18NCatalog.Title__c;
						addNewDetails.rName = i18NCatalog1.Title__c;
						addNewDetails.rid = q.Par_Ca__c;
						addNewDetails.qid = q.Ch_Ca__c;
						addNewDetails.rNameURL = q.P_T__c;
						addNewDetails.qNameURL = q.C_T__c;
					}
					if(r!= null){
						O_Cat_P_18N__c i18NCatalog = [select id,Title__c,O_Cat_P__c,O_Cat_P__r.Title__c,name from O_Cat_P_18N__c where O_Cat_P__c =: r.Par_Ca__c and Locale__c =: USER_LOCALE];
						addNewDetails.l1Name = i18NCatalog.Title__c;
						addNewDetails.l1NameURL = r.P_T__c;
					}
				}
			}else{
				if(USER_LOCALE == Label.HN_DefaultLocale){
					if (oModelProd!= null){
						addNewDetails.customName = oModelProd.C_Pro__r.Custom__c;
					}
					if(q!=null){
						addNewDetails.rName = q.P_T__c;
						addNewDetails.rid = q.Par_Ca__c;
						addNewDetails.qName = q.C_T__c;
						addNewDetails.qid = q.Ch_Ca__c;
					}
					if(p != null){
						addNewDetails.pName =  p.C_T__c;
						addNewDetails.pid =  p.Ch_Ca__c;
					}
					if(o != null){
						addNewDetails.oName =  o.C_T__c;
						addNewDetails.oid =  o.Ch_Ca__c;
					}
					if(r!= null){
						addNewDetails.l1Name = r.P_T__c;
					}
				}else{
					if (oModelProd!= null){
						addNewDetails.customName = oModelProd.C_Pro__r.Custom__c;
					}
					if(o != null){
						O_Cat_P_18N__c i18NCatalog = [select id,Title__c,O_Cat_P__c,O_Cat_P__r.Title__c,name from O_Cat_P_18N__c where O_Cat_P__c =: o.Ch_Ca__c and Locale__c LIKE: User_Locale_Search];
						addNewDetails.oName = i18NCatalog.Title__c ;
						addNewDetails.oid =  o.Ch_Ca__c;
						addNewDetails.oNameURL =  o.C_T__c;
					}
					if(p != null){
						O_Cat_P_18N__c i18NCatalog = [select id,Title__c,O_Cat_P__c,O_Cat_P__r.Title__c,name from O_Cat_P_18N__c where O_Cat_P__c =: p.Ch_Ca__c and Locale__c LIKE: User_Locale_Search];
						addNewDetails.pName = i18NCatalog.Title__c ;
						addNewDetails.pid =  p.Ch_Ca__c;
						addNewDetails.pNameURL =  p.C_T__c;
					}
					if(q!=null){
						O_Cat_P_18N__c i18NCatalog = [select id,Title__c,O_Cat_P__c,O_Cat_P__r.Title__c,name from O_Cat_P_18N__c where O_Cat_P__c =: q.Ch_Ca__c and Locale__c LIKE: User_Locale_Search];
						O_Cat_P_18N__c i18NCatalog1 = [select id,Title__c,O_Cat_P__c,O_Cat_P__r.Title__c,name from O_Cat_P_18N__c where O_Cat_P__c =: q.Par_Ca__c and Locale__c LIKE: User_Locale_Search];
						addNewDetails.qName = i18NCatalog.Title__c;
						addNewDetails.rName = i18NCatalog1.Title__c;
						addNewDetails.rid = q.Par_Ca__c;
						addNewDetails.qid = q.Ch_Ca__c;
						addNewDetails.rNameURL = q.P_T__c;
						addNewDetails.qNameURL = q.C_T__c;
					}
					if(r!= null){
						O_Cat_P_18N__c i18NCatalog = [select id,Title__c,O_Cat_P__c,O_Cat_P__r.Title__c,name from O_Cat_P_18N__c where O_Cat_P__c =: r.Par_Ca__c and Locale__c LIKE: User_Locale_Search];
						addNewDetails.l1Name = i18NCatalog.Title__c;
						addNewDetails.l1NameURL = r.P_T__c;
					}
				}
			}
			if(addNewDetails!=null){
				res.success = true;
				res.data = addNewDetails;
			}else{
				res.success = false;
			}
		}catch(Exception exp){
			res.success = false;
			res.data = exp.getMessage() + ' ' + exp.getLineNumber();
		}
		return res ;
	}

	public class details{
		public string l1Name{get;set;}
		public string rName{get;set;}
		public string rid{get;set;}
		public string qName{get;set;}
		public string qid{get;set;}
		public string pName{get;set;}
		public string pid{get;set;}
		public string oName{get;set;}
		public string oid{get;set;}
		public string customName{get;set;}
		public string l1NameURL{get;set;}
		public string rNameURL{get;set;}
		public string qNameURL{get;set;} 
		public string pNameURL{get;set;}       
		public string oNameURL{get;set;}
	}
}

For guest user and loggedin user the code is repetetice. Can anyone please help me to optimize/reduce the line of code?