Legacy GM Best way to simplify this code?

Calvert

Member
I'm sure there is a very basic function to simplify this code, I just haven't learned it yet. I have a few hundred numbers to program, so learning this right now is very important to me. I've written a small piece of basic code for an example. The simplified idea is to check if a variable has a value from 48-50, it produces a specific outcome, without having to actually write out all numbers from 48-50!

Code:
if global.light = 50
{
    global.torch = 5;
}
if global.light = 49
{
    global.torch = 5;
}
if global.light = 48
{
    global.torch = 5;
}
Any thoughts?
 
S

Steevo

Guest
Code:
if (global.light >= 48 && global.light <= 50)
  global.torch = 5;
Less lines.
 
S

Steevo

Guest
Also less clear. Blocking your code with brackets is always safer and easier to read. The number of lines used is largely irrelevant.
He was asking for 'simplest' code. Four less keystrokes qualifies a 'simpler'.

How is surrounding a single liner with braces 'safer'?

It's also common practice in most languages to drop the braces if the code after an if, while, for, etc statement is a one liner.

Look at languages like Python. They don't even use braces, only tabbing, yet people have coped with that for decades.
 

NeoShade

Member
There's no guarantee that the one-liner will always remain a one-liner. If you come back to that code in the future and add a line to it without remembering that you need to add braces, you're going to get unexpected results.

Shorter does not always equal simpler. If that were the case, this would be the simplest form:
Code:
if global.l>=48 && global.l<=50 
global.t=5;
It could still achieve the exact same thing, but a lack of descriptive variable names, whitespace and indenting makes it much less simple to read than the previous example.
 
C

CedSharp

Guest
There's no guarantee that the one-liner will always remain a one-liner. If you come back to that code in the future and add a line to it without remembering that you need to add braces, you're going to get unexpected results.

Shorter does not always equal simpler. If that were the case, this would be the simplest form:
Code:
if global.l>=48 && global.l<=50
global.t=5;
It could still achieve the exact same thing, but a lack of descriptive variable names, whitespace and indenting makes it much less simple to read than the previous example.
I disagree, this would be the shortest form:
Code:
if global.l>=48 && glboal.l<=50 global.t=5
I removed the newline and you just saved yet-another-line.

-- jokes aside --

Writing if statements with 1 statement without braces is actually the common way for any c-like languages.

It marks a difference between blocks and simple-statements.

I myself will even keep the actual statement on the same line if it's under 80 characters long,
otherwise I indent it under it.

Code:
// Short, consise, very safe and easy to read
if( global.light >= 48 && global.light <= 50 )
  global.torch = 5;
else if( global.light < 48 ) global.torch = 3;
else {
  global.torch = 2;
  show_debug_message('This should never happen');
}
It's easy to read, fast to type, and you can clearly see the difference
between the multi-statement and the single-statement conditions!

(this is just sharing my own styling, it is by no mean something you "should" do but rather "can" do :p )
 
Last edited:

sylvain_l

Member
shorter and simpler:
Code:
global.torch = 5
without more detail of the context and use of the code, that could do the job too. :D

or at the opposite, to reproduce OP code behaviour:
Code:
switch(global.l)
{
    case 50:
    case 49:
    case 48:
        global.torch = 5;
}
I didn't read anywhere that non integer value of global.l should assign a 5 value to global.torch :)rolleyes:if I forget that OP already acknowledge frostycat solution)
 
FrostyCats solution is the most common. BUT as soon as you have to change some values, it becomes relatively confusing. I would use something like this:

Code:
if (global.light <= 10) {
     global.torch = 2;
} else if (global.light <= 20) {
     global.torch = 3;
} else if (global.light <= 47) {
     global.torch = 4;
} else if (global.light <= 50) {
     global.torch = 5;
}
I think this solution is more elegant and way easier to maintain. But that's certainly a matter of taste.
 

GMWolf

aka fel666
A switch statement would be nice here.
But I would use a data driven approach using an array., Or perhaps a map.
 
Top