Skip to main content
Rollback to Revision 6
Source Link
Simon Forsberg
  • 59.8k
  • 9
  • 160
  • 312
public static void main(String[] args) {

    ultraFizzBuzz((Integer arg) -> {
        String retVal = "";
        if(arg % 3 == 0) { retVal += "fizz"; }
        if(arg % 5 == 0) { retVal += "buzz"; }
        return retVal;
    });
}

public static void ultraFizzBuzz(modConditions<Integer> tester) {
    
    for(int i = 1; i < 100; i++) {
        String result = tester.test(i);
        if(result != "")
            System.out.println(resulttester.test(i));
        else
            System.out.println(i);
    }
}

Am I overcomplicating things? Would this be considered bad programming practice?

EDIT:

Based on suggestions, here is revised code:

static List<Function<Integer, String>> fizzBuzzList = Arrays.asList(
    x -> x % 3 == 0 ? "fizz" : "",
    x -> x % 5 == 0 ? "buzz" : "",
    x -> x % 7 == 0 ? "pop"  : ""
);

public static void main(String[] args) {
    
    fizzBuzz(fizzBuzzList);
}

public static void fizzBuzz(List<Function<Integer, String>> tester) {
    
    for(int i = 1; i <= 100; i++) {
        boolean hasPrinted = false; // will be true if any functions in tester return non-empty string
        
        for(Function<Integer, String> f : tester) {
            String result = f.apply(i);
            
            if(!result.isEmpty()) {
                System.out.print(f.apply(i));
                hasPrinted = true;
            }
        }
        
        if(!hasPrinted) {
            System.out.print(i);
        }
        
        System.out.println();
    }
}

More logic is now based inside of the actual function

public static void main(String[] args) {

    ultraFizzBuzz((Integer arg) -> {
        String retVal = "";
        if(arg % 3 == 0) { retVal += "fizz"; }
        if(arg % 5 == 0) { retVal += "buzz"; }
        return retVal;
    });
}

public static void ultraFizzBuzz(modConditions<Integer> tester) {
    
    for(int i = 1; i < 100; i++) {
        String result = tester.test(i);
        if(result != "")
            System.out.println(result);
        else
            System.out.println(i);
    }
}

Am I overcomplicating things? Would this be considered bad programming practice?

EDIT:

Based on suggestions, here is revised code:

static List<Function<Integer, String>> fizzBuzzList = Arrays.asList(
    x -> x % 3 == 0 ? "fizz" : "",
    x -> x % 5 == 0 ? "buzz" : "",
    x -> x % 7 == 0 ? "pop"  : ""
);

public static void main(String[] args) {
    
    fizzBuzz(fizzBuzzList);
}

public static void fizzBuzz(List<Function<Integer, String>> tester) {
    
    for(int i = 1; i <= 100; i++) {
        boolean hasPrinted = false; // will be true if any functions in tester return non-empty string
        
        for(Function<Integer, String> f : tester) {
            String result = f.apply(i);
            
            if(!result.isEmpty()) {
                System.out.print(f.apply(i));
                hasPrinted = true;
            }
        }
        
        if(!hasPrinted) {
            System.out.print(i);
        }
        
        System.out.println();
    }
}

More logic is now based inside of the actual function

public static void main(String[] args) {

    ultraFizzBuzz((Integer arg) -> {
        String retVal = "";
        if(arg % 3 == 0) { retVal += "fizz"; }
        if(arg % 5 == 0) { retVal += "buzz"; }
        return retVal;
    });
}

public static void ultraFizzBuzz(modConditions<Integer> tester) {
    
    for(int i = 1; i < 100; i++) {
        String result = tester.test(i);
        if(result != "")
            System.out.println(tester.test(i));
        else
            System.out.println(i);
    }
}

Am I overcomplicating things? Would this be considered bad programming practice?

deleted 8 characters in body
Source Link
NaN
  • 205
  • 1
  • 3
public static void main(String[] args) {

    ultraFizzBuzz((Integer arg) -> {
        String retVal = "";
        if(arg % 3 == 0) { retVal += "fizz"; }
        if(arg % 5 == 0) { retVal += "buzz"; }
        return retVal;
    });
}

