Coding: Fleeting Thoughts

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

Moderators: phlip, Moderators General, Prelates

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 » Fri Jul 06, 2012 5:40 pm UTC

Oh Microsoft, I don't know how you manage it, but you managed to fuck up VS 2012 that bad. (They are fixing it, due to loads of developer complaints).
Short version: Programs compiled with Visual Studio 2012 may not work on Windows XP.

We didn't find out till we released the beta of something we were working on. Shortly after... got a load of people saying they couldn't get it working. The work around is rather annoying as well.
Image

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 06, 2012 7:48 pm UTC

Third Party Library wrote:Unknown Exception
YOU'RE NOT HELPFUL AT ALL!

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Coding: Fleeting Thoughts

Postby sourmìlk » Sat Jul 07, 2012 12:06 am UTC

Xeio wrote:
Third Party Library wrote:Unknown Exception
YOU'RE NOT HELPFUL AT ALL!

This is why I always put helpful messages in my ex.what() function. Even if I just have a catch (std::exception &ex) block, calling ex.what() will tell me what the problem is. I don't need to know exactly what function was thrown.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

Dr. Willpower
Posts: 197
Joined: Wed May 28, 2008 3:55 pm UTC

Re: Coding: Fleeting Thoughts

Postby Dr. Willpower » Sat Jul 07, 2012 12:18 am UTC

sourmìlk wrote:
Xeio wrote:
Third Party Library wrote:Unknown Exception
YOU'RE NOT HELPFUL AT ALL!

This is why I always put helpful messages in my ex.what() function. Even if I just have a catch (std::exception &ex) block, calling ex.what() will tell me what the problem is. I don't need to know exactly what function was thrown.


The good old fashioned printf debugger. Me and C spent a lot of time together working with that debugger. HAr har har.
Image
Hat me, bro

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 07, 2012 2:10 am UTC

elminster wrote:Oh Microsoft, I don't know how you manage it, but you managed to fuck up VS 2012 that bad. (They are fixing it, due to loads of developer complaints).
Short version: Programs compiled with Visual Studio 2012 may not work on Windows XP.

We didn't find out till we released the beta of something we were working on. Shortly after... got a load of people saying they couldn't get it working. The work around is rather annoying as well.

If you took the latest version of gcc, glibc, etc. I don't think you could compile a complex application that ran on the Linux kernel 2.4.0 either.

XP is a decade old, and exited mainstream support years ago. It's kind of ridiculous that the compiler team is still having to support it because so many people are still using it.

Ben-oni
Posts: 278
Joined: Mon Sep 26, 2011 4:56 am UTC

Re: Coding: Fleeting Thoughts

Postby Ben-oni » Sat Jul 07, 2012 2:17 am UTC

Of course Visual Studio isn't obligated to support XP. There are other IDE's out there. Nobody is forced to use VS2012. By the same token, don't expect anybody to use it for serious development.

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Coding: Fleeting Thoughts

Postby sourmìlk » Sat Jul 07, 2012 3:02 am UTC

Dr. Willpower wrote:The good old fashioned printf debugger. Me and C spent a lot of time together working with that debugger. HAr har har.


I like the printf debugger. But even aside from that, you may well have to log or display exception information to the user, and in either case it's good to pass something useful along with it so that, even if you don't know the exception type, you at least know what's wrong.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

troyp
Posts: 557
Joined: Thu May 22, 2008 9:20 pm UTC
Location: Lismore, NSW

Re: Coding: Fleeting Thoughts

Postby troyp » Sat Jul 07, 2012 3:17 am UTC

Sc4Freak wrote:If you took the latest version of gcc, glibc, etc. I don't think you could compile a complex application that ran on the Linux kernel 2.4.0 either.

XP is a decade old, and exited mainstream support years ago. It's kind of ridiculous that the compiler team is still having to support it because so many people are still using it.

Vista was an utter failure and from what I can tell, it doesn't seem to have been a serious attempt at an OS but just a kind of half-developed 7 (there's no use disputing this with me - I'm just going off what I've heard, haven't even used Vista, but you can't deny that the reception of Vista by computer-literate users was vastly different from that of XP or 7). That means that XP is essentially the previous version of Windows before 7, which puts things in a different light.

