Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 514 515 [516] 517 518 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 908263 times)

Dutrius

  • Bay Watcher
  • No longer extremely unavailable!
    • View Profile
    • Arcanus Technica
Re: if self.isCoder(): post() #Programming Thread
« Reply #7725 on: August 31, 2015, 03:15:05 pm »

I often attach a comment to any line that contains something temporary that says something like this:
Code: [Select]
string foo = "bar";      //Temp. (brief explanation of why)

Debug statements often say something like:
Code: [Select]
Debug.writeline("Doing something.");
Something();
Debug.writeline("Doing another thing.");
AnotherThing();
Debug.writeline("Done.");
If the log says "Doing something." and not "Doing another thing." then you know it failed at AnotherThing().
Logged
No longer extremely unavailable!
Sig text
ArcTech: Incursus. On hold indefinitely.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7726 on: August 31, 2015, 08:06:48 pm »

Another handy one is to put a debug at the start of functions saying what parameters they were called with,  such as "called <function> with <parameters>" and one at the end, saying "completed <function>" . so you can check whether the parameters look normal, and check that there's a matching "completed" statement. On occasion I've even set up a debug print command that knows how to indent, so the debug log gets indented according to call depth.

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7727 on: September 01, 2015, 03:24:16 pm »

Welp, I just found the strangest Ruby bug I've ever seen.  Only happens in Windows.

Code: [Select]
Dir["Com3.asdfasdf"]

It should return [], but instead it returns ["Com3.asdfasdf"]. 

Anything that starts with Com3, Con, or Nul gives false file reports.  These are all reserved filenames in windows, but also seem to cause this bug when scanning directories in Ruby.

Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

Moghjubar

  • Bay Watcher
  • Science gets you to space.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7728 on: September 01, 2015, 03:54:55 pm »

Thanks Reelya. That feedback was actually really helpful; it's very easy to learn how to program, as in learning the syntax of a language, but it's difficult to learn how to be an effective programmer.

This is what I say: programming is easy, development is tough, and then there's engineering and architecture if you want hard mode.
Logged
Steam ID
Making things in Unity
Current Project: Demon Legend
Made This too (publisher abandoned ) Farworld Pioneers
Mastodon

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7729 on: September 01, 2015, 04:14:26 pm »

Yeah, programming is easy until code you wrote a month suddenly stops making sense.
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7730 on: September 01, 2015, 04:20:01 pm »

yes, when programming avoid this:

// you're not dumb you know what this does

this just leads to a lot of "fuck you past me"

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7731 on: September 01, 2015, 04:35:33 pm »

yes, when programming avoid this:

// you're not dumb you know what this does

this just leads to a lot of "fuck you past me"

Heh, comment lines I wrote in some encryption code that I came across again recently:

Code: [Select]
#WARNING!  DO NOT MESS WITH THIS CODE.  NOT EVEN YOU LEVI!
#If you are ignoring that warning, make sure you understand EXACTLY WHAT and WHY
#each bit does, otherwise I can't guarantee its security. (I won't even guarantee it now)
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

i2amroy

  • Bay Watcher
  • Cats, ruling the world one dwarf at a time
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7732 on: September 01, 2015, 04:42:38 pm »

Reminds me how I was perusing the web code for one of my college's websites recently and saw this gem:
Quote
WARNING: THIS IS A HORRIBLE HACKY WORKAROUND AND I CAN'T FIND WHAT'S WRONG! SOMEONE PLEASE FIX THIS!!
As best as I could tell that particular bit of code has been there for at least 4 years now... in the middle of one of the most commonly used homework submission sites on the campus. :P
Logged
Quote from: PTTG
It would be brutally difficult and probably won't work. In other words, it's absolutely dwarven!
Cataclysm: Dark Days Ahead - A fun zombie survival rougelike that I'm dev-ing for.

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #7733 on: September 01, 2015, 04:55:11 pm »

My old projects are littered with unhelpful comments.

Quote
I'm not sure how this works, but it works, mostly
Quote
This is a dumb way of doing things but changing it makes [completely unrelated module] crash
Quote
Future me, don't look at this or you'll go blind
Quote
This should do X but it doesn't
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Bumber

  • Bay Watcher
  • REMOVE KOBOLD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7734 on: September 01, 2015, 05:16:29 pm »

yes, when programming avoid this:

// you're not dumb you know what this does

this just leads to a lot of "fuck you past me"
CURRENT Putnam [CP] began trolling PAST Putnam [PP]
Logged
Reading his name would trigger it. Thinking of him would trigger it. No other circumstances would trigger it- it was strictly related to the concept of Bill Clinton entering the conscious mind.

THE xTROLL FUR SOCKx RUSE WAS A........... DISTACTION        the carp HAVE the wagon

A wizard has turned you into a wagon. This was inevitable (Y/y)?

Antsan

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7735 on: September 01, 2015, 05:48:17 pm »