public static void ultraFizzBuzz(modConditions<Integer> tester) {
    
    for(int i = 1; i < 100; i++) {
        String result = tester.test(i);
        if(result != "")
            System.out.println(tester.test(i)result);
        else
            System.out.println(i);
    }
}
public static void main(String[] args) {

    ultraFizzBuzz((Integer arg) -> {
        String retVal = "";
        if(arg % 3 == 0) { retVal += "fizz"; }
        if(arg % 5 == 0) { retVal += "buzz"; }
        return retVal;
    });
}

public static void ultraFizzBuzz(modConditions<Integer> tester) {
    
    for(int i = 1; i < 100; i++) {
        String result = tester.test(i);
        if(result != "")
            System.out.println(tester.test(i));
        else
            System.out.println(i);
    }
}
public static void main(String[] args) {

    ultraFizzBuzz((Integer arg) -> {
        String retVal = "";
        if(arg % 3 == 0) { retVal += "fizz"; }
        if(arg % 5 == 0) { retVal += "buzz"; }
        return retVal;
    });
}

public static void ultraFizzBuzz(modConditions<Integer> tester) {
    
    for(int i = 1; i < 100; i++) {
        String result = tester.test(i);
        if(result != "")
            System.out.println(result);
        else
            System.out.println(i);
    }
}
added revised code
Source Link
NaN
  • 205
  • 1
  • 3

EDIT:

Based on suggestions, here is revised code:

static List<Function<Integer, String>> fizzBuzzList = Arrays.asList(
    x -> x % 3 == 0 ? "fizz" : "",
    x -> x % 5 == 0 ? "buzz" : "",
    x -> x % 7 == 0 ? "pop"  : ""
);

public static void main(String[] args) {
    
    fizzBuzz(fizzBuzzList);
}

public static void fizzBuzz(List<Function<Integer, String>> tester) {
    
    for(int i = 1; i <= 100; i++) {
        boolean hasPrinted = false; // will be true if any functions in tester return non-empty string
        
        for(Function<Integer, String> f : tester) {
            String result = f.apply(i);
            
            if(!result.isEmpty()) {
                System.out.print(f.apply(i));
                hasPrinted = true;
            }
        }
        
        if(!hasPrinted) {
            System.out.print(i);
        }
        
        System.out.println();
    }
}

More logic is now based inside of the actual function

EDIT:

Based on suggestions, here is revised code:

static List<Function<Integer, String>> fizzBuzzList = Arrays.asList(
    x -> x % 3 == 0 ? "fizz" : "",
    x -> x % 5 == 0 ? "buzz" : "",
    x -> x % 7 == 0 ? "pop"  : ""
);

public static void main(String[] args) {
    
    fizzBuzz(fizzBuzzList);
}

public static void fizzBuzz(List<Function<Integer, String>> tester) {
    
    for(int i = 1; i <= 100; i++) {
        boolean hasPrinted = false; // will be true if any functions in tester return non-empty string
        
        for(Function<Integer, String> f : tester) {
            String result = f.apply(i);
            
            if(!result.isEmpty()) {
                System.out.print(f.apply(i));
                hasPrinted = true;
            }
        }
        
        if(!hasPrinted) {
            System.out.print(i);
        }
        
        System.out.println();
    }
}

More logic is now based inside of the actual function

Post Reopened by 200_success
added 3 characters in body
Source Link
NaN
  • 205
  • 1
  • 3
Loading
added 114 characters in body
Source Link
NaN
  • 205
  • 1
  • 3
Loading
Post Closed as "Not suitable for this site" by 200_success
someone edited my title and made me sound like a narcissist
Link
NaN
  • 205
  • 1
  • 3
Loading
added 51 characters in body
Source Link
NaN
  • 205
  • 1
  • 3
Loading
edited title
Link
200_success
  • 145.6k
  • 22
  • 191
  • 481
Loading
Source Link
NaN
  • 205
  • 1
  • 3
Loading