Plus, a 10-year old computer probably wouldn't run 7. If a user doesn't need new hardware, they're probably not inclined to buy it just so they can pay MS for a new OS. Similarly, software vendors probably aren't going to ignore half their potential customers just because MS tells them to. As Ben-oni says, if they don't support XP, VS2012 just won't be competitive. Presumably they've realized this, since they're going to support XP, but I find it insulting that they tried this in the first place*.

* I'm not having a go at the VS team, really. If it's just them having their heads in the clouds, fair enough, but I can't help suspecting they were instructed to do this to hurt app support for XP and drive 7 adoption.

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 07, 2012 3:35 am UTC

I don't deny that XP still holds a huge marketshare. Or even that Vista was a failure. That wasn't my point.

What I have a problem with is people's expectations for MS to continue supporting outdated, legacy software. Yes, I understand the reasons why. Yes, I know your customers are all still running XP. I know your business depends on support them. I know all that. It still doesn't alter the fact that peoples' resistance to change is holding back the industry. XP is a decade old already. There are very few mainstream markets where software is expected to be maintained for 13+ years, especially not in the consumer space. I would be a much happier developer if I didn't have to worry about legacy systems or backwards compatibility.

troyp
Posts: 557
Joined: Thu May 22, 2008 9:20 pm UTC
Location: Lismore, NSW

Re: Coding: Fleeting Thoughts

Postby troyp » Sat Jul 07, 2012 5:34 am UTC

Fair enough, but this is kind of a special case, isn't it? I mean, if MS, chose not to support VS2012 on XP in the normal sense of not supporting running it on XP (and maybe they have, I can't remember), that would be one thing. But not supporting it to compile *for* XP is different. As long as any significant amount of software is still being released for XP, developers will still need development software for it, so I think a very long support time makes perfect sense.

Which is not to deny it's probably a massive PITA for the VS developers.

Ben-oni
Posts: 278
Joined: Mon Sep 26, 2011 4:56 am UTC

Re: Coding: Fleeting Thoughts

Postby Ben-oni » Sat Jul 07, 2012 7:27 am UTC

Sc4Freak wrote:It still doesn't alter the fact that peoples' resistance to change is holding back the industry.

Which industry? You're sounding like a MS drone. "If only we could get everybody to use one platform..." "If only there weren't so many languages..." "If only everything ran in the same runtime environment..." "If only we could get people to use the same browser..." "If only people would upgrade right away..." "We have this great new way of doing things that will make everything so much easier for everyone. There's only one catch... we haven't actually tested this in the real world, and we need everyone to switch over now."

What a pipe dream! People, and especially businesses, like what works. If it works, why change it? XP works. It works so well that it was Microsoft's premier OS for a very long time.

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 07, 2012 7:53 am UTC

Yes, I know. I already stated that several times. And this is exactly that - a pipe dream. Oh, how I would love it if people didn't cling to legacy systems. If everyone always upgraded to the latest version of their chosen browser. If all Windows customers didn't prefer to keep using a decade old OS. If carriers and OEMs didn't decide to keep 90% of Android devices in the dark ages. All of that would make developer's lives much easier.

No, it's not possible. But yes, legacy holds back progress. How many years have been lost in progression of web technologies because of IE6? IE6 was a technological marvel in 2000, but a decade later one in twenty are still using it because of a refusal to upgrade. What if all those IE6/7/8 users upgraded to something more modern years ago? Where could the web be today? Rinse and repeat this effect ad nauseum for the multitudes of hacks and workarounds we have to deal with today to ensure backwards compatibility.

These things are never going to change, and people and businesses have good reason for it. But at least one can dream. And complain about it in a fleeting thoughts thread, I guess.
Last edited by Sc4Freak on Sat Jul 07, 2012 7:54 am UTC, edited 1 time in total.

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Coding: Fleeting Thoughts

Postby sourmìlk » Sat Jul 07, 2012 7:54 am UTC

