#Many ifs and returns, is there a better way to write this?

1 messages · Page 1 of 1 (latest)

lilac oak
#
    fn position(self: Window, disp_frame: Frame) Position {
        if (self.isTop(disp_frame)) {
            if (self.isRight(disp_frame)) return .top_right;
            if (self.isLeft(disp_frame)) return .top_left;
            if (self.isFullWidth(disp_frame)) return .top_wide;
            return .top_mid;
        }
        if (self.isBottom(disp_frame)) {
            if (self.isRight(disp_frame)) return .bottom_right;
            if (self.isLeft(disp_frame)) return .bottom_left;
            if (self.isFullWidth(disp_frame)) return .bottom_wide;
            return .bottom_mid;
        }
        if (self.isFullHeight(disp_frame)) {
            if (self.isRight(disp_frame)) return .tall_right;
            if (self.isLeft(disp_frame)) return .tall_left;
            return .full;
        }
        return .other;
    }
ruby gale
#

LGTM

burnt glade
#

but basically a switch with a comptime function that compress it a bit

#

Otherwise this is the best way

lilac oak
#

thanks, at least I'm not doing something bad

burnt glade
#

You and also use and btw

#

but that only if you care about the little extra nesting

#

which is not much

#

and in zig is shortcircuiting

lilac oak
#

I had an and version but it just repeated the first condition a lot

burnt glade
#

Yep

#

that's why I said only if you care about the nesting