How can I make this repeating code look neater

stitch123

EDIT: Forgot to mention that I'm on a constrained platform so I'm trying to use as little additional variables as possible.

...
switch (var->type) {
    case (something1):
        if (var->x >= 100 && var->x <= 105) {
            do_something(var);
            return;
        }
        break;

    case (something2):
        if (var->x >= 150 && var->x <= 155) {
            do_something(var);
            return;
        }
        break;

    case (something3):
        if (var->y >= 80 && var->y <= 85) {
            do_something(var);
            return;
        }
        break;

    case (something4):
        if (var->y >= 120 && var->y <= 125) {
            do_something(var);
            return;
        }
        break;
}
... 

Basically the code just checks the type of the variable, checks range on an axis specific to that type, and calls a function if the condition is met, does nothing if not. do_something() is the exact same function within all of the if segments.

I'm sure there's an easy way to write this with less repetition, but I can't seem to figure it out.

Thanks in advance.

Lee Daniel Crocker

How about:

switch (var->type) {
case (something1):
    if (! (var->x >= 100 && var->x <= 105)) return;
    break;
case (something2):
    if (! (var->x >= 150 && var->x <= 155)) return;
    break;
case (something3):
    if (! (var->y >= 80 && var->y <= 85)) return;
    break;
case (something4):
    if (! (var->y >= 120 && var->y <= 125)) return;
    break;
default:
    return;
}
do_something();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how can I make a detailed data.table neater?

How can I organize my code so that it's neater?

How can I organize my code so that it's neater?

How do I make a query shorter and neater?

Angularjs: how can I make a service code "look synchronous"?

How can I add a method or make this code so that I am not repeating myself so many times?

I want to know how can is shorten this code and make it look more proper

How can I make my code look like the screenshot? http://prntscr.com/pqrax8

How do I optimize and make my code look easier

How can I make my slideshow repeating without JS?

How can I make my TextField look like the following (FLUTTER)

How can I make a cell and its elements look greyed out?

How can I make my bottom sheet look like this?

How can I make my error pages look more friendly?

How can I make Gimp look more like Photoshop

How can I make this query not look for partials in length?

How can I make yEd look more integrated into Ubuntu?

How can I make Switch/Case only look at specific breakpoint?

How can I make many jQuery ajax calls look pretty?

How can I make the "Look In" field in a JFileChooser editable on WIndows?

How can I make my uitextfield look like it is caving in?

How can I make Palatino look good in Firefox?

How can I make my menu look like a nested list

How can I make my Ubuntu 12.04 look like a Mac?

How can I make this cover look like a book?

how can i make trisquel look like gnome 3?

How can I make fonts look like they do in Windows?

How can I make these cards look more professionally designed?

how can I make the output look like a table?