ben-oni wrote:If it works, why change it?


This attitude is exactly the problem. We'd all still be using stone tools if this were the case. And not the fancy stone tools strapped to wood and carved before flaking like they were using only a few hundred thousands years ago, like the really ancient ones that even Australopithecines could figure out. Because hey, they work, so why improve?

Something working doesn't mean there's no improvement. It doesn't mean the improvement isn't substantially better than the current version.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

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 » Sun Jul 08, 2012 1:32 am UTC

Microsoft extended it's support date on XP till 2014. XP still runs on more than a quarter of all desktops machines (Way more than vista). To drop support for XP as it stands isn't really a good idea at this time.
Something like compiling for XP can still be supported along side other things as an optional feature. Sure it takes development time, but it's not altering the way things targeted at windows 7 will be compiled (Except lack of features that could have been implemented... like better C++11 support).

Frankly I think a lot of people would have been happy if they would have just kept VS 2010 and added C++11 support. After checking their feedback forums, it seems a number of the changes made to VS 2012 have been debatable as to whether they're actually good or not.

IE was only such a thorn in developers side since it was the default browser. Not only that, it continues to have poor standards support. Given the current browser usage stats, it's clear that IE would have almost died out long ago if it wasn't simply for it's default installation status.

Also... not being able to compile for XP is just annoying. We have used the work around to compile for XP atm, but since they're going to add XP it in their RTM version (Currently on the RC)... I guess the workaround is ok till then.
Image

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

Re: Coding: Fleeting Thoughts

Postby Steax » Sun Jul 08, 2012 4:46 am UTC

The fact that IE is dying despite its status as a default install is quite telling. The web would be very different if it didn't come pre-installed... But to be fair, its one weakness was its inability to auto-update or get users to do it. In contrast, all other major browsers would constantly nag its users, mentioning security and whatnot, with more or less one-click upgrades.

And amusingly, Windows is still the only major OS that doesn't have simple-click upgrades (generally speaking, though exceptions like Windows Update exist). OS X and various other linux systems both have support for application automatic/one-click updates and OS updates. And, from my friends at school tech support, Windows is still the most complicated OS when upgrading to a new version. It's no surprise that old versions of Windows that just 'work' are lingering for so long. Hell, I have a XP machine sitting next to me right now (it's for storage and old games) and I still see no reason to upgrade. I have a Windows 7 machine elsewhere here, and I can't tell their difference aside from program support and UI.
In Minecraft, I use the username Rirez.

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

Re: Coding: Fleeting Thoughts

Postby hotaru » Sun Jul 08, 2012 11:33 pm UTC

troyp wrote:Plus, a 10-year old computer probably wouldn't run 7.

I doubt that. I've got a 7 year old laptop and an 8 year old desktop running windows 7, and they run better with 7 than they ever did with xp.

Code: Select all

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

troyp
Posts: 557
Joined: Thu May 22, 2008 9:20 pm UTC
Location: Lismore, NSW

Re: Coding: Fleeting Thoughts

Postby troyp » Sun Jul 08, 2012 11:49 pm UTC

hotaru wrote:I doubt that. I've got a 7 year old laptop and an 8 year old desktop running windows 7, and they run better with 7 than they ever did with xp.

I can't speak definitively, but from what I've heard, Win7 tends to run faster on than XP on modern hardware, but requires more memory* (preferably >= 1 GB), and that much memory would have been very rare on a desktop machine 10 years ago. How much memory do your machines have?

* The difference in *stated* minimum memory is HUGE, I believe 1GB for 7 and 128MB for XP (or 64MB for light activities). But I think that's more a matter that MS decided it wasn't in their interest to bullshit about it anymore.

edit: s/very rare/rare. It wouldn't have been that rare in 2002, come to think of it, but I think the average was still around 256MB.

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

Re: Coding: Fleeting Thoughts

Postby hotaru » Mon Jul 09, 2012 12:53 am UTC

troyp wrote:I can't speak definitively, but from what I've heard, Win7 tends to run faster on than XP on modern hardware, but requires more memory* (preferably >= 1 GB), and that much memory would have been very rare on a desktop machine 10 years ago. How much memory do your machines have?

* The difference in *stated* minimum memory is HUGE, I believe 1GB for 7 and 128MB for XP (or 64MB for light activities). But I think that's more a matter that MS decided it wasn't in their interest to bullshit about it anymore.

edit: s/very rare/rare. It wouldn't have been that rare in 2002, come to think of it, but I think the average was still around 256MB.

1GB on the desktop, 1.5GB on the laptop.

Code: Select all

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

troyp
Posts: 557
Joined: Thu May 22, 2008 9:20 pm UTC
Location: Lismore, NSW

Re: Coding: Fleeting Thoughts

Postby troyp » Mon Jul 09, 2012 12:43 pm UTC

hotaru wrote:1GB on the desktop, 1.5GB on the laptop.

Ah, that makes sense then.

FT: I really wish Python had increment and decrement operators (not C-style mutating ones, obviously, since Python ints are immutable, just ones that return new values). It would be worth it purely for converting back and forth between 0-based and 1-based list indexing. I'm so sick of these (...+1)s and (...-1)s.

User avatar
Jplus
Posts: 1721
Joined: Wed Apr 21, 2010 12:29 pm UTC
Location: Netherlands

Re: Coding: Fleeting Thoughts

Postby Jplus » Tue Jul 10, 2012 11:08 am UTC

troyp wrote:forth

:)
"There are only two hard problems in computer science: cache coherence, naming things, and off-by-one errors." (Phil Karlton and Leon Bambrick)

