Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coding Efficiency
#21
I'm with you on avoiding the microwave pasta. + 1

Pete
Fake News + Phony Politicians = Real Problems

Reply
#22
(08-15-2024, 06:06 PM)justsomeguy Wrote: You know I always want write clean, efficent code, but my muddled brain always ends making thing way more complicated and I feature creep.

I start out thinking about doing this:
Code: (Select All)
FUNCTION GetMonth$(month)
    SELECT CASE month
        CASE 1: GetMonth$ = "January"
...
END FUNCTION

I end up doing this. Big Grin

Code: (Select All)
month$ = "JanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember."
DO
INPUT "Input a number between 1 and 12"; n%
i% = 1: m$ = "": DO
  n% = n% + (INSTR("JFMASOND.", MID$(month$, i%, 1)) > 0)
  m$ = _TRIM$(m$ + CHR$((ASC(MID$(month$, i%, 1)) * -(n% = 0)) + (32 * -(n% <> 0))))
i% = i% + 1: LOOP WHILE i% < LEN(month$) AND n% >= 0
PRINT m$
LOOP
LOL, that's a perfect example of the "clever" code I spoke of earlier. It's beautiful code in its own strange way but come back to it a year later and it looks like an alien took a dump in the source, especially if every single stage of it is not documented well.

Please don't think I'm making fun of your code. Just a perfect example of how some of our "feature creepy" minds work. Smile
There are two ways to write error-free programs; only the third one works.
QB64 Tutorial
Reply
#23
Quote:LOL, that's a perfect example of the "clever" code I spoke of earlier. It's beautiful code in its own strange way but come back to it a year later and it looks like an alien took a dump in the source, especially if every single stage of it is not documented well.

Please don't think I'm making fun of your code. Just a perfect example of how some of our "feature creepy" minds work. Smile
It was written as a joke and kinda as a challenge, feel free to make fun of it all you want.

Its fun to challenge yourself to code something obvious in an unconventional way.
Reply
#24
Here's a good example of coding efficiency which I dug up from the old forums.

Here's the original code:
Code: (Select All)

RANDOMIZE TIMER
WHILE player < 100 AND AI < 100 'pig from Rosetta B+ started 2018-10-16
turn = (turn + 1) MOD 2
di = INT(RND * 6) + 1
PRINT: PRINT "Player:"; player; " AI:"; AI
accum = 0 '<<<<<<<<<<<<<<<<< EDIT one line instead of two
IF turn THEN 'player
DO
IF di = 1 THEN
INPUT "Player you rolled a 1, your turn is over, press enter..."; wate$
EXIT DO
ELSE
accum = accum + di
PRINT "Player you rolled a "; di; "your accumulated total is now "; accum
INPUT "Do you want to (r)oll again or (h)old, Enter r or h > "; choice$
IF choice$ = "r" THEN
di = INT(RND * 6) + 1
ELSE
player = player + accum
EXIT DO
END IF
END IF
LOOP
ELSE
FOR i = 1 TO 5
IF di = 1 THEN
INPUT "AI rolled a 1, it's turn is over, press enter..."; wate$
EXIT FOR
ELSE
accum = accum + di
PRINT "AI rolled a "; di; "it's total accumulated now is"; accum
IF i < 4 AND accum + AI < 100 THEN '<<<<<<<<<<<<<<<<<<<<<<<<<EDIT
PRINT "AI is rolling again."
INPUT "press enter..."; wate$
di = INT(RND * 6) + 1
ELSE
PRINT "AI is holding with"; accum; "added to it's score."
INPUT "press enter..."; wate$
AI = AI + accum
EXIT FOR
END IF
END IF
NEXT
END IF
WEND
IF player > AI THEN PRINT "Player wins!" ELSE PRINT "AI wins!"
END

It's pretty simple to decipher what it's doing, and what type of game it is.

Quote:Yeah, Pig, I found it at Rosetta, I was curious if it was an interesting game.
You get to keep rolling a die and accumulating a score unless you roll a 1 then the accumulated score is lost.
If you don't roll a 1 you have option of holding or rolling again. First to 100 wins.

I have AI programmed to roll 4 times here.

