Skip to main content
Post Closed as "Not suitable for this site" by vnp, Sᴀᴍ Onᴇᴌᴀ, pacmaninbw, Peilonrayz, Linny
Rollback to Revision 3
Source Link
Zeta
  • 19.3k
  • 2
  • 55
  • 89

User details Validation logging

I have written this codelogic for logging, but the codeis getting replicatereplication for all fields ie last(last name, DOB,city, state, zipcodeZIP, etc). Here only validation logic is going to differ, whereas logging functionality will remain the same, and i will be logging both success and error message's.Is there any way to reduce the redundant code ?

public boolean validFirstName(UserInputDetails record) {
        if (CommonUtil.isNullOrEmpty(record.getFirstName())) {
            record.setErrorCode(ErrorCodes.FIRSTNAME_NULL.getErrorCode());
            if(record.isDetailedLoggingEnabled()){
                Map<String,String> dtlLogMap = new HashMap<>(5);
                dtlLogMap.put("transId",String.valueOf(record.getTransactionId()));
                dtlLogMap.put("firstName",record.getFirstName());
                dtlLogMap.put("validationName","VALID_FIRSTNAME_CHECK");
                dtlLogMap.put("validationStatus","Errored");
                dtlLogMap.put("errorCode",record.getErrorCode());
                DetailedLogger.log(dtlLogMap);
            }
            return false;
        }
        
        if ( record.getFirstName().length() >=50) {
            record.setErrorCode(ErrorCodes.FIRSTNAME_INVALID_LENGTH.getErrorCode());
            if(record.isDetailedLoggingEnabled()){
                Map<String,String> dtlLogMap = new HashMap<>(6);
                dtlLogMap.put("transId",String.valueOf(record.getTransactionId()));
                dtlLogMap.put("firstName",record.getFirstName());
                dtlLogMap.put("firstNameLength",returnStringifNotNull(record.getFirstName().length()));
                dtlLogMap.put("validationName","VALID_FIRSTNAME_LENGTH_CHECK");
                dtlLogMap.put("validationStatus","Errored");
                dtlLogMap.put("errorCode",record.getErrorCode());
                DetailedLogger.log(dtlLogMap);
            }
            return false;
        }
        if(record.isDetailedLoggingEnabled()){
            Map<String,String> dtlLogMap = new HashMap<>(4);
            dtlLogMap.put("transId",String.valueOf(record.getTransactionId()));
            dtlLogMap.put("firstName",record.getFirstName());
            dtlLogMap.put("validationName","VALID_FIRSTNAME_CHECK");
            dtlLogMap.put("validationStatus","passed");
            DetailedLogger.log(dtlLogMap);
        }
        return true;
    }

public boolean validZipCode(UserInputDetails record) {
        if (CommonUtil.isNullOrEmpty(record.getZipCode())) {
            record.setErrorCode(ErrorCodes.ZIPCODE_NULL.getErrorCode());
            if(record.isDetailedLoggingEnabled()){
                Map<String,String> dtlLogMap = new HashMap<>(5);
                dtlLogMap.put("transId",String.valueOf(record.getTransactionId()));
                dtlLogMap.put("zipcode",record.getZipCode());
                dtlLogMap.put("validationName","VALID_ZIPCODE_CHECK");
                dtlLogMap.put("validationStatus","Errored");
                dtlLogMap.put("errorCode",record.getErrorCode());
                DetailedLogger.log(dtlLogMap);
            }
            return false;
        }
        
        if ( record.getZipCode().length() >=5) {
            record.setErrorCode(ErrorCodes.ZIPCODE_INVALID_LENGTH.getErrorCode());
            if(record.isDetailedLoggingEnabled()){
                Map<String,String> dtlLogMap = new HashMap<>(6);
                dtlLogMap.put("transId",String.valueOf(record.getTransactionId()));
                dtlLogMap.put("zipcode",record.getZipCode());
                dtlLogMap.put("zipcodeLength",returnStringifNotNull(record.getZipCode().length()));
                dtlLogMap.put("validationName","VALID_ZIPCODE_LENGTH_CHECK");
                dtlLogMap.put("validationStatus","Errored");
                dtlLogMap.put("errorCode",record.getErrorCode());
                DetailedLogger.log(dtlLogMap);
            }
            return false;
        }
        if(record.isDetailedLoggingEnabled()){
            Map<String,String> dtlLogMap = new HashMap<>(4);
            dtlLogMap.put("transId",String.valueOf(record.getTransactionId()));
            dtlLogMap.put("zipcode",record.getZipCode());
            dtlLogMap.put("validationName","VALID_ZIPCODE_CHECK");
            dtlLogMap.put("validationStatus","passed");
            DetailedLogger.log(dtlLogMap);
        }
        return true;
    }
// some more method , but logic / error message /error code will be change
validLastName(UserInputDetails record)
{
}
validateDOB(UserInputDetails record){

}

User details Validation logging

I have written this code for logging, but the code getting replicate for all fields ie last name, DOB,city, state, zipcode etc. Here only validation logic is going to differ, whereas logging functionality will remain the same, and i will be logging both success and error message's.Is there any way to reduce the redundant code ?

public boolean validFirstName(UserInputDetails record) {
        if (CommonUtil.isNullOrEmpty(record.getFirstName())) {
            record.setErrorCode(ErrorCodes.FIRSTNAME_NULL.getErrorCode());
            if(record.isDetailedLoggingEnabled()){
                Map<String,String> dtlLogMap = new HashMap<>(5);
                dtlLogMap.put("transId",String.valueOf(record.getTransactionId()));
                dtlLogMap.put("firstName",record.getFirstName());
                dtlLogMap.put("validationName","VALID_FIRSTNAME_CHECK");
                dtlLogMap.put("validationStatus","Errored");
                dtlLogMap.put("errorCode",record.getErrorCode());
                DetailedLogger.log(dtlLogMap);
            }
            return false;
        }
        
        if ( record.getFirstName().length() >=50) {
            record.setErrorCode(ErrorCodes.FIRSTNAME_INVALID_LENGTH.getErrorCode());
            if(record.isDetailedLoggingEnabled()){
                Map<String,String> dtlLogMap = new HashMap<>(6);
                dtlLogMap.put("transId",String.valueOf(record.getTransactionId()));
                dtlLogMap.put("firstName",record.getFirstName());
                dtlLogMap.put("firstNameLength",returnStringifNotNull(record.getFirstName().length()));
                dtlLogMap.put("validationName","VALID_FIRSTNAME_LENGTH_CHECK");
                dtlLogMap.put("validationStatus","Errored");
                dtlLogMap.put("errorCode",record.getErrorCode());
                DetailedLogger.log(dtlLogMap);
            }
            return false;
        }
        if(record.isDetailedLoggingEnabled()){
            Map<String,String> dtlLogMap = new HashMap<>(4);
            dtlLogMap.put("transId",String.valueOf(record.getTransactionId()));
            dtlLogMap.put("firstName",record.getFirstName());
            dtlLogMap.put("validationName","VALID_FIRSTNAME_CHECK");
            dtlLogMap.put("validationStatus","passed");
            DetailedLogger.log(dtlLogMap);
        }
        return true;
    }

public boolean validZipCode(UserInputDetails record) {
        if (CommonUtil.isNullOrEmpty(record.getZipCode())) {
            record.setErrorCode(ErrorCodes.ZIPCODE_NULL.getErrorCode());
            if(record.isDetailedLoggingEnabled()){
                Map<String,String> dtlLogMap = new HashMap<>(5);
                dtlLogMap.put("transId",String.valueOf(record.getTransactionId()));
                dtlLogMap.put("zipcode",record.getZipCode());
                dtlLogMap.put("validationName","VALID_ZIPCODE_CHECK");
                dtlLogMap.put("validationStatus","Errored");
                dtlLogMap.put("errorCode",record.getErrorCode());
                DetailedLogger.log(dtlLogMap);
            }
            return false;
        }
        
        if ( record.getZipCode().length() >=5) {
            record.setErrorCode(ErrorCodes.ZIPCODE_INVALID_LENGTH.getErrorCode());
            if(record.isDetailedLoggingEnabled()){
                Map<String,String> dtlLogMap = new HashMap<>(6);
                dtlLogMap.put("transId",String.valueOf(record.getTransactionId()));
                dtlLogMap.put("zipcode",record.getZipCode());
                dtlLogMap.put("zipcodeLength",returnStringifNotNull(record.getZipCode().length()));
                dtlLogMap.put("validationName","VALID_ZIPCODE_LENGTH_CHECK");
                dtlLogMap.put("validationStatus","Errored");
                dtlLogMap.put("errorCode",record.getErrorCode());
                DetailedLogger.log(dtlLogMap);
            }
            return false;
        }
        if(record.isDetailedLoggingEnabled()){
            Map<String,String> dtlLogMap = new HashMap<>(4);
            dtlLogMap.put("transId",String.valueOf(record.getTransactionId()));
            dtlLogMap.put("zipcode",record.getZipCode());
            dtlLogMap.put("validationName","VALID_ZIPCODE_CHECK");
            dtlLogMap.put("validationStatus","passed");
            DetailedLogger.log(dtlLogMap);
        }
        return true;
    }