coding and xkcd combined

(Julian/Julian's)

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? » Tue Jul 10, 2012 11:13 am UTC

troyp wrote:
hotaru wrote:1GB on the desktop, 1.5GB on the laptop.

Ah, that makes sense then.

FT: I really wish Python had increment and decrement operators (not C-style mutating ones, obviously, since Python ints are immutable, just ones that return new values). It would be worth it purely for converting back and forth between 0-based and 1-based list indexing. I'm so sick of these (...+1)s and (...-1)s.


The problem isn't the operators, it's MIXING INDEX CONVENTIONS!
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Coding: Fleeting Thoughts

Postby sourmìlk » Tue Jul 10, 2012 12:26 pm UTC

You, sir, name? wrote:The problem isn't the operators, it's MIXING INDEX CONVENTIONS!

I think that, in this situation, we should listen to what Donald Knuth has to say about mixing index conventions: "Who are you? How did you get in my house?".
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

troyp
Posts: 557
Joined: Thu May 22, 2008 9:20 pm UTC
Location: Lismore, NSW

Re: Coding: Fleeting Thoughts

Postby troyp » Tue Jul 10, 2012 11:31 pm UTC

You, sir, name? wrote:The problem isn't the operators, it's MIXING INDEX CONVENTIONS!

I didn't mean literally using different explicit indices*. The post was probably poorly worded. But the fact is that for various purposes, it's natural to use either zero- or one-based indexing.

As a simple example, I might have to iterate over a list, multiplying elements by some expression involving the number of elements seen so far (ie. implicitly using 1-based indexing). So I can use the list indices in the iteration (eg using enumerate) and change all 'i's to '(i+1)'s in the expression, or use the natural iteration for the problem and subtract to get back to the the 0-based indices lists use. This gets worse in more complex situations, but even in simple cases, it can really detract from the readability of the expression**, especially when the +/-1 forces parens to be added. Admittedly, when the clutter annoys me, I can usually refactor it out, but that sometimes involves rewriting a whole section of code to take a different approach, when I feel the original expression would have been fine if it only wasn't so *messy*.

* My instinctive reaction to mixed indexing conventions is negative, but your post has forced me to revisit my opinions on them, with the result that I'm now not convinced they're a bad idea. I mean, it's obvious why they'd cause confusion, but also why they'd reduce it, so...I don't know. (There's also the wisdom of black hat guy to consider.)

** I have to confess, I'm really fussy about this. Some people feel if it takes a couple of seconds to figure out what an expression means, it's obvious and there's no reason to complain. I think - in the context of reading code - that obvious means I don't have to figure it out at all and even a couple of seconds is enough to interrupt my train of thought. Maybe it's just that my concentration is limited so I have to marshal it :-S

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

Re: Coding: Fleeting Thoughts

Postby PM 2Ring » Wed Jul 11, 2012 5:55 am UTC

troyp wrote:
You, sir, name? wrote:The problem isn't the operators, it's MIXING INDEX CONVENTIONS!

I didn't mean literally using different explicit indices*. The post was probably poorly worded. But the fact is that for various purposes, it's natural to use either zero- or one-based indexing.

As a simple example, I might have to iterate over a list, multiplying elements by some expression involving the number of elements seen so far (ie. implicitly using 1-based indexing). So I can use the list indices in the iteration (eg using enumerate) and change all 'i's to '(i+1)'s in the expression, or use the natural iteration for the problem and subtract to get back to the the 0-based indices lists use.


Yeah, ok, I confess that I have done that sort of thing myself, but it's still effectively mixing index conventions, which can be confusing to anyone reading the code (including yourself several months or years in the future) and it increases the risk of an off-by-one error somewhere.

As an alternative, you can put a dummy entry at the start of the list; in Python, a value of None can be handy for this sort of thing. Sure, it wastes a tiny bit of data space, OTOH, it may save some code space by simplifying the logic.

troyp
Posts: 557
Joined: Thu May 22, 2008 9:20 pm UTC
Location: Lismore, NSW

Re: Coding: Fleeting Thoughts

Postby troyp » Wed Jul 11, 2012 7:24 am UTC

PM 2Ring wrote:Yeah, ok, I confess that I have done that sort of thing myself, but it's still effectively mixing index conventions, which can be confusing to anyone reading the code (including yourself several months or years in the future) and it increases the risk of an off-by-one error somewhere.

Are you talking about the second choice I listed ("use the natural iteration for the problem and subtract to get back to the the 0-based indices ")? If so, I agree that it increases the chances of errors. I prefer the first option (iterating over the list naturally and converting the expressions) as the simpler one, but now and then the second option gives me a really significant reduction in the complexity of the expressions and I decide it's worth it.

PM 2Ring wrote:As an alternative, you can put a dummy entry at the start of the list; in Python, a value of None can be handy for this sort of thing. Sure, it wastes a tiny bit of data space, OTOH, it may save some code space by simplifying the logic.

Yeah, I do this sometimes, and remind myself to do it more. At first I resisted doing it - it wasn't wasting the space in a *practical* sense that bothered me, it just seemed inelegant to me, filling the zeroth spot with a dummy value. Over time, I've tried to focus on more practical notions of elegance, and I've become somewhat fond of this solution. I still see it as implicitly using one-based indexing, tbh, but it's often the most readable way to do so. In many cases you can just "do everything normally" except that you need to handle the firstzeroth element specially. If you're lucky, you might even be able to choose a dummy value that gets ignored anyway.

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

Re: Coding: Fleeting Thoughts

Postby PM 2Ring » Wed Jul 11, 2012 8:23 am UTC

troyp wrote:
PM 2Ring wrote:Yeah, ok, I confess that I have done that sort of thing myself, but it's still effectively mixing index conventions, which can be confusing to anyone reading the code (including yourself several months or years in the future) and it increases the risk of an off-by-one error somewhere.

Are you talking about the second choice I listed ("use the natural iteration for the problem and subtract to get back to the the 0-based indices ")? If so, I agree that it increases the chances of errors. I prefer the first option (iterating over the list naturally and converting the expressions) as the simpler one, but now and then the second option gives me a really significant reduction in the complexity of the expressions and I decide it's worth it.

Yes, I was talking about the second choice.

troyp wrote:
PM 2Ring wrote:As an alternative, you can put a dummy entry at the start of the list; in Python, a value of None can be handy for this sort of thing. Sure, it wastes a tiny bit of data space, OTOH, it may save some code space by simplifying the logic.

Yeah, I do this sometimes, and remind myself to do it more. At first I resisted doing it - it wasn't wasting the space in a *practical* sense that bothered me, it just seemed inelegant to me, filling the zeroth spot with a dummy value. Over time, I've tried to focus on more practical notions of elegance, and I've become somewhat fond of this solution. I still see it as implicitly using one-based indexing, tbh, but it's often the most readable way to do so. In many cases you can just "do everything normally" except that you need to handle the firstzeroth element specially. If you're lucky, you might even be able to choose a dummy value that gets ignored anyway.


If you're lucky, (or write good code :) ), the dummy value may actually make the code more efficient by eliminating a test on the index value. Although it's not really relevant in Python, in lower-level languages like C, use of sentinel nodes at each end of a list allow you to loop through a list without needing to check the list element index to ensure that you haven't over-run the list.

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 » Wed Jul 11, 2012 2:20 pm UTC

You, sir, name? wrote:The problem isn't the operators, it's MIXING INDEX CONVENTIONS!
Too late.

I think partially because the indexes can be visible to customers. I don't really know though. Nothing I can do about it anyway.

troyp
Posts: 557
Joined: Thu May 22, 2008 9:20 pm UTC
Location: Lismore, NSW

Re: Coding: Fleeting Thoughts

Postby troyp » Wed Jul 11, 2012 3:43 pm UTC

PM 2Ring wrote: If you're lucky, (or write good code :) ), the dummy value may actually make the code more efficient by eliminating a test on the index value. Although it's not really relevant in Python, in lower-level languages like C, use of sentinel nodes at each end of a list allow you to loop through a list without needing to check the list element index to ensure that you haven't over-run the list.

I've used sentinels, but not as a performance hack when I already have indices and the end-point. Why is it faster to test the values than test the indices? Anyway, I agree it wouldn't be relevant to Python - you're unlikely to do better than the built-in iteration methods for speed. Of course there are other ways you could use the extra element.

Some languages use the zeroth element of a character array to indicate string length, don't they? (again, not relevant to Python, though.)

I guess you could reuse the spot in a python list to hold some associated value (since Python won't let you add an attribute to objects of builtin types even if they're mutable >:-( ). I don't like using the spot for a value that could be misinterpreted as a proper element, though: I prefer the dummy value to either be ignored or throw an error if you process it. I might do it if the types were different and there was no danger of confusion (I don't know if I ever have, though).

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

Re: Coding: Fleeting Thoughts

Postby PM 2Ring » Thu Jul 12, 2012 8:18 am UTC

troyp wrote:
PM 2Ring wrote: If you're lucky, (or write good code :) ), the dummy value may actually make the code more efficient by eliminating a test on the index value. Although it's not really relevant in Python, in lower-level languages like C, use of sentinel nodes at each end of a list allow you to loop through a list without needing to check the list element index to ensure that you haven't over-run the list.

I've used sentinels, but not as a performance hack when I already have indices and the end-point. Why is it faster to test the values than test the indices? Anyway, I agree it wouldn't be relevant to Python - you're unlikely to do better than the built-in iteration methods for speed. Of course there are other ways you could use the extra element.

Some languages use the zeroth element of a character array to indicate string length, don't they? (again, not relevant to Python, though.)

I guess you could reuse the spot in a python list to hold some associated value (since Python won't let you add an attribute to objects of builtin types even if they're mutable >:-( ). I don't like using the spot for a value that could be misinterpreted as a proper element, though: I prefer the dummy value to either be ignored or throw an error if you process it. I might do it if the types were different and there was no danger of confusion (I don't know if I ever have, though).


It's not faster to test the values than to test the indices, in fact, it will often be slower unless the data type of the element is an elementary type. But it will be faster to just test the value than to test both the index and the value. :)

BASIC strings often stored their length in the zeroth element, and IIRC, some dialects of Pascal did that, as well.

Yeah, it can be dangerous if a sentinel element may be mistaken for a regular element; such things need to be clearly documented.

On a related note, sticking "foreign" data into data structures can be dangerous, even when well-documented.

IIRC, the earliest Macintosh OS had to be radically revised because of this. The old Macs ran on the Motorola 68000 chip, which was ostensibly a 32 bit chip, however, it only used the 24 low-order bits in address calculations, since these machines had less than 16 MB of memory; back in those days, RAM was roughly $100/MB. So the Mac OS designers decided to utilize those spare bits, and stored various binary flags in many of the address words of the OS. However, a couple of years later, Motorola released the 68020 chip, which did use the full 32 bits. So all those flags had to be moved...

Sorry, I don't have any more details, or know how accurate that story is - I heard it on an Amiga programming forum (Amiga also used the MC68k family of chips).

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Coding: Fleeting Thoughts

Postby sourmìlk » Thu Jul 12, 2012 10:28 am UTC

PM 2Ring wrote:BASIC strings often stored their length in the zeroth element, and IIRC, some dialects of Pascal did that, as well.


Funny story: until he was out of college, my dad used mostly Pascal, and before that BASIC. So when he started working with C, it was relatively early and a lot of the compilers were buggy, so he was encouraged to report any bugs. He hadn't had any experience with array indices starting at zero, so when he reported that array[1] didn't access the first element as a bug. I made fun of him for that.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

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

Re: Coding: Fleeting Thoughts

Postby PM 2Ring » Thu Jul 12, 2012 11:40 am UTC

sourmìlk wrote:
PM 2Ring wrote:BASIC strings often stored their length in the zeroth element, and IIRC, some dialects of Pascal did that, as well.


Funny story: until he was out of college, my dad used mostly Pascal, and before that BASIC. So when he started working with C, it was relatively early and a lot of the compilers were buggy, so he was encouraged to report any bugs. He hadn't had any experience with array indices starting at zero, so when he reported that array[1] didn't access the first element as a bug. I made fun of him for that.


LOL. :)

The first C compiler I used was BDS C. It ran on 8 bit machines, so it was rather tiny. It didn't have proper support for floating point types: it did have a floating point library, of sorts, but it didn't use standard C float or double, instead it handled floating point numbers as 5 element arrays!

I just thought of another language that used the initial element of a string to store its length: BCPL, a precursor to C. The original Amiga dos.library was written in BCPL (it was ported from TRIPOS), and although it was eventually rewritten in C it always retained some aspects of BCPL. Negotiating with that BCPL stuff made Amiga dos programming somewhat interesting. :)

