Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 14 15 [16] 17 18 ... 91

Author Topic: Programming Help Thread (For Dummies)  (Read 100615 times)

supermalparit

  • Bay Watcher
    • View Profile
    • Towns devblog
Re: Programming Help Thread (For Dummies)
« Reply #225 on: August 02, 2011, 01:28:31 am »

If I'm gonna debug it, it's gonna be a whole lot of println, I expect... So, I wonder if you're supposed to leave all that debugging code in when you're finished, or you should go through the tedious job of cleaning the code back up.

Well, you have a few options, it depends if you think that the prints will be useful in the future. Also, you can define a "final static boolean" in your main class and then use it for debugging purposes.

Code: [Select]
public final static boolean DEBUG = true;

....
....

if (MyMainClass.DEBUG) {
   print (....);
}

And, when you have to release the game to the public just change the value to 'false'. I supose that compiler it's smart enough to doesn't put that code in the .class file.

Anyway, I prefer a clean code, but it's just a personal style. :P

Regards
Logged

Starver

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #226 on: August 02, 2011, 01:52:04 am »

If I'm gonna debug it, it's gonna be a whole lot of println, I expect... So, I wonder if you're supposed to leave all that debugging code in when you're finished, or you should go through the tedious job of cleaning the code back up.ime to clean up anyway.
I use three different approaches in my programming and scripting efforts.

1) Absolutely temporary.  As in the likes of print("reached here!\n"); before the segment and die "Got here!\n"; after it (the aforementioned and all following being specifically Perl examples, but adaptable to any language), which I shuffle through the code across multiple runs with a bit of cutting and pasting, maybe with some variable-state revelation added to the messages, as appropriate to the current locale.

2) Nice-looking semi-permanent.  Setting up print("Reached stage 1: Starting loop with X = $X\n")-type statements in all the most logical locations, which I later comment out, ready to be brought out of retirement if necessary, but probably cull if it ever gets to a publication stage.

3) DEBUG-class statements.  Extend the class-2 statements into print("Reached stage 1: Starting loop with X = $X\n") if $DEBUG>2 and insert the given 'global' variable in the opening/initialising code so that you can run it with different amounts of debug verbosity, according to what value of $DEBUG you are currently running your code with.  This could even be extended to being nudged up and down within particular segments of the code, as well as defined by the tester at run-time by importing a value through a program parameter!  Although you may still want to strip all of these lines (easily searchable-for by the existence of the $DEBUG variable in the code-segment) and any support code before actual publication if it potentially shows so much of the workings that someone happening upon the possibility of "-DEBUGLEVEL=7" as a program option could derive more about the internal workings (or at least unrevealed calculations) than you'd be happy them knowing.

(Ninjaed on the third variety, which is very much the same except that I wasn't demonstrating it through an OO method and mine probably won't compile quite as cleanly without the manual culling I mention. :) )
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #227 on: August 02, 2011, 03:54:41 am »

3) DEBUG-class statements.  Extend the class-2 statements into print("Reached stage 1: Starting loop with X = $X\n") if $DEBUG>2 and insert the given 'global' variable in the opening/initialising code so that you can run it with different amounts of debug verbosity, according to what value of $DEBUG you are currently running your code with.  This could even be extended to being nudged up and down within particular segments of the code, as well as defined by the tester at run-time by importing a value through a program parameter!  Although you may still want to strip all of these lines (easily searchable-for by the existence of the $DEBUG variable in the code-segment) and any support code before actual publication if it potentially shows so much of the workings that someone happening upon the possibility of "-DEBUGLEVEL=7" as a program option could derive more about the internal workings (or at least unrevealed calculations) than you'd be happy them knowing.

(Ninjaed on the third variety, which is very much the same except that I wasn't demonstrating it through an OO method and mine probably won't compile quite as cleanly without the manual culling I mention. :) )
If the language you're using uses a precompiler you could probably write a macro that only inserts the debugging statements into the code if needed. In Common Lisp it'd be something like:


