Skip to content

Instantly share code, notes, and snippets.

@adityagodbole
Last active May 20, 2016 20:40
Show Gist options
  • Save adityagodbole/3f87f22d5cb1f06efcabae1588cda9df to your computer and use it in GitHub Desktop.
Save adityagodbole/3f87f22d5cb1f06efcabae1588cda9df to your computer and use it in GitHub Desktop.
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