I have vague memories of using a C library years ago that manipulated Pascal-esque strings that stored their length at the start of the string, but to keep these strings vaguely compatible with standard C strings the pointers that this library used were offset to point at the start of the character byte data, so to access the length you used an index of -1. Some people found using such negative array indices a little off-putting, but I thought it was rather cool. :)

korona
Posts: 495
Joined: Sun Jul 04, 2010 8:40 pm UTC

Re: Coding: Fleeting Thoughts

Postby korona » Thu Jul 12, 2012 9:21 pm UTC

PM 2Ring wrote:I have vague memories of using a C library years ago that manipulated Pascal-esque strings that stored their length at the start of the string, but to keep these strings vaguely compatible with standard C strings the pointers that this library used were offset to point at the start of the character byte data, so to access the length you used an index of -1. Some people found using such negative array indices a little off-putting, but I thought it was rather cool. :)

That's a trick I used when I was developing a x86 just-in-time compiler for a certain managed programming language. If the length of the array is placed at address - 4, usual array access opcodes do not need a displacement byte.

troyp
Posts: 557
Joined: Thu May 22, 2008 9:20 pm UTC
Location: Lismore, NSW

Re: Coding: Fleeting Thoughts

Postby troyp » Thu Jul 12, 2012 10:48 pm UTC

This is probably a stupid question (I only know a little C and C++ and tend to think of memory management as "the GC's job"), but if you use a negative index, how do you know some other data isn't using the memory? Can you declare the array so the extra space is set aside?

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 » Thu Jul 12, 2012 11:06 pm UTC

You wouldn't use -4 off of the allocated address, you allocate memory, then use +4 (or whatever) as the address of your actual array. You just have to make sure to allocate the extra bytes when you allocate the array (and of course make sure to pass the original allocated memory address when you free and such).

I would think wrapping everything up in a struct or class would make the whole process easier than using negative indexes though...

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6568
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Coding: Fleeting Thoughts

Postby Thesh » Thu Jul 12, 2012 11:30 pm UTC

troyp wrote:This is probably a stupid question (I only know a little C and C++ and tend to think of memory management as "the GC's job"), but if you use a negative index, how do you know some other data isn't using the memory? Can you declare the array so the extra space is set aside?


Let's say you allocate this 8 bytes of memory and assign is to a variable a. You can then set a second pointer b to the element at index 1. b[0] = a[1] and b[-1] = a[0].

I don't see anything inherently dangerous about that, but I wonder why you would not just use a struct like this:

Code: Select all

typedef struct {
   size_t length;
   char val[0];
} string;

int main() {
   int i;
   string* s = (string*)malloc(sizeof(string) + 10*sizeof(char));
   s->length = 10;
   for (i=0; i<s->length; i++)
      s->val[i] = 'a'+i;
   for (i=0; i<s->length; i++)
      printf("%c",s->val[i]);
   printf("%\n");

   return 0;
}


