Coding: Fleeting Thoughts

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
Rippy
Posts: 2101
Joined: Sun Jul 22, 2007 11:27 pm UTC
Location: Ontario, Can o' Duh

Re: Coding: Fleeting Thoughts

Postby Rippy » Mon Jul 26, 2010 3:42 am UTC

b.i.o wrote:That's pretty cool. What's the algorithm like/is the code up somewhere?

Yeah just let me clean up the embarrassing code and I can upload it, under a WTFPL license of course :).

The algorithm's actually really simple, I learned it here. Basically you start with a randomized grid, and loop n times, each time doubling the width/height and then cycling through each tile, replacing it with a randomly-chosen adjacent tile. I'd explain it more thoroughly, but that article can probably do a better job of it than I can.

Edit: Added a quote due to new-page-ness.

User avatar
b.i.o
Green is the loneliest number
Posts: 2519
Joined: Fri Jul 27, 2007 4:38 pm UTC
Location: Hong Kong

Re: Coding: Fleeting Thoughts

Postby b.i.o » Mon Jul 26, 2010 4:34 am UTC

Aah, okay. I figured it was doing something like that, but I wasn't quite sure how it was ensuring that things stayed reasonably connected.

I've been meaning to do something with <canvas> anyway, and this looks like a fun little thing to implement. (Only a month to go 'till Node Knockout! I really need to get started actually learning more than the basics of JS...)

There are a couple of other cool things in there, too. The procedural generation wiki linked looks like it could be very interesting reading. Making games isn't something that interests me anymore, but procedural generation's pretty cool regardless.

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Coding: Fleeting Thoughts

Postby Berengal » Mon Jul 26, 2010 4:26 pm UTC

What do you call the opposite of dependency injection?
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
Xanthir
My HERO!!!
Posts: 5426
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Mon Jul 26, 2010 4:59 pm UTC

You mean, like, on-the-fly dependency cleanup?
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Coding: Fleeting Thoughts

Postby Berengal » Mon Jul 26, 2010 5:32 pm UTC

No, I mean like fetching connections to hard-coded databases all over the place.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
headprogrammingczar
Posts: 3072
Joined: Mon Oct 22, 2007 5:28 pm UTC
Location: Beaming you up

Re: Coding: Fleeting Thoughts

Postby headprogrammingczar » Mon Jul 26, 2010 7:16 pm UTC

Codethulu? It spreads its fell tentacles to everywhere.
<quintopia> You're not crazy. you're the goddamn headprogrammingspock!
<Weeks> You're the goddamn headprogrammingspock!
<Cheese> I love you

qbg
Posts: 586
Joined: Tue Dec 18, 2007 3:37 pm UTC

Re: Coding: Fleeting Thoughts

Postby qbg » Mon Jul 26, 2010 9:43 pm UTC

Consider the following code from a lazy lisp of mine:

Code: Select all

