Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leap: implement basis #69

Open
bitfield opened this issue Dec 19, 2018 · 0 comments
Open

Leap: implement basis #69

bitfield opened this issue Dec 19, 2018 · 0 comments
Labels
enhancement New feature or request

Comments

@bitfield
Copy link
Contributor

This is a common side exercise, probably because it looks (and is) pretty simple. Some people even attempt this before Two Fer.

Every solution, without exception, looks like this:

func IsLeapYear(year int) bool {
	return (year%4 == 0 && year%100 != 0) || year%400 == 0
}

(or some equivalent Boolean transformation). Fine as far as it goes, but there's a missed opportunity to make this code clear, simple, and readable, in a way that matches the English description of the problem. Something along these lines:

func IsLeapYear(year int) bool {
	if year%4 != 0 {
		return false
	}
	if year%400 == 0 {
		return true
	}
	if year%100 == 0 {
		return false
	}
	return true
}

(Not saying this is the perfect solution, but it illustrates what I'm talking about.)

@bitfield bitfield added the enhancement New feature or request label Dec 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant