🔒 Closed Coding good practice 1 of many

Status
Not open for further replies.

codyscott

Honorary Poster
As a good practice, huwag ilagay lahat sa MAIN method ang code.
Keep it as simple and readable ang MAIN method.
Use good naming conventions.

Here is one good example.
I-divide ang mga "tasks" as methods.

Maligayang coding sa inyong lahat.

:) cleancode1.webp
 
Heto yung code kung gusto nyong pag-aralan.
It's not perfect and maraming dapat pang gawin but it's running as it is. The idea to get from is:
Follow Java naming convention lalo na sa methods and variables.....and malinis at madaling basahin na MAIN method.
**********************

import java.util.Scanner;

public class MainApp {

static Scanner input = new Scanner(System.in);

public static void main(String[] args){
boolean running = true;
do{
displayWelcome();
int userInput = getInputFromUser();
performSelectedOperation(userInput);
running = askToRunProgramAgain();
}while(running);
}


private static void displayWelcome(){
System.out.println("Welcome to my System.");
System.out.println("******************");
System.out.println("Please choose an operation");
System.out.println("[ 1 ] Addition");
System.out.println("[ 2 ] Multiplication");
System.out.println("[ 3 ] Exit");
}

private static int getInputFromUser() {
int userInput;
input.reset();
userInput = input.nextInt();
return userInput;
}

private static void performSelectedOperation(int numberSelected){
switch(numberSelected){
case 1: System.out.println("You choosed ADDITION");
break;
case 2: System.out.println("You choosed MULTIPLICATION");
break;
case 3: System.out.println("You choosed EXIT");
System.exit(0);
default: System.out.println("Entry not valid");
System.exit(0);
}
}

private static boolean askToRunProgramAgain() {
System.out.println("Run program again? [ Y ] = Yes, [ N ] = No");
input.reset();
String userInput = input.next().toLowerCase().trim();
if(userInput.equals("y")){
return true;
} else {
return false;
}
}

}
 
esse08
static means, puwede mo gamitin DIRECTLY yung method kahit hindi ka mag INSTANTIATE ng MainApp class first.

halimbawa yung method na "private static void displayWelcome()".
since static siya, puwede mo gamitin kaagad:
displayWelcome();

pero kung HINDI static yan, kailangan mo munang:
//mag-instantiate
1) MainApp app = new MainApp();
//then puwede mo ng gamitin yung method
2) app.displayWelcome();

ganon din sa variable, pag naka static, you can directly use it.
pag hindi static, you need to use getter/setter method kung merong available.
 
Also,set these as principles of software engineer:
1. Use top-down
2. Make your code readable
3. Give methods and variables a descriptive name
4. Only public provide access to those methods that are needed to be publicly accessed
5. Kapag may natutunan pa ako, add ko. Hehe
 
Status
Not open for further replies.

About this Thread

  • 7
    Replies
  • 1K
    Views
  • 4
    Participants
Last reply from:
haxxorisme

Online now

Members online
528
Guests online
904
Total visitors
1,432

Forum statistics

Threads
2,275,071
Posts
28,960,437
Members
1,233,568
Latest member
Watchinnm
Back
Top