Skip to content

Leap: implement basis #69

@bitfield

Description

@bitfield

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.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions