Code Optimization PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Code Optimization

For the given code we can optimize few part of this like initialization and click events of
elements should be in a separate function, so it will be easy for reading the code. Secondly
the statement inside onClick method can be converted to if and else statement. Lastly the
processData method contains unnecessary two time conversion of toUpperCase, we can use
“result” variable in if statement to avoid second conversion. "Update after Button Click" text
is unnecessary as we are updating the global data in processData method. So this text will be
unused and we can remove it. We can use lambda function in onClick listener but it will not
have impact on execution time.

The optimized Code:

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

public class MainActivity extends Activity {

private EditText userInput;

private TextView displayText;

private Button actionButton;

private static String globalData = "Initial Data";

private static final String SECRET = "SECRET";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initializeUIAndListners();

}
private void initializeUIAndListners() {

userInput = findViewById(R.id.user_input);

displayText = findViewById(R.id.display_text);

actionButton = findViewById(R.id.action_button);

actionButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String input = userInput.getText().toString();

if (input.isEmpty()) {

displayText.setText("Please enter some text");

} else {

processData(input);

displayText.setText(globalData);

});

private void processData(String data) {

String result = data.toUpperCase();

displayText.setText("Processing: " + result);

if (SECRET.equals(result)) {

globalData = "You discovered the secret!";

} else {

globalData = result;

(Additionally)
Optimization of code is done without changing the logic of code. In the processData the else
statement can be updated to that secret doesn’t match but it’s not in logic so I didn’t change
it.
Business Logic

Introduction
In this application, users can provide input through an input field, triggering a process when
they click the action button. The system then processes the input and provides feedback in
real-time through a TextView. If the user input matches with predefined secret then it will
display the appropriate text.

Description

1. User Input Field


- The `userInput` field is where users provide their input.

2. Action Button
a. Clicking the `actionButton` initiates the data processing sequence.
b. If the input is empty, the application prompts the user to enter text.
c. The system then processes the input and displays a message in real-time in the
`displayText` field, notifying the user that their input is being processed.
d. System will convert string to upper case and match with the SECRET variable which is
constant and if it matches then display “You discovered the secret”
e. If the input does not match the secret, the original input text is displayed in the
`displayText` field.

You might also like