(def fibs (append '(0 1) (zip + fibs (rest fibs))))

(nth fibs 5000)

Hopefully it should be obvious what it does.

zip presents an interesting question: how lazy should it be? Should it reduce the value being supplied to the function or not? If you don't then the call to nth will fail with a stack overflow exception as it has to evaluate a very deep expression. If you do reduce it, then it isn't fully lazy.

How do languages like Haskell handle this?

User avatar
Xanthir
My HERO!!!
Posts: 5426
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Mon Jul 26, 2010 10:05 pm UTC

Berengal wrote:No, I mean like fetching connections to hard-coded databases all over the place.

Oh god, opposite in the other axis. Wow.

Um, dependency cancer - it's spread to all parts of the code body.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Coding: Fleeting Thoughts

Postby Berengal » Mon Jul 26, 2010 10:52 pm UTC

headprogrammingczar wrote:Codethulu? It spreads its fell tentacles to everywhere.

I like it. When the entire code base does this, can we call it a snake nest? Wormhole? Octopus clusterfuck?
Xanthir wrote:Um, dependency cancer - it's spread to all parts of the code body.

Oh, and did I mention it's contagious? That's right. Intra-application dependencies everywhere!

I wonder if it would be easier to just grab the database structure and shove it in an in-memory db until I can get this thing properly tested.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
Emu*
Posts: 689
Joined: Mon Apr 28, 2008 9:47 am UTC
Location: Cardiff, UK
Contact:

Re: Coding: Fleeting Thoughts

Postby Emu* » Tue Jul 27, 2010 8:51 am UTC

Refactor! Refactor! </sirens>
Cosmologicon wrote:Emu* implemented a naive east-first strategy and ran it for an hour, producing results that rivaled many sophisticated strategies, visiting 614 cells. For this, Emu* is awarded Best Deterministic Algorithm!

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Coding: Fleeting Thoughts

Postby Berengal » Tue Jul 27, 2010 8:57 pm UTC

Today I got to be a hero. The story of Codethulu, let me continue it:

By now I've spent over a week looking at this abominable code, all the time with a (non-fictious) deadline hanging over me*. Not only is the logic incredibly convoluted, as I've previously mentioned, but I've unavoidably noticed some pretty grave bugs and ticking bombs as well. This is where those of you familiar with the concept "Schrödingbug" are holding your breath.

Sure enough, fifteen minutes after I got to work today the director of finances was screaming hellfire about a problem that was much more severe** than the one I've been working on, and with a much shorter deadline. Now, because of vacations and my previous week of facing this monster, I was the one with the most experience dealing with the code in question, so I was called in immediately. My first hurdle was the few crisis modifications I had made and which I had tested in production (in cooperation with the project owner) but which was never put in production, which could have messed something up (remember the database connections going all over the place). Fortunately, we were quick to dismiss that possibility, and soon after the possibility of the new (non-working) features being the cause also followed. I still got to order my former*** boss, now acting boss-of-my-boss to roll back and take backups of the database, just in case.

When I got to work today, I enlisted our most senior programmer to pair program with me, both to spread the message that pair programming is good and to increase the bus number of the code in question****. In addition, our skills and focus areas are quite dissimilar and my intention was to exploit this. Now our task had changed and we had to throw all caution to the wind because there was no time, yet screwing up any further was not an option (but quite possible).

Unfortunately I can't go into much detail about the problem itself, but I can tell you that the code is incredibly slow. Our fallback, undo as much as we could, redo with the rolled back version and make some calls telling angry people who get paid to only think about money to forget about the money, would probably not make the deadline (a couple of days). And this was using lots of computers, including high-grade servers, to share the work. By the end of today we started a rewritten single-threaded batch application on a musty old developer pc***** that would chug through the entire dataset in a couple of hours. One of the things we did different from the original code was we didn't generate an image just to write it to file on a network share (overwriting the same identical file if it already existed, which was most of the time), only to read it back in right afterwards in order to use it.

Even more fun stuff happened. Our initial issue description was something like "the sequence is wrong," which neither of us knew what meant (of course), but it eventually lead us to reverse-engineer some weird custom barcode format. We did this just before a crisis-status-meeting where we impressed people with the ability to manually check parity bits and count in binary enough to buy more debugging information.
This new debugging information led us even further into the bowels of Codethulu, but suddenly everything fell into place in my mind. That moment when you suddenly emerge from the muddy disconnectedness and reach enlightenment is truly a great rush, which even now I'm riding on high as a house.
Just then the director of finance came to check in on us, and I shouted something like "The duplicates! The panel does not have a dispatch class! It is only used once! They were all fighting for the maximum!" at her. Kudos to her for managing to tolerate this gibberish and the sight of a fat geek dancing in front of her despite her own despair. Her only reply was "Great work! You shall receive an ice-cream for this," before she left without any further questions.
At this point, my pair and I were working so well together that communicating understanding was completely effortless. Sometimes we even both controlled the peripherals at the same time (allowing us to partake in refreshments without breaking our stride.) We were both in a state of high-quality flow that lasted for several hours.

Tomorrow I get to tell our director of finances that her invoicing system has support for playing music.

* Fortunately I'm safe in the knowledge that missing it wouldn't be my fault, but it would still have some non-negligible direct consequences for my employer's finances. The kind that shows up on graphs as unfortunate bumps.
** The kind that would prevent the graph from ever being made out of concern for people with heart conditions.
*** He was the boss of the development department but got sick of us maintaining our production environment. So he grabbed some practically minded developers and the DBAs and made a new department, leaving us to do development and system maintenance in peace. May he live long and prosper, and the force be with him.
**** I have no intention of being left alone with it ever again.
***** The kind with four different IDEs, antivirus and a constantly re-indexing email inbox from hell, all fighting for the same meager amount of resources.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
headprogrammingczar
Posts: 3072
Joined: Mon Oct 22, 2007 5:28 pm UTC
Location: Beaming you up

Re: Coding: Fleeting Thoughts

Postby headprogrammingczar » Tue Jul 27, 2010 10:07 pm UTC

The last sentence made my day.
<quintopia> You're not crazy. you're the goddamn headprogrammingspock!
<Weeks> You're the goddamn headprogrammingspock!
<Cheese> I love you

User avatar
Xanthir
My HERO!!!
Posts: 5426
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Wed Jul 28, 2010 12:07 am UTC

:slow-clap:
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
e^iπ+1=0
Much, much better than Gooder
Posts: 2065
Joined: Sun Feb 15, 2009 9:41 am UTC
Location: Lancaster

Re: Coding: Fleeting Thoughts

Postby e^iπ+1=0 » Wed Jul 28, 2010 3:51 am UTC

Image
poxic wrote:You, sir, have heroic hair.
poxic wrote:I note that the hair is not slowing down. It appears to have progressed from heroic to rocking.

(Avatar by Sungura)

User avatar
b.i.o
Green is the loneliest number
Posts: 2519
Joined: Fri Jul 27, 2007 4:38 pm UTC
Location: Hong Kong

Re: Coding: Fleeting Thoughts

Postby b.i.o » Wed Jul 28, 2010 3:59 am UTC

Map generation thingy in (horribly written) client-side JS: http://basicio.com/cs/maps/. I may make it more configurable later. I'd like to make it automatically generate some countries and provinces, too. And if I decide to do anything more with it I'll probably refactor things to be nicer in general and also use underscore.js, because I'm going to go insane if I have to write any more C-style for loops.

Also, this:
headprogrammingczar wrote:The last sentence made my day.

User avatar
Rippy
Posts: 2101
Joined: Sun Jul 22, 2007 11:27 pm UTC
Location: Ontario, Can o' Duh

Re: Coding: Fleeting Thoughts

Postby Rippy » Wed Jul 28, 2010 5:13 pm UTC

I - is that - does that really count as a "fleeting thought", Berengal? :P

That map generator's nice. Much better in JS than in command-line C in terms of choosing parameters (if you get around to that).

And, as for the reason I'm here:

Code: Select all

Bitwise "or" (from Haskell.org docs)
(.|.) :: a -> a -> a

teehee.

User avatar
b.i.o
Green is the loneliest number
Posts: 2519
Joined: Fri Jul 27, 2007 4:38 pm UTC
Location: Hong Kong

Re: Coding: Fleeting Thoughts

Postby b.i.o » Thu Jul 29, 2010 1:12 am UTC

Rippy wrote:That map generator's nice. Much better in JS than in command-line C in terms of choosing parameters (if you get around to that).

Yeah, I probably will, and it'll be easy enough--I already have the variables set up, I really just need to add the input boxes to grab stuff from--it'll only take me 10 minutes or so. I'm kind of using it as a vehicle to explore a couple of technologies I know I may want to use for Node Knockout

I have a few ideas I'd like to play with on the map side of things, too:
  • I'd like to clean up some of the borders, since it's a bit noisy right now for larger maps (I have one that's 2048x2048 centered as my desktop background right now), and I think that'll be pretty easy. Just get rid of any border-colored pixels that are only adjacent to water or other border-colored pixels, and I think it'll be pretty much exactly what I want.
  • I'd also like to see if I can get it to auto-divide the map up into countries. I have a couple of ideas for that, although I'll have to test and see how well they work.
  • I may explore getting some other terrain on there too, although I haven't thought about good ways to do that yet. I have a feeling the procedural generation wiki may come in handy there.

User avatar
Rippy
Posts: 2101
Joined: Sun Jul 22, 2007 11:27 pm UTC
Location: Ontario, Can o' Duh

Re: Coding: Fleeting Thoughts

Postby Rippy » Thu Jul 29, 2010 4:46 am UTC

Yeah what I did to make the borders less rough was to run a smoothing pass or two which changes a tile if there's more of another tile surrounding it than its own type. (So for land/water, a "land" with 5/8 adjacent tiles being water turns to water). It's applied on the [# of smoothing requested] last passes of procedural generation, but doing it too much makes really rounded, Spore-like terrain.

For countries, you may want to try seeding the map with several different "land" types. I tried it with a forest colour to see what happened, and while they looked nothing like forests, it started to look strikingly like a RISK board.

I was gonna write something to spread some trees around, and have them tend to clump together, but got lazy. I figured if I was gonna do that, I might as well go all-out and do height maps so there can be rivers and mountains. I think it'd be an interesting challenge to simulate erosion (start with a heightmap, determine water flows, have tiles' soil, height and moisture values be affected with time, spread trees, etc...).

User avatar
TheChewanater
Posts: 1279
Joined: Sat Aug 08, 2009 5:24 am UTC
Location: lol why am I still wearing a Santa suit?

Re: Coding: Fleeting Thoughts

Postby TheChewanater » Thu Jul 29, 2010 6:07 am UTC

Rippy wrote:I was gonna write something to spread some trees around, and have them tend to clump together, but got lazy. I figured if I was gonna do that, I might as well go all-out and do height maps so there can be rivers and mountains. I think it'd be an interesting challenge to simulate erosion (start with a heightmap, determine water flows, have tiles' soil, height and moisture values be affected with time, spread trees, etc...).


Are we still speaking of map generators?

EDIT: That map algorithm can be accomplished pretty easily in GIMP. I might write a plugin that does it automatically.
planet.png
planet.png (79.06 KiB) Viewed 5244 times
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.

User avatar
Steax
SecondTalon's Goon Squad
Posts: 3038
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Coding: Fleeting Thoughts

Postby Steax » Thu Jul 29, 2010 7:39 am UTC

You should also do climate patterns to determine different regions. And possibly also lay down animal/plants and simulate their development. And-
In Minecraft, I use the username Rirez.

User avatar
PM 2Ring
Posts: 3715
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: Coding: Fleeting Thoughts

Postby PM 2Ring » Thu Jul 29, 2010 1:08 pm UTC

Christoph Hormann knows a lot about realistic landscape generation, from planetary level down to fine detailed terain. There are some tutorials on his web site. He mostly uses POV-Ray to do the actual rendering, but his techniques can be utilized in other rendering systems.

I've done a little bit of that sort of thing, but cratered icy moons are easier. :)
Image

User avatar
b.i.o
Green is the loneliest number
Posts: 2519
Joined: Fri Jul 27, 2007 4:38 pm UTC
Location: Hong Kong

Re: Coding: Fleeting Thoughts

Postby b.i.o » Thu Jul 29, 2010 5:18 pm UTC

Rippy wrote:For countries, you may want to try seeding the map with several different "land" types. I tried it with a forest colour to see what happened, and while they looked nothing like forests, it started to look strikingly like a RISK board.

Yep, that was the idea I was going to try first. Actually, I think I might go do that now...

I think it'd be an interesting challenge to simulate erosion (start with a heightmap, determine water flows, have tiles' soil, height and moisture values be affected with time, spread trees, etc...).

Yeah, it would be. I think I'm starting to understand why Dwarf Fortress maps take so long to generate, though...

User avatar
Rippy
Posts: 2101
Joined: Sun Jul 22, 2007 11:27 pm UTC
Location: Ontario, Can o' Duh

Re: Coding: Fleeting Thoughts

Postby Rippy » Fri Jul 30, 2010 5:20 pm UTC

Yeah it's awe-inspiring just looking at the all the stages it goes through. I remember being super impressed when I saw well-travelled pathways erode (grass dying etc) in-game. And it's [presumably] tied to weight, since dragons do it with every step.

Anyway, I just wanted to comment that functional programming is just wonderful. I haven't even done anything significant in Haskell yet (though last year I did some courses in Scheme), but I don't even want to yet: I am still just in awe of the neat, logical layout of the language. Types, typeclasses, partial application, currying, kinds, monads, it's just... cool.

I'm mostly done Learn You a Haskell, after reading a big chunk of my O'reilly Haskell book and eventually getting stuck. That's a great way to learn, I've found: Get 2 books, read one until you get stuck, then start reading the other book til you get stuck, repeat. Both resources introduce things at different times and in different ways, plus the double-exposure to each concept really ingrains it.

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5101
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Coding: Fleeting Thoughts

Postby Xeio » Fri Jul 30, 2010 6:39 pm UTC

I had never known about the ?? Operator until ReSharper (A visual studio plugin we use at work, basically it warns/errors if you break the coding standards) suggested it...

Neat.

User avatar
b.i.o
Green is the loneliest number
Posts: 2519
Joined: Fri Jul 27, 2007 4:38 pm UTC
Location: Hong Kong

Re: Coding: Fleeting Thoughts

Postby b.i.o » Fri Jul 30, 2010 9:09 pm UTC

Rippy wrote:I'm mostly done Learn You a Haskell, after reading a big chunk of my O'reilly Haskell book and eventually getting stuck. That's a great way to learn, I've found: Get 2 books, read one until you get stuck, then start reading the other book til you get stuck, repeat. Both resources introduce things at different times and in different ways, plus the double-exposure to each concept really ingrains it.

This, I've found, is a really excellent way to learn. It's what I did for Haskell as well (I used those two books plus Yet Another Haskell Tutorial, which is book-length).

elminster
Posts: 1560
Joined: Mon Feb 26, 2007 1:56 pm UTC
Location: London, UK, Dimensions 1 to 42.
Contact:

Re: Coding: Fleeting Thoughts

Postby elminster » Sat Jul 31, 2010 12:42 am UTC

Ok, call me retarded but in C# you'd expect the following code:

Code: Select all

FileStream fs = new FileStream("C:/fileName.bin", FileMode.OpenOrCreate, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write("ABC");
bw.Close();
To output to "C:/fileName.bin" (or create if it doesn't exist) the bytes (in hex): "41 42 43" ...but somehow it outputs "01 41 42 43".
Furthermore, it sometimes outputs 11 as the first bytes and when written to any other section of the file it isn't preceded by any data I didn't put there. Also... the stream position says it's 0 and moving it or offsetting it backwards doesn't help.
Luckily the file parsing doesn't actually validate that bit of the file, but still... Seriously... wtf Microsoft?
Image

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Sat Jul 31, 2010 12:56 am UTC

elminster wrote:Ok, call me retarded but in C# you'd expect the following code:

Code: Select all

FileStream fs = new FileStream("C:/fileName.bin", FileMode.OpenOrCreate, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write("ABC");
bw.Close();
To output to "C:/fileName.bin" (or create if it doesn't exist) the bytes (in hex): "41 42 43" ...but somehow it outputs "01 41 42 43".
Furthermore, it sometimes outputs 11 as the first bytes and when written to any other section of the file it isn't preceded by any data I didn't put there. Also... the stream position says it's 0 and moving it or offsetting it backwards doesn't help.
Luckily the file parsing doesn't actually validate that bit of the file, but still... Seriously... wtf Microsoft?


Might be some weird character encoding voodoo. I know Java uses all-unicode strings, which is full of obscure deep magic, hidden behind it's superficially simple exterior. Maybe this is something similar, like a byte-order mark or some such.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

elminster
Posts: 1560
Joined: Mon Feb 26, 2007 1:56 pm UTC
Location: London, UK, Dimensions 1 to 42.
Contact:

Re: Coding: Fleeting Thoughts

Postby elminster » Sat Jul 31, 2010 2:06 am UTC

You, sir, name? wrote:Might be some weird character encoding voodoo. I know Java uses all-unicode strings, which is full of obscure deep magic, hidden behind it's superficially simple exterior. Maybe this is something similar, like a byte-order mark or some such.

http://msdn.microsoft.com/en-us/library/yzxa6408.aspx wrote:Writes a length-prefixed string to this stream in the current encoding of...

You know what... I need to read the documentation better. Yeah, writing it as a char array works as expected.
Edit: The fact that there's garbage data (meh, I didn't write the file specs) around the strings later in the file put me off.
Image

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Sat Jul 31, 2010 11:14 am UTC

I would not call a function with that behavior "write". That's just irresponsible.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
Sc4Freak
Posts: 673
Joined: Thu Jul 12, 2007 4:50 am UTC
Location: Redmond, Washington

Re: Coding: Fleeting Thoughts

Postby Sc4Freak » Sat Jul 31, 2010 12:05 pm UTC

And why not? It writes the string to the stream - i.e. it serializes it to a binary format. You have to indicate length of the string somehow - you can either do it the C way and use null termination, or you can do it the Pascal way and prepend the string with a value indicating its length. .NET chooses the latter. The BinaryWriter.Write method has overloads for all primitive types (char, byte, int, etc.) as well as char and byte arrays. Strings are a primitive type, too, which is why it was included in the overloads.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Sat Jul 31, 2010 12:36 pm UTC

Well, I don't know how C# does things, but I'd have thought signaling end-of-string with \0 was the convention.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
PM 2Ring
Posts: 3715
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: Coding: Fleeting Thoughts

Postby PM 2Ring » Sat Jul 31, 2010 4:15 pm UTC

You, sir, name? wrote:Well, I don't know how C# does things, but I'd have thought signaling end-of-string with \0 was the convention.

It's a C convention. Prepending the string with its length is an earlier convention. It was fairly standard in BASIC, and Pascal.

Sure, you'd expect C# to follow C conventions, but you ought to know by now what Microsoft's attitude is to conventions. :)

Strings in BCPL are rather scary: the length of the string is at a negative offset to the head of the string, i.e. s[0] is the start of the character data, and s[-1] contains the string length. (Note that those indices are word-based, not byte based.)

0xBADFEED
Posts: 687
Joined: Mon May 05, 2008 2:14 am UTC

Re: Coding: Fleeting Thoughts

Postby 0xBADFEED » Sat Jul 31, 2010 4:57 pm UTC

PM 2Ring wrote:
You, sir, name? wrote:Well, I don't know how C# does things, but I'd have thought signaling end-of-string with \0 was the convention.

It's a C convention. Prepending the string with its length is an earlier convention. It was fairly standard in BASIC, and Pascal.

Sure, you'd expect C# to follow C conventions, but you ought to know by now what Microsoft's attitude is to conventions. :)

Strings in BCPL are rather scary: the length of the string is at a negative offset to the head of the string, i.e. s[0] is the start of the character data, and s[-1] contains the string length. (Note that those indices are word-based, not byte based.)

Honestly, Pascal-style is the better convention. I don't think it had anything to do with MS's attitude and it was purely a technical choice where they went with the better option. It makes a lot more sense when you think about wanting to read in the binary file you just wrote.

OK, so I have to read a string now. How much buffer should I allocate? With length-prefixed strings you know. With NTBS's you have to read/rewind/read or grow the buffer incrementally, both of which are less performant and more complicated.

User avatar
TNorthover
Posts: 191
Joined: Wed May 06, 2009 7:11 am UTC
Location: Cambridge, UK

Re: Coding: Fleeting Thoughts

Postby TNorthover » Sat Jul 31, 2010 5:04 pm UTC

0xBADFEED wrote:OK, so I have to read a string now. How much buffer should I allocate? With length-prefixed strings you know. With NTBS's you have to read/rewind/read or grow the buffer incrementally, both of which are less performant and more complicated.

And when you add in the issue of strings that you want to actually contain \0, the C way looks even worse. Damn that stuck in the craw.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Sat Jul 31, 2010 5:09 pm UTC

TNorthover wrote:
0xBADFEED wrote:OK, so I have to read a string now. How much buffer should I allocate? With length-prefixed strings you know. With NTBS's you have to read/rewind/read or grow the buffer incrementally, both of which are less performant and more complicated.

And when you add in the issue of strings that you want to actually contain \0, the C way looks even worse. Damn that stuck in the craw.


If it has a \0, it's not a string, but some sort of binary array.

Anyway, nothing prevents you from implementing that sort of arrays in C.

e.g.

Code: Select all

struct uint8_array {
  uint32_t n;
  uint8_t data[];
};
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
TNorthover
Posts: 191
Joined: Wed May 06, 2009 7:11 am UTC
Location: Cambridge, UK

Re: Coding: Fleeting Thoughts

Postby TNorthover » Sat Jul 31, 2010 5:21 pm UTC

You, sir, name? wrote:If it has a \0, it's not a string, but some sort of binary array.

Most languages, unicode and I would disagree with you.

User avatar
hotaru
Posts: 1045
Joined: Fri Apr 13, 2007 6:54 pm UTC

Re: Coding: Fleeting Thoughts

Postby hotaru » Sat Jul 31, 2010 5:50 pm UTC

TNorthover wrote:Most languages, unicode and I would disagree with you.

which languages use U+0000?

Code: Select all

factorial product enumFromTo 1
isPrime n 
factorial (1) `mod== 1

User avatar
TNorthover
Posts: 191
Joined: Wed May 06, 2009 7:11 am UTC
Location: Cambridge, UK

Re: Coding: Fleeting Thoughts

Postby TNorthover » Sat Jul 31, 2010 6:18 pm UTC

hotaru wrote:which languages use U+0000?

None that I know of, but I can't think of any that routinely use any characters outside ASCII (λ is optional in some functional languages). On the other hand every language I know of that works with unicode strings, and most that don't, allow them to contain U+0000 (or 0).

0xBADFEED
Posts: 687
Joined: Mon May 05, 2008 2:14 am UTC

Re: Coding: Fleeting Thoughts

Postby 0xBADFEED » Sat Jul 31, 2010 6:40 pm UTC

TNorthover wrote:
You, sir, name? wrote:If it has a \0, it's not a string, but some sort of binary array.

Most languages, unicode and I would disagree with you.

You can put Unicode in an NTBS just fine... provided you use UTF-8. You can't put UTF-16 or UTF-32 in an NTBS and expect it to play nicely since many code-point encodings will contain the null-byte.

Edit:
Although, if someone's putting UTF-16 or UTF-32 into byte strings and trying to manipulate them as NTBS's then hope for them was lost long ago.

User avatar
TNorthover
Posts: 191
Joined: Wed May 06, 2009 7:11 am UTC
Location: Cambridge, UK

Re: Coding: Fleeting Thoughts

Postby TNorthover » Sat Jul 31, 2010 6:48 pm UTC

0xBADFEED wrote:
TNorthover wrote:
You, sir, name? wrote:If it has a \0, it's not a string, but some sort of binary array.

Most languages, unicode and I would disagree with you.

You can put Unicode in an NTBS just fine... provided you use UTF-8.

My main point with unicode was that it, if anything, canonically says what is in a string and it has a code point for 0. What happens with various encodings wasn't even on my mind.

(Incidentally, I think you can put any unicode character except 0 into a utf8-encoded, null-terminated string. Most languages' strings don't have that limitation, naturally).


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 11 guests