Code prints "abcdefghij".
Summum ius, summa iniuria.

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 13, 2012 7:31 pm UTC

Oh, hey, we're going to start unit testing our codebase! Only... uh... 6 years of backlog*! :P

*Not that we're actually going to go back and do all of that

User avatar
Yakk
Poster with most posts but no title.
Posts: 11115
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Coding: Fleeting Thoughts

Postby Yakk » Fri Jul 13, 2012 7:47 pm UTC

Thesh wrote:I don't see anything inherently dangerous about that, but I wonder why you would not just use a struct like this:

Probably to allow drop-in replacement without having to add ->val all over the place?
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

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? » Fri Jul 13, 2012 9:03 pm UTC

Xeio wrote:Oh, hey, we're going to start unit testing our codebase! Only... uh... 6 years of backlog*! :P

*Not that we're actually going to go back and do all of that


Unless you design your code with unit testing in mind, it's going to be virtually impossible to unit test. Because it won't have units to test.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
Aaeriele
Posts: 2127
Joined: Tue Feb 23, 2010 3:30 am UTC
Location: San Francisco, CA

Re: Coding: Fleeting Thoughts

Postby Aaeriele » Fri Jul 13, 2012 9:32 pm UTC

You, sir, name? wrote:
Xeio wrote:Oh, hey, we're going to start unit testing our codebase! Only... uh... 6 years of backlog*! :P