(defmacro debug-format (debug-level &body format-statement)
  "Warning, untested!"
  (when (> debug-setting debug-level)
    '(format ,@format-statement)))


which could for example be called in the following way:
(debug-format 3 t "Starting loop 1 with Tmax = ~a" Tmax)


Alternatively, you could use a similar macro to only execute a block of code if the debug-setting is above a certain level:

(defmacro debug-code (debug-level &body body)
  "Warning, untested!"
  (when (> debug-setting debug-level)
    '(progn ,@body)))

But you could probably write a similar macro for other languages as well.
« Last Edit: August 02, 2011, 05:35:12 am by Virex »
Logged

Starver

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #228 on: August 02, 2011, 04:30:27 am »

(Been a while (1993, IIRC) since I used Lisp, Common or otherwise, but your examples look about right if I remember the punctuation uses correctly.)

But you could probably write a similar macro for other languages as well.

Yep.  Perl (having used that in my above examples, in that case for readability) also has nice meta-code capabilities, e.g. using iterators, and I've done similar in some versions of my self-named "Class-3" debugging efforts (bigger projects whose code is intended for an reviewing audience and specifically needs to leave in whatever debug code remains during validation) but I'm not sure if the general public is ready for the mess of punctuation that would mean.  :)
Logged

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #229 on: August 02, 2011, 10:29:50 am »

Curiously, not three days ago I read about macros and precompiler in Java which I'm using, and the impression I've retained is that Java either has none of those or they are unreachable/undoable, and only can be faked via some convoluted route. That is, C++ style manipulations with them.

Well, I managed to solve that problem, but almost immediately got a non-critical exception which I haven't been able to reproduce in further runs... I wonder if I should pursue it, or other programmers write such things off as side-effects of the Heisenberg's uncertainty principle.
Logged

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: Programming Help Thread (For Dummies)
« Reply #230 on: August 02, 2011, 10:43:42 am »

Supermikhail: what environment are you using? Its been a while since I did c++ and when I did i used vi and gcc, but MS Visual Studio has a good debugger, and I hear that Eclipse for c++ does too (Eclipse for java definitely does).



Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

Normandy

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #231 on: August 02, 2011, 12:29:31 pm »

There's no need for printlns or other cruft if you've got a good debugger. Just set a few breakpoints and run through the debugger.
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #232 on: August 02, 2011, 01:40:09 pm »

There's no need for printlns or other cruft if you've got a good debugger. Just set a few breakpoints and run through the debugger.
Breakpoints are a pain in the donkey if you've got a large amount of cases to check though (unit testing anyone?). In such a case it's much easier and informative to get a little rapport of every case that went right and wrong. But if you've got one single spot where things absolutely go fubar, then yeah, a breakpoint is going to be of more use then normal output.
« Last Edit: August 02, 2011, 01:41:40 pm by Virex »
Logged

Mephisto

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #233 on: August 02, 2011, 09:57:46 pm »

There's no need for printlns or other cruft if you've got a good debugger. Just set a few breakpoints and run through the debugger.
Breakpoints are a pain in the donkey if you've got a large amount of cases to check though (unit testing anyone?). In such a case it's much easier and informative to get a little rapport of every case that went right and wrong. But if you've got one single spot where things absolutely go fubar, then yeah, a breakpoint is going to be of more use then normal output.

Unit testing! Every language I've seriously used has some form of official unit testing. Specifically, Java, C#, Python, and Ruby all have pretty much the exact same framework. Likely a bunch of others use the same format.

If you write extensive unit tests and they all pass, it's more likely that your program will work when all of the little bits are added together. If you modify something and a test case breaks, you know what's wrong, or at least near where the wrong thing lies in code.
Logged

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #234 on: August 02, 2011, 11:06:20 pm »

Supermikhail: what environment are you using? Its been a while since I did c++ and when I did i used vi and gcc, but MS Visual Studio has a good debugger, and I hear that Eclipse for c++ does too (Eclipse for java definitely does).
I'm using Eclipse (with Java, as previously mentioned), and upon further consideration, I'd say I was right to go with printlns - everything I needed was visible at the bottom of the screen, plus I only needed to test the final step in a long chain of repeating steps, and I imagine with breakpoints it would have involved... well, having breaks at each step.

But at the same time I only needed to test one unit... Maybe it's all just 'cause I don't know anything better. Something new to look forward to, but not before I finish this HTML book. By the way, I'm getting the impression that BBCode is somewhat like an outdated version of HTML, but don't know what to make of it. I only know it feels weird when I have to use things here that I have just read to be obsolete or deprecated.
Logged

Starver

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #235 on: August 03, 2011, 01:43:09 am »

Unit testing! Every language I've seriously If you write extensive unit tests and they all pass, it's more likely that your program will work when all of the little bits are added together. If you modify something and a test case breaks, you know what's wrong, or at least near where the wrong thing lies in code.

Not that I'm saying that it applies here, but when I wrote my Rubik's Cube Emulator for the BBC model B computer at college (yes, that would place it firmly in the '80s, for you history buffs!) I wrote it in sections.  The display procedures, in one, the slice-moving procedures in another, the human interface in another, code for storing user-defined procedures[1], and and I'm sure there were at least two more (but can't work out what they were).  Having written them (inclusive of some test-lines) them all in BASIC (yeah, yeah, but the alternative was assembler[2]) I then had the challenge of putting them altogether.  I found I could effectively "export" a listing to tape[3] and then re-import it through the CLI as if I was re-typing it all in again, by hand.  (Just had to ensure that I re-shuffled the line-numbers so they didn't overwrite each other.  But that was what the RENUMBER command was made for, so no problems there...)

Went swimmingly, until....  Error, error, error...  Memory full.  Ah, the limitations of 32Kb of RAM trying to containing (probably, if my memory serves, only in the lower half of it!) the huge amount of to-be-interpreted code in (YMMV) human-readable form.  Poor little thing just wasn't capable of it.

The obvious solutions (mentioned in the project report) were to have perhaps written significant chunks of the code in Assembler or it might well have fit into a new-fangled BBC Master's memory space (128kb) with almost no core code re-writing necessary.  I'm sure, now I come to think of it, I could also have introduced code optimisations.  Perhaps generated the "Net" and "Perspective" display of the cube procedurally, rather than storing the line and square coordinates in a set of DATA lines, although not sure what I could have done with the sequence-storage bits, I was rather proud of how I handled that, as I recall... though who knows what I'd think of it these days if I saw it!

Anyway, I submitted the exercise anyway, and mentioned the obvious solutions in the write-up, and whether that helped or harmed my grades I can't tell, but it couldn't have done too much damage given the grade I got...


Erm, yeah.  Sorry, that looks more self-aggrandising than I had intended[4].  You almost certainly won't break the memory limits, although watch out for "merging errors" of other kinds, is what I really meant to allude to when I started this post...  Perhaps that should be the tl;dr; message you take away from this.



[1] The interface was text-entry, so type in R+ to twist the right-hand side, so I was allowing the user to store a number of their own sequences.  Which reflects, pretty much, how I personally solve the Cube.

[2] Which would have probably avoided the main problem I eventually encountered, so maybe I should have done, in hindesight

[3] C90 tapes.  Or C45s or C30s.  I wonder how many Mb... possibly even Kb... they would have stored?  Probably easy to work out from the baud rate.

[4] Perhaps I should have instead told you how I absolutely hated the language Ada, instead.  Designed (or at least taught to us) specifically with modular design and testing in mind.  But maybe I'd just been burnt by my prior experiences, because I absolutely detested its format.  Never had any problem with Pascal (which at least became useful, later on with a bit of OO knowledge add, in getting to grips with Delphi), which I learnt in-between the two others and was my first experience of a proper compiled higher-level language, but then again there was more of a "try it and see!" philosophy in those lessons.  Don't talk to me about COBOL, though...  Wasn't even my class, I was just helping someone else out who wasn't that good at using computer keyboards.  Sometimes wish I'd kept it up, though, and found a way to monetise the knowledge in the whole Y2K run-up.  Until I remember how godawful it was.  Which is perhaps the point.
Logged

Starver

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #236 on: August 03, 2011, 02:06:21 am »

By the way, I'm getting the impression that BBCode is somewhat like an outdated version of HTML, but don't know what to make of it. I only know it feels weird when I have to use things here that I have just read to be obsolete or deprecated.

This a bit off-topic (so I didn't add it to my previous post, which may have been off-topic in other ways, in hindsight), but I'm pretty sure that BBCode is supposed to be a subset of what could be displayed in HTML (and in a form significantly different so that anti-HTML handling doesn't get confused over what's allowed, what should be stripped and what should be enclosed by < and > for literal display.

(Actually, last time I looked at the Glow button, it didn't do anything on my screen.  I'm not advocating any <BLINK>-tag equivalent, thankyouNetscape, but I'm wondering if it's working.  Certainly wasn't (last time I checked) on Firefox.  Let's see if it works this time.  Nope.  How about Shadow.  Nope, not that either.  Must check the raw HTML/CSS for what they think they're trying to achieve.  Later.)

By deprecated, are you specifically meaning the [B(old)] and [I(talics)] tags, as opposed to <str(ong)> and <em(phasis)> ones, among others?  Actually, IIRC, there's nothing to say that a browser should make Strong text appear Bold or display Emphasised text Italicised, although it's as good a convention as anything.  Without looking at the source of a page here, I don't know if the BBCode versions get translated to the given HTML, or whether it uses .css to actually make it officially of the stated form (maybe I should check this post, once it's completed), but either way could satisfy the "form and function" separation, depending on how you view it.

And I know that I tend to want to emphasise things by italics and make them stand out strongly by bold, even given how I know this may or may not be strictly accurate.  Still, it's such all-pervading belief that it is that I'm betting most screen-readers will deal with it appropriately, and anything else that analyses it for the purposes of anything other than direct human-readable output (e.g. the sense-interpreters that the likes of Google are probably employing, and which may be the only things on the net that can read my own textual output without getting to the tl;dr; stage!).


Anyway, also there's the point of further keeping it simple and verifiable.  So that URLs can't be so misformed (at least not without intention and the possibility of moderator checks) and have an "easy" and a "complex" behaviour.  Just like Wiki-code has a [[literal]] internal link and one which can be [[not_so_literal|otherwise phrased]].
Logged

Mephisto

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #237 on: August 03, 2011, 05:55:05 am »

[4] Perhaps I should have instead told you how I absolutely hated the language Ada, instead.  Designed (or at least taught to us) specifically with modular design and testing in mind.  But maybe I'd just been burnt by my prior experiences, because I absolutely detested its format.  Never had any problem with Pascal (which at least became useful, later on with a bit of OO knowledge add, in getting to grips with Delphi), which I learnt in-between the two others and was my first experience of a proper compiled higher-level language, but then again there was more of a "try it and see!" philosophy in those lessons.  Don't talk to me about COBOL, though...  Wasn't even my class, I was just helping someone else out who wasn't that good at using computer keyboards.  Sometimes wish I'd kept it up, though, and found a way to monetise the knowledge in the whole Y2K run-up.  Until I remember how godawful it was.  Which is perhaps the point.

Bwa ha ha ha! Ada, my first language. I hated it.
Logged

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: Programming Help Thread (For Dummies)
« Reply #238 on: August 03, 2011, 10:48:57 am »

Supermikhail: Re BBCode

BBCode is actually newer than html. BBCode is an alternative to allowing raw html input in forum software. It is dangerous to allow raw unfiltered html, it allows all kinds of injection and hijacking attacks against users. Fortunately it is very easy to strip html out of an input. BBCode allows people to allow a limited and safe set of markup.

The glow and shadow tags for instance create spans that are styled with text-shadow css. It works just fine on firefox 3.6 and just about any of the latest version of browsers.
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

Starver

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #239 on: August 04, 2011, 03:39:08 am »

Supermikhail: Re BBCode

BBCode is actually newer than html. BBCode is an alternative to allowing raw html input in forum software. It is dangerous to allow raw unfiltered html, it allows all kinds of injection and hijacking attacks against users. Fortunately it is very easy to strip html out of an input. BBCode allows people to allow a limited and safe set of markup.
Better said than how I put it.  (Less words, in particular.  More concise.  My failing.)

Quote
The glow and shadow tags for instance create spans that are styled with text-shadow css. It works just fine on firefox 3.6 and just about any of the latest version of browsers.
Not 3.0.7, though, I can report.  Unless its my settings, which seem to run .css ok and in similar ways, cross-browser, in the likes of self-authored pages.  (Really, I don't think I've missed anything from it not rendering, though. :)((Just checked, and IE8 likes it.  If the effects actually looked Ok I might consider upgrading my portable-installed browser, but right now not worth it. :)) ))
« Last Edit: August 04, 2011, 03:43:36 am by Starver »
Logged
Pages: 1 ... 14 15 [16] 17 18 ... 91