Huh, I mostly use comments when presenting code for other people, to work out an interface before actually implementing it (and when that is done, the comment is removed) or split code into sections, but almost never for documenting how something works, what it does or what it is intended to do. If I don't understand something I wrote a while ago I normally try the parts and see what they do.
On the other hand I often rewrite passages I don't understand anymore. Also I work with Common Lisp and Haskell, in which most code is sufficiently self-documenting and trying arbitrary code is trivial. In Poslin I use stepping to find out how something works.

A warning I have seen some times in regards to comments: They are prone to getting out of date if you fix a bug or refactor. Then they aren't only useless but might even cause confusion when you spot a difference between what a comment says and what the code says.
« Last Edit: September 01, 2015, 05:52:15 pm by Antsan »
Logged
Taste my Paci-Fist

LoSboccacc

  • Bay Watcher
  • Σὺν Ἀθηνᾷ καὶ χεῖρα κίνει
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7736 on: September 01, 2015, 06:13:44 pm »

same here, no comments

if the code smells so much you're tempted to comment it, move it to it's own method until it became clear and name the method as you would explain the operation in the comment.



above all thing, all developers should read code complete.
Logged

itisnotlogical

  • Bay Watcher
  • might be dat boi
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7737 on: September 01, 2015, 10:41:44 pm »

I don't believe "self-documenting code" is the be-all end-all. Just because everything is easily readable and makes total sense to you doesn't mean it will be as readable to anybody else that's poking around, or even to yourself a few weeks down the line when you've taken a break. Comments are a tool, just like an IDE or a compiler.
Logged
This game is Curtain Fire Shooting Game.
Girls do their best now and are preparing. Please watch warmly until it is ready.

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7738 on: September 02, 2015, 12:20:35 am »

I used to comment my code as a sort of "distilled" version of the program.  That is, I'd write:

Code: [Select]
// Look up the gateway
api.gateways.find({where: {site_id: req.param('site_id'), production_state: 'production'}}).then(function(data) {
    // Stuff
});

The comment is so much faster and easier to parse than the line after it, even if the line after it is pretty clear if you work with the project very much.

Despite that, I got out of the habit because the comments tended to rot and they didn't add much clarity in the end.  In general, the methods and functions were short enough that you could quickly determine what a block of code did without the comments, and only if the code because particularly obtuse (callbacks or other abnormal control flow) would I add comments.

Or if a library or something had a stupid API that made me jump through too many hoops.  Then I'd write an angry paragraph explaining why I did what I did to get it to work.
Logged
Through pain, I find wisdom.

Antsan

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7739 on: September 02, 2015, 04:13:44 am »

I don't believe "self-documenting code" is the be-all end-all. Just because everything is easily readable and makes total sense to you doesn't mean it will be as readable to anybody else that's poking around, or even to yourself a few weeks down the line when you've taken a break. Comments are a tool, just like an IDE or a compiler.
Self-documenting code isn't the be-all-end-all, you're right about that.
But with Common Lisp/Haskell/any language with a decent Syntax I don't only have code I can actually read comfortably (as opposed to something like what Telgin just posted) but often there are other features which help mitigate the necessity of comments.

Haskell has its types, which function effectively as a part of the specification that can be programmatically checked, so they are never incorrect or out-of-date.

Common Lisp and Haskell both have a REPL (although the one for Haskell is quite incomplete), so if you don't understand what a certain part of the code does, you can just check immediately. Note that this doesn't prevent compilation.

Common Lisp also has macros like TRACE, which allow, well, tracing and untracing of functions with one call, like this (I think I've got the format wrong, but that's hardly important):
Code: [Select]
> (trace foo)
FOO
> (foo 3)
: (FOO 3)
  : (FOO 2)
    : (FOO 1)
    -> 0
  -> 1
-> 2
Common Lisp also has very sophisticated error handling facilities (not only throw and catch but also so-called "restarts", which provide a way of jumping back into the context where the error happened, instead of being stuck with unwound stack of where the catch happened) and documentation strings (which are basically comments, only that they're associated with the function/variable being defined) which can be accessed without looking into the code, so you can check what a certain function does if you know only its name, without needing to search for where it is defined.
Then there are type annotations and probably many more features I forgot. Not to speak how it might be a better practice to write a macro encapsulating an unusual idea so it becomes easily readable than to comment it. The macro shouldn't be documented with a comment, but a documentation string and how it works can be checked with MACROEXPAND in the REPL.

The remaining cases where comments are actually useful are sparse.

One way to prevent comment rot would be to tie comments to specific code lines. I don't know, maybe a program that goes over a source file, making checksums for code and accompanying comments (how would you detect which part of the code a given comment was for? In Common Lisp you'd count leading ';'s and associate with the appropriate source subtree) and then you can check afterwards whether the checksum of the code changes without the comment checksum changing, too.
Logged
Taste my Paci-Fist
Pages: 1 ... 514 515 [516] 517 518 ... 796