// some more method , but logic / error message /error code will be change
validLastName(UserInputDetails record)
{
}
validateDOB(UserInputDetails record){

}

User details logging

I have written this logic for logging, but is getting replication for all fields (last name, DOB,city, state, ZIP, etc). Here only validation logic is going to differ, whereas logging functionality will remain the same.

public boolean validFirstName(UserInputDetails record) {
        if (CommonUtil.isNullOrEmpty(record.getFirstName())) {
            record.setErrorCode(ErrorCodes.FIRSTNAME_NULL.getErrorCode());
            if(record.isDetailedLoggingEnabled()){
                Map<String,String> dtlLogMap = new HashMap<>(5);
                dtlLogMap.put("transId",String.valueOf(record.getTransactionId()));
                dtlLogMap.put("firstName",record.getFirstName());
                dtlLogMap.put("validationName","VALID_FIRSTNAME_CHECK");
                dtlLogMap.put("validationStatus","Errored");
                dtlLogMap.put("errorCode",record.getErrorCode());
                DetailedLogger.log(dtlLogMap);
            }
            return false;
        }
        
        if ( record.getFirstName().length() >=50) {
            record.setErrorCode(ErrorCodes.FIRSTNAME_INVALID_LENGTH.getErrorCode());
            if(record.isDetailedLoggingEnabled()){
                Map<String,String> dtlLogMap = new HashMap<>(6);
                dtlLogMap.put("transId",String.valueOf(record.getTransactionId()));
                dtlLogMap.put("firstName",record.getFirstName());
                dtlLogMap.put("firstNameLength",returnStringifNotNull(record.getFirstName().length()));
                dtlLogMap.put("validationName","VALID_FIRSTNAME_LENGTH_CHECK");
                dtlLogMap.put("validationStatus","Errored");
                dtlLogMap.put("errorCode",record.getErrorCode());
                DetailedLogger.log(dtlLogMap);
            }
            return false;
        }
        if(record.isDetailedLoggingEnabled()){
            Map<String,String> dtlLogMap = new HashMap<>(4);
            dtlLogMap.put("transId",String.valueOf(record.getTransactionId()));
            dtlLogMap.put("firstName",record.getFirstName());
            dtlLogMap.put("validationName","VALID_FIRSTNAME_CHECK");
            dtlLogMap.put("validationStatus","passed");
            DetailedLogger.log(dtlLogMap);
        }
        return true;
    }
added 101 characters in body; edited title
Source Link
Vasanth
  • 173
  • 7

User details Validation logging

I have written this logiccode for logging, but isthe code getting replicationreplicate for all fields (lastie last name, DOB,city, state, ZIP,zipcode etc). Here only validation logic is going to differ, whereas logging functionality will remain the same, and i will be logging both success and error message's.Is there any way to reduce the redundant code ?

User details logging

I have written this logic for logging, but is getting replication for all fields (last name, DOB,city, state, ZIP, etc). Here only validation logic is going to differ, whereas logging functionality will remain the same.

User details Validation logging

I have written this code for logging, but the code getting replicate for all fields ie last name, DOB,city, state, zipcode etc. Here only validation logic is going to differ, whereas logging functionality will remain the same, and i will be logging both success and error message's.Is there any way to reduce the redundant code ?

[Edit removed during grace period]
Source Link
Vasanth
  • 173
  • 7
added 2197 characters in body
Source Link
Vasanth
  • 173
  • 7
Loading
edited body
Source Link
Vasanth
  • 173
  • 7
Loading
deleted 132 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
Loading
Source Link
Vasanth
  • 173
  • 7
Loading