And so, in the quest to produce ever smaller and fewer lines of code, I came up with this monster:
Code: (Select All)
_TITLE "Pig 5 (Rosetta task)" ' B+ started 2018-10-17 Steve McNeill finished 2018-10-19
WHILE player < 100 AND AI < 100
IF Who$ <> " Player" THEN Who$ = " Player" ELSE Who$ = " AI"
FOR i = 1 TO 100
di = INT(RND * 6) + 1
IF i = 1 AND di = 1 THEN accum = 0 ELSE IF i = 1 THEN accum = di ELSE IF di = 1 THEN accum = 0 ELSE accum = accum + di
IF di = 1 THEN PRINT CHR$(10) + Who$ + " rolled a" + RTRIM$(STR$(di)) + CHR$(10) ELSE PRINT CHR$(10) + Who$ + " rolled a" + RTRIM$(STR$(di)) + ", the accumulated total is" + STR$(accum) + CHR$(10);
IF di = 1 THEN INPUT " The turn is over, press enter...", wate$ ELSE RANDOMIZE TIMER
IF di > 1 AND Who$ = " Player" THEN INPUT " Do you want to (r)oll again or (h)old, Enter r or h"; choice$ ELSE IF di > 1 AND (i < 3 AND accum + AI < 100) THEN INPUT " AI is rolling again, press enter... ", wate$
IF di > 1 AND Who$ = " Player" AND choice$ <> "r" THEN player = player + accum
IF di > 1 AND Who$ <> " Player" AND (i >= 3 OR accum + AI >= 100) THEN AI = AI + accum
IF di > 1 AND Who$ <> " Player" AND (i >= 3 OR accum + AI >= 100) THEN INPUT " AI is holding so accumulated amount is scored, press enter", wate$
IF (Who$ <> " Player" AND (i >= 3 OR accum + AI >= 100)) OR (Who$ = " Player" AND choice$ <> "r") OR di = 1 THEN EXIT FOR
NEXT
PRINT CHR$(10) + " Player =" + STR$(player) + " AI =" + STR$(AI)
WEND
PRINT Who$;" wins!"

Now, is anyone able to decipher and sort out that obfuscated mess??

Sometimes less is just a mess. Big Grin
Reply
#25
Funniest use of RANDOMIZE TIMER I've ever seen. I mean what kind of hillbilly meth-head would ever code it that way?

Pete  Big Grin
Fake News + Phony Politicians = Real Problems

Reply
#26
(09-11-2024, 06:45 AM)Pete Wrote: Funniest use of RANDOMIZE TIMER I've ever seen. I mean what kind of hillbilly meth-head would ever code it that way?

Pete  Big Grin

One looking for short lines count, without wanting it to take up a line all by itself.  LOL!  

Like I said, it may be a lot few lines of code, but to go back and look at them now..  I had to to do a search to find RANDOMIZE to even know what the heck you were talking about there!  Big Grin

If someone told me to take that second code, and fix it cause there's a glitch in it, I'd have to spend three days now trying to sort out just what the heck it's supposed to do, and WTH did I code it so stupidly!
Reply
#27
@Steve, Nice condensing of game in LOC and without :, colons!, is impressive but...

I don't particular care for scrolling horizontally, so making less lines but longer ones ??? bad trade-off in my book.

But that is style, this thread is about efficiency and this:
   

I am not sure this is more efficient??? I totally agree it is messy as far ease of reading goes.
b = b + ...
Reply
#28
It's Lines-of-Code efficient.  That's what I was mentioning in the first post -- which form of efficiency does one usually code for?  Program efficiency?  Disk space efficiency?  Line count?  Reusability?  Ease of coding/maintaining?  

That's very efficient on line count, but fails as far as everything else goes.  Less is not always better, I don't think.  Wink

(09-11-2024, 01:51 PM)bplus Wrote: I don't particular care for scrolling horizontally, so making less lines but longer ones ??? bad trade-off in my book.

As for that complaint.  Pah!  Use a smaller font, and a wider monitor!  Tongue
Reply
#29
Yeah wider monitor! as soon as I drop cable from my Spectrum bill, should have that in 6 months!
b = b + ...
Reply
#30
Not all of us are lucky enough to have a have a chubby chaser 2000 monitor like Steve. Mine just allows fat ASCII fonts, which is efficient for my eyesight, but doesn't really solve the problem at hand, horizontal scrolling. Big Grin

I think Steve actually added a neat dimension to this conversation. I mean unless your program is completely and forever finished... code for your code still exists, but you ****ing died, how can you consider it holistically efficient if it takes you three days debug it or reacquaint yourself with it? So neat concept, code that is both efficient to run but is also efficient to work with.

+1

Pete
Fake News + Phony Politicians = Real Problems

Reply




Users browsing this thread: 18 Guest(s)