16 Sep, 2012, wifidi wrote in the 1st comment:
Votes: 0
Return can directly evaluate a statement or chain of statements. I recently learned this from Professor Carlyle Flemming in a Java class at Tarrant County College. In this example, six statements compile 99 bytes smaller:
/*
* True if room is private.
*/
bool Room::is_private() {
int count = people.size();

return
(
(IS_SET (room_flags, ROOM_PRIVATE) && count >= 2) ||
(IS_SET (room_flags, ROOM_SOLITARY) && count >= 1)
)
;
}

/*
* True if room is dark.
*/
bool Room::is_dark ()
{
return
(
(light <= 0) ||
(IS_SET (room_flags, ROOM_DARK)) ||
((g_world->is_dark()) && !(sector_type == SECT_INSIDE || sector_type == SECT_CITY))
)
;
}


Of course the visual help of using parentheses like braces is optional. Unifying statements is sometimes difficult or time consuming for the average programmer and in my case wouldn't exist without having seen the English version. If text MUDs never outpace computer performance, faster and smaller logically equivalent programs can't be more important than how clear the code is to the programmers who use it. If you can think like a circuit board from the get go, congratulations, you're closer to being like the machines you are programming. However it's imaginable logic outlays can become so hard to follow the programmer prefers code that compiles, works and is comprehended. I recommend an introduction to logic class in philosophy to beginning programmers. In the end, there's right for what the programmer is thinking as well as for the way something should be, and in an ideal world those two things are the same.

edit by kiasyn: code tags
16 Sep, 2012, wifidi wrote in the 2nd comment:
Votes: 0
Whoops! This is correct in stock Murk++:
/*
* True if room is dark.
*/
bool Room::is_dark ()
{
return
(
((light == 0) && (g_world->is_dark()) && !(sector_type == SECT_INSIDE || sector_type == SECT_CITY))
// no light sources are in the room, it's dark out and the room is not an inside or city sector
||
IS_SET (room_flags, ROOM_DARK)
// the builder designates this room is dark
)
;
}

…because the Sun is programmed by time of day rather than the room variable 'light'. I'm too late to edit the original post. Funny I spiffed up the original post with the time I had to edit it without having the logic right. Making the Sun count toward the Room variable 'light' makes sense, yet is potentially a first step toward a lot of work, e.g. windows, doors, characters or mobiles blocking them, fog, etc.
17 Sep, 2012, Tyche wrote in the 3rd comment:
Votes: 0
char * ch = "hello";
int a(char * ch) {
if ((ch[1] == 'e') && !(ch[4] == 'q'))
return 1;
return 0;
}
int b(char * ch) {
return ((ch[1] == 'e') && !(ch[4] == 'q')) ;
}

int main() {
a(ch);
b(ch);
return 0;
}


(gdb) disass a
Dump of assembler code for function a:
0x00401170 <+0>: push %ebp
0x00401171 <+1>: mov %esp,%ebp
0x00401173 <+3>: mov 0x8(%ebp),%eax
0x00401176 <+6>: add $0x1,%eax
0x00401179 <+9>: movzbl (%eax),%eax
0x0040117c <+12>: cmp $0x65,%al
0x0040117e <+14>: jne 0x401194 <a+36>
0x00401180 <+16>: mov 0x8(%ebp),%eax
0x00401183 <+19>: add $0x4,%eax
0x00401186 <+22>: movzbl (%eax),%eax
0x00401189 <+25>: cmp $0x71,%al
0x0040118b <+27>: je 0x401194 <a+36>
0x0040118d <+29>: mov $0x1,%eax
0x00401192 <+34>: jmp 0x401199 <a+41>
0x00401194 <+36>: mov $0x0,%eax
0x00401199 <+41>: pop %ebp
0x0040119a <+42>: ret
End of assembler dump.
(gdb) disass b
Dump of assembler code for function b:
0x0040119b <+0>: push %ebp
0x0040119c <+1>: mov %esp,%ebp
0x0040119e <+3>: mov 0x8(%ebp),%eax
0x004011a1 <+6>: add $0x1,%eax
0x004011a4 <+9>: movzbl (%eax),%eax
0x004011a7 <+12>: cmp $0x65,%al
0x004011a9 <+14>: jne 0x4011bf <b+36>
0x004011ab <+16>: mov 0x8(%ebp),%eax
0x004011ae <+19>: add $0x4,%eax
0x004011b1 <+22>: movzbl (%eax),%eax
0x004011b4 <+25>: cmp $0x71,%al
0x004011b6 <+27>: je 0x4011bf <b+36>
0x004011b8 <+29>: mov $0x1,%eax
0x004011bd <+34>: jmp 0x4011c4 <b+41>
0x004011bf <+36>: mov $0x0,%eax
0x004011c4 <+41>: pop %ebp
0x004011c5 <+42>: ret
18 Sep, 2012, Lyanic wrote in the 4th comment:
Votes: 0
Thanks, Tyche. I started to reply to this thread yesterday, but I figured, "Why bother? Someone else will do it for me."
0.0/4