Skip to main content
deleted 14 characters in body
Source Link
Pimgd
  • 22.6k
  • 5
  • 68
  • 144

Your approach is mostly correct, but it has the fallacies of encompassing the entire logic and gives no meaningful names/explanations to the flow, variables etc. a more robust, extensible, clear and maintainable solution would be to replace all constants with final variables and encapsulate small problem domains into their own methods. For exmaple:

   public static final String PLUS = "+";
   public static final String MINUS = "-";

   ...

   if (isStudentNameLine(String line)) {
        processStudentNameLine(String line);
   } else {
   ...

   private static boolean isStudentNameLine(String line) {
       return !line.startsWith(PLUS) && !line.startsWith(MINUS);
   }

   private static void processStudentNameLine(String line) {
        System.out.print(line);
   }

This may seem like an overkill to replace one lines of codes with methods. There are many advantages to this approach:

  1. using variables instead of constants mean you dont have to deal with typos
  2. smaller methods are easier to debug
  3. the code becomes clearer

Your approach is mostly correct, but it has the fallacies of encompassing the entire logic and gives no meaningful names/explanations to the flow, variables etc. a more robust, extensible, clear and maintainable solution would be to replace all constants with final variables and encapsulate small problem domains into their own methods. For exmaple:

   public static final String PLUS = "+";
   public static final String MINUS = "-";

   ...

   if (isStudentNameLine(String line)) {
        processStudentNameLine(String line);
   } else {
   ...

   private static boolean isStudentNameLine(String line) {
       return !line.startsWith(PLUS) && !line.startsWith(MINUS);
   }

   private static void processStudentNameLine(String line) {
        System.out.print(line);
   }

This may seem like an overkill to replace one lines of codes with methods. There are many advantages to this approach:

  1. using variables instead of constants mean you dont have to deal with typos
  2. smaller methods are easier to debug
  3. the code becomes clearer

Your approach is mostly correct, but it has the fallacies of encompassing the entire logic and gives no meaningful names/explanations to the flow, variables etc. a more robust, extensible, clear and maintainable solution would be to replace all constants with final variables and encapsulate small problem domains into their own methods. For exmaple:

   public static final String PLUS = "+";
   public static final String MINUS = "-";

   ...

   if (isStudentNameLine(line)) {
        processStudentNameLine(line);
   } else {
   ...

   private static boolean isStudentNameLine(String line) {
       return !line.startsWith(PLUS) && !line.startsWith(MINUS);
   }

   private static void processStudentNameLine(String line) {
        System.out.print(line);
   }

This may seem like an overkill to replace one lines of codes with methods. There are many advantages to this approach:

  1. using variables instead of constants mean you dont have to deal with typos
  2. smaller methods are easier to debug
  3. the code becomes clearer
Source Link

Your approach is mostly correct, but it has the fallacies of encompassing the entire logic and gives no meaningful names/explanations to the flow, variables etc. a more robust, extensible, clear and maintainable solution would be to replace all constants with final variables and encapsulate small problem domains into their own methods. For exmaple:

   public static final String PLUS = "+";
   public static final String MINUS = "-";

   ...

   if (isStudentNameLine(String line)) {
        processStudentNameLine(String line);
   } else {
   ...

   private static boolean isStudentNameLine(String line) {
       return !line.startsWith(PLUS) && !line.startsWith(MINUS);
   }

   private static void processStudentNameLine(String line) {
        System.out.print(line);
   }

This may seem like an overkill to replace one lines of codes with methods. There are many advantages to this approach:

  1. using variables instead of constants mean you dont have to deal with typos
  2. smaller methods are easier to debug
  3. the code becomes clearer