*Not that we're actually going to go back and do all of that


Unless you design your code with unit testing in mind, it's going to be virtually impossible to unit test. Because it won't have units to test.


^ That.

Generally the best approach I've seen for adding tests to code after the fact is to not do so. More specifically, don't add tests to old code - instead, as you refactor, add tests for the new code from the refactor (and design the refactored version in such a way as to be conducive to unit testing).

What's probably going to actually happen if your company presses forward with adding tests to old code is that you'll wind up with a lot of integration tests, but not many unit tests.
Vaniver wrote:Harvard is a hedge fund that runs the most prestigious dating agency in the world, and incidentally employs famous scientists to do research.

afuzzyduck wrote:ITS MEANT TO BE FLUTTERSHY BUT I JUST SEE AAERIELE! CURSE YOU FORA!

Ben-oni
Posts: 278
Joined: Mon Sep 26, 2011 4:56 am UTC

Re: Coding: Fleeting Thoughts

Postby Ben-oni » Fri Jul 13, 2012 10:42 pm UTC

You, sir, name? wrote:
Xeio wrote:Oh, hey, we're going to start unit testing our codebase! Only... uh... 6 years of backlog*! :P

*Not that we're actually going to go back and do all of that


Unless you design your code with unit testing in mind, it's going to be virtually impossible to unit test. Because it won't have units to test.

Wrong. If your code is functional in nature, then it will generally be conducive to unit testing, even if you didn't have such in mind to begin with.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 8 guests