Skip to content

Instantly share code, notes, and snippets.

@adityagodbole
Last active May 20, 2016 20:40
Verbose java?

One of the pet FUD items of Go programmers when it comes to Java bashing is the verbosity of the language.

Here is a real world comparison. This is function that takes the name of a text file which has one number on each line and returns an array of long/uint64 numbers, or raises exception/returns error on error.

    private static Long[] getNumbers(String filename) throws IOException {
            final Stream<String> lines = Files.lines(Paths.get(filename));
            return lines.map(Long::parseLong).toArray(Long[]::new);
    }
func getNumbers(name string) ([]uint64, error) {
	rFile, err := os.Open(name)
	if err != nil {
		return nil, err
	}
	scanner := bufio.NewScanner(rFile)
	nums := []uint64{}
	for scanner.Scan() {
		s := scanner.Text()
		val, err := strconv.ParseUint(s, 10, 64)
		if err != nil {
			return nil, err
		}
		nums = append(nums, val)
	}
	if err := scanner.Err(); err != nil {
		return nil, err
	}
	return nums, nil
}

So, which one is verbose and more error prone?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment