Coding: Fleeting Thoughts

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

Moderators: phlip, Moderators General, Prelates

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 » Mon Dec 30, 2013 7:56 pm UTC

Xeio wrote:I love light-on-dark for everything. I even use the dark reddit theme.

If only I could theme the whole internets.


I mean, you could technically do this. It looks kind of bad on some websites, though.

Code: Select all

// ==UserScript==
// @name          Dark
// @grant         none
// @description   Make everything dark
// @include       *
// @require       http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js
// ==/UserScript==

$(document).ready(function () {
    $('*').css('background-color', 'black');
    $('*').css('color', 'white');
});
 
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
chridd
Has a vermicelli title
Posts: 843
Joined: Tue Aug 19, 2008 10:07 am UTC
Location: ...Earth, I guess?
Contact:

Re: Coding: Fleeting Thoughts

Postby chridd » Mon Dec 30, 2013 8:31 pm UTC

TheChewanater wrote:
Xeio wrote:I love light-on-dark for everything. I even use the dark reddit theme.

If only I could theme the whole internets.


I mean, you could technically do this. It looks kind of bad on some websites, though.

[...]
If you're using Mac OS, and you don't care about images, you could press Control-Option-Command-8 (invert all colors on screen). (...though apparently this doesn't work in newer versions by default.)
~ chri d. d. /tʃɹɪ.di.di/ (Phonotactics, schmphonotactics) · she · Forum game scores
mittfh wrote:I wish this post was very quotable...

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

Re: Coding: Fleeting Thoughts

Postby troyp » Mon Dec 30, 2013 11:08 pm UTC

Ubik wrote:It might be related to the amount of stuff on screen at once. With prose, the text forms a roughly rectangular block of relatively bright light on otherwise dark background, but in the case of code there's usually a lot more empty around it, so it's less bright on average.

With monochrome terminal the brightness and contrast were probably somewhat adjusted for the use, as opposed to the full-color web use where that part has been given to the content creators. And yes, I think all the cases of those #f00/#0f0/#00f on #000 pages with otherwise bad typography too probably have ruined the idea of light text on dark background for you. Less contrast (so that neither the dark or the light color would stay at the extreme end of brightness scale) could do wonders to readability.

That's an interesting point. I do always use low-contrast themes, especially for dark themes. I could never tolerate #fff on #000 for coding...but I can tolerate it for a shell/interpreter. Maybe it's about the amount of contiguous text, like you said. Another possibility is that it's because I don't have to read very much at once (which is a similar hypothesis, I guess, except the code is broken up in time rather than space).

phlip wrote:I'm weird for this... A light-background terminal just looks wrong to me, I can't use it, but I prefer using a light background in my text editor, a dark background just doesn't do it for me. The end result being that if I'm coding in Sublime it's dark-on-light, but if I'm coding in vim it's light-on-dark, and both of those look "right" to me...

Presumably that's a result of familiarity (unless it also applies to other interpreters, eg. IDLE). I find a light terminal background looks strange as well, although in my case it doesn't really bother me (at one point I even used a light terminal theme for novelty appeal).

Xeio wrote:I love light-on-dark for everything. I even use the dark reddit theme.

If only I could theme the whole internets.

You can. Just use a userscript/userstyle. (Or possibly, you could set your browser to give priority to a user css file).

There are also bookmarklets that reverse colours. It's a very crude way of dark-theming a site, but I occasionally use it for the reverse.

Probably the best approach would be multi-tiered. Use Stylish userthemes for popular sites, which should be well-designed. For other sites, use a reverse-colours bookmarklet and see how it looks - if necessary make adjustments with a browser extension that lets you twerk colours (or edit css directly).
[t-w-e-a-k -> "twerk"? What kind of bizarre word filter is that?]

Another tool you could use is the Readable bookmarklet (or one of its competitors) - you can set a dark theme for that, save it as a bookmarklet and apply it to any site. This is a good option for sites that are mostly just text, but probably not suitable for anything with a more complex structure. (Also, it's actually a webapp activated by a bookmarklet, so it won't work if you're offline and reading local html).

edit: reverse colours bookmarklets (the fora won't link bookmarklets, so these are links to where you can find them):
Darken
Squarefree Invert Lightness
Invert Colors

There's also some javascript on this SO page, although you may have to bookmark it yourself.

Thesh wrote:I have no problem with a light background in a terminal in general, but I like to code at night with the lights off so I make my terminal near-black and use vim with desert theme, since it seems to be easiest on my eyes.

Desert and evening themes are really nice. One thing that can be said about Vim is that it has nice themes out of the box. The default themes in Emacs are awful. I just don't understand it. There're a couple of usable light themes and I think one mediocre dark theme, but that's it. Unless you like high-contrast dark themes in which case there are about half a dozen variants on the same "amateur web site" neon colour scheme. People new to Emacs have to learn new bindings, terminology and concepts...they could at least spare them the need to search out and install a colour theme right off.

Even I'm not entirely happy with my Emacs theme. I generally use Zenburn, which I like...but it's a bit lower contrast than I need. Most of the time it doesn't matter, but if I'm using my laptop in bright sunlight, it's hard to see and I'd really benefit from a somewhat darker theme (like desert/evening in vim).

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 » Tue Dec 31, 2013 4:48 pm UTC

I never knew a commit process could make me want to kill myself.

But after trying to check in these SQL changes for nearly an hour unsucessfully... well...

lgw
Posts: 437
Joined: Mon Apr 12, 2010 10:52 pm UTC

Re: Coding: Fleeting Thoughts

Postby lgw » Tue Dec 31, 2013 7:13 pm UTC

My commit process regularly fails 4 hours into the 6-hour process. 1 hour? Lucky! :wink:
"In no set of physics laws do you get two cats." - doogly

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

Re: Coding: Fleeting Thoughts

Postby Thesh » Tue Dec 31, 2013 8:34 pm UTC

Code: Select all

--truncate the table here
truncate table [table]


Thank god for that comment; it would have taken me hours to figure out what that line did.

EDIT: this script has 2570 lines, 1900 of which have trailing whitespace...
Summum ius, summa iniuria.

User avatar
3rdtry
Posts: 152
Joined: Sat Feb 16, 2013 1:46 pm UTC

Re: Coding: Fleeting Thoughts

Postby 3rdtry » Thu Jan 02, 2014 12:32 am UTC

I just got like the craziest idea of the year (heh): a puzzle video game based on SQL tables and/or UML class diagrams. Starting from simple ones and getting to the really complicated ones with sub-subqueries and quaternary association classes and whatnot.

OK, it needs some (a lot of) polishing, but if Kohctpyktop can be a game, anything can be a game.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Thu Jan 02, 2014 2:16 am UTC

Thesh wrote:EDIT: this script has 2570 lines, 1900 of which have trailing whitespace...
Impressive. Though out of curiosity, how many of those 1900 lines consist entirely of whitespace?

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

Re: Coding: Fleeting Thoughts

Postby Thesh » Thu Jan 02, 2014 2:23 am UTC

EvanED wrote:
Thesh wrote:EDIT: this script has 2570 lines, 1900 of which have trailing whitespace...
Impressive. Though out of curiosity, how many of those 1900 lines consist entirely of whitespace?


I just checked, and there were only 10 lines that consisted entirely of whitespace, out of 102 total blank lines.
Summum ius, summa iniuria.

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

Re: Coding: Fleeting Thoughts

Postby Yakk » Thu Jan 02, 2014 5:30 am UTC

Have you made sure it was not a program written in whitespace the language?
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
Thesh
Made to Fuck Dinosaurs
Posts: 6579
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Coding: Fleeting Thoughts

Postby Thesh » Tue Jan 07, 2014 11:05 pm UTC

I decided that SQL gets null wrong. Null does not mean unknown value, null means does not have a value. Therefore, I propose the following revised logic:

Code: Select all

a    | b       | operation   | result
_____________________________________
null | null    | a = b       | undefined
null | null    | a != b      | undefined
null | value   | a = b       | false
null | value   | a != b      | true


Why does this matter?

Code: Select all

SELECT 1 WHERE 1 NOT IN (NULL)


That's why.
Summum ius, summa iniuria.

User avatar
Xenomortis
Not actually a special flower.
Posts: 1448
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Tue Jan 07, 2014 11:11 pm UTC

Thesh wrote:I decided that SQL gets null wrong. Null does not mean unknown value, null means does not have a value.

SQL implements ternary logic.
Null means the value does not exist, not that there shouldn't be one.
Image

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

Re: Coding: Fleeting Thoughts

Postby Thesh » Tue Jan 07, 2014 11:16 pm UTC

Xenomortis wrote:
Thesh wrote:I decided that SQL gets null wrong. Null does not mean unknown value, null means does not have a value.

SQL implements ternary logic.
Null means the value does not exist, not that there shouldn't be one.


Like I said, it does it wrong :)

In truth it can mean either one, but I find in cases when I am doing comparisons, NULL usually means "doesn't have a value AND shouldn't have a value." Which usually means writing "a != b OR a IS NULL OR b IS NULL" (often one of those will always be not-null). This is especially annoying when you do a left join.
Summum ius, summa iniuria.

User avatar
Xenomortis
Not actually a special flower.
Posts: 1448
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Tue Jan 07, 2014 11:47 pm UTC

From a coding point of view, I agree.
But from a record keeping perspective, SQLs interpretation does make sense, even if using it is unwieldy.
Image

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

Re: Coding: Fleeting Thoughts

Postby Thesh » Wed Jan 08, 2014 12:00 am UTC

Xenomortis wrote:But from a record keeping perspective, SQLs interpretation does make sense, even if using it is unwieldy.

But that's the thing, it only makes sense some of the time. Take a table with date of birth and date of death; if someone is alive, they have no date of death; it's not unknown, it hasn't happened, there should be no value there.

There are definitely situations where NULL means unknown as well, such as middle name. If it's not supplied, it doesn't mean they don't have a middle name, it means that you don't know what it is.

If I was redoing SQL, I would also add ?= and ?!= operators as short hands for "a = b OR a IS NULL AND b IS NULL" and "a != b OR (a IS NULL AND b IS NOT NULL) OR (a IS NOT NULL AND b IS NULL)" respectively (note that with the logic I propose you can just do "NOT (a = b OR a IS NULL AND b IS NULL)").
Summum ius, summa iniuria.

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

Re: Coding: Fleeting Thoughts

Postby Aaeriele » Wed Jan 08, 2014 5:59 pm UTC

Thesh wrote:I decided that SQL gets null wrong. Null does not mean unknown value, null means does not have a value. Therefore, I propose the following revised logic:

Code: Select all

a    | b       | operation   | result
_____________________________________
null | null    | a = b       | undefined
null | null    | a != b      | undefined
null | value   | a = b       | false
null | value   | a != b      | true



If you did that, you would either have to special-case "a = NULL" to be defined, or not allow NULL to be used as an equality value (instead requiring code to special case comparison to use "IS NULL" instead).
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!

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

Re: Coding: Fleeting Thoughts

Postby Thesh » Wed Jan 08, 2014 6:47 pm UTC

Aaeriele wrote:
Thesh wrote:I decided that SQL gets null wrong. Null does not mean unknown value, null means does not have a value. Therefore, I propose the following revised logic:

Code: Select all

a    | b       | operation   | result
_____________________________________
null | null    | a = b       | undefined
null | null    | a != b      | undefined
null | value   | a = b       | false
null | value   | a != b      | true



If you did that, you would either have to special-case "a = NULL" to be defined, or not allow NULL to be used as an equality value (instead requiring code to special case comparison to use "IS NULL" instead).


Not sure what you mean? a = NULL would be defined as false; it's not a special case.

I would actually probably change undefined to "null" for consistency with a boolean data type.
Summum ius, summa iniuria.

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

Re: Coding: Fleeting Thoughts

Postby Yakk » Wed Jan 08, 2014 8:35 pm UTC

Or allow user-defined data types with user-defined semantics.

Then you can have an MAYBE_KNOWN<OPTIONAL<DATE>> field. There is "unknown", there is "does not exist" and there is "on this date".

But that is just crazy talk from non-database perspective.
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
Thesh
Made to Fuck Dinosaurs
Posts: 6579
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Coding: Fleeting Thoughts

Postby Thesh » Wed Jan 08, 2014 8:45 pm UTC

I considered it, and it's logically the best solution, but I would rather deal with one problem than three.
Summum ius, summa iniuria.

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

Re: Coding: Fleeting Thoughts

Postby headprogrammingczar » Wed Jan 08, 2014 9:34 pm UTC

Yakk wrote:Or allow user-defined data types with user-defined semantics.

Then you can have an MAYBE_KNOWN<OPTIONAL<DATE>> field. There is "unknown", there is "does not exist" and there is "on this date".

But that is just crazy talk from non-database perspective.


This design is completely flawed. You need to be able to keep track of both "I don't know if we have a value" and "I know we have a value but not what it is". Conveniently, there's an elegant description of that via MAYBE_KNOWN<OPTIONAL<MAYBE_KNOWN<DATE>>>. ;)
<quintopia> You're not crazy. you're the goddamn headprogrammingspock!
<Weeks> You're the goddamn headprogrammingspock!
<Cheese> I love you

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

Re: Coding: Fleeting Thoughts

Postby Thesh » Wed Jan 08, 2014 9:42 pm UTC

You also need one for "We have a value, but don't know if we should."
Summum ius, summa iniuria.

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

Re: Coding: Fleeting Thoughts

Postby Yakk » Wed Jan 08, 2014 9:48 pm UTC

HUH<DATE>
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? » Wed Jan 08, 2014 10:00 pm UTC

There's also WHY<T> (you don't know what type meaning of this type is), UNKNOWABLE<T> (it's impossible to truly know the nature of UNKNOWABLE<T> from T; a form of metaphysical type erasure), and IMPOSSIBLE (a value that cannot be represented within a universe that is governed by logic and reason).

I can't imagine evaluating zen koans without these types.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Wed Jan 08, 2014 10:31 pm UTC

Don't forget FILE_NOT_FOUND

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

Re: Coding: Fleeting Thoughts

Postby Aaeriele » Thu Jan 09, 2014 7:01 am UTC

Thesh wrote:Not sure what you mean? a = NULL would be defined as false; it's not a special case.

I would actually probably change undefined to "null" for consistency with a boolean data type.


What happens when a contains the value NULL? E.g. attempting to check if a field contains null? "SELECT foo from bar if baz = NULL"
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!

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

Re: Coding: Fleeting Thoughts

Postby Thesh » Thu Jan 09, 2014 11:25 am UTC

Aaeriele wrote:
Thesh wrote:Not sure what you mean? a = NULL would be defined as false; it's not a special case.

I would actually probably change undefined to "null" for consistency with a boolean data type.


What happens when a contains the value NULL? E.g. attempting to check if a field contains null? "SELECT foo from bar if baz = NULL"

NULL = NULL returns undefined/null - it's essentially the same thing that happens in ANSI SQL today, except ANSI SQL calls it unknown.
Summum ius, summa iniuria.

User avatar
Xenomortis
Not actually a special flower.
Posts: 1448
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Fri Jan 10, 2014 10:19 pm UTC

So I've started using Python for the first time this week (at my new job).
I spent about a day writing a script to recreate a C# program that took me a couple of hours to write.

There are some things I do like, but there are times where I feel that it's trying to be too helpful.
And why do I need to write "self" everywhere?
Image

Nyktos
Posts: 138
Joined: Mon Mar 02, 2009 4:02 pm UTC

Re: Coding: Fleeting Thoughts

Postby Nyktos » Sat Jan 11, 2014 12:51 am UTC

Xenomortis wrote:And why do I need to write "self" everywhere?
Because Python doesn't have variable declarations, so you need some other way to distinguish instance attributes from locals.

User avatar
Xenomortis
Not actually a special flower.
Posts: 1448
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Sat Jan 11, 2014 1:06 am UTC

Nyktos wrote:
Xenomortis wrote:And why do I need to write "self" everywhere?
Because Python doesn't have variable declarations, so you need some other way to distinguish instance attributes from locals.

Right.
But I don't think I should be allowed to have a local variable in a class method with the same name as an attribute.

And having to specify the object instance in the method signature, but then not provide it explicitly in the method call is weird. It's also unnecessary; a class method without a reference to the instance is invalid, unless the @staticmethod decorator is present - why not just depend on the decorator?

I just can't see why this can't be implicit and hidden, like other languages.
Image

Nyktos
Posts: 138
Joined: Mon Mar 02, 2009 4:02 pm UTC

Re: Coding: Fleeting Thoughts

Postby Nyktos » Sat Jan 11, 2014 1:27 am UTC

Xenomortis wrote:Right.
But I don't think I should be allowed to have a local variable in a class method with the same name as an attribute.
Without declarations, how does Python know what names attributes might have?

Xenomortis wrote:And having to specify the object instance in the method signature, but then not provide it explicitly in the method call is weird. It's also unnecessary; a class method without a reference to the instance is invalid, unless the @staticmethod decorator is present - why not just depend on the decorator?
staticmethod's not magical; it's just a very simple application of the descriptor protocol. It can be reimplemented in five lines of Python:

Code: Select all

class staticmethod(object):
    def __init__(self, fn):
        self._fn = fn
    def __get__(self, instance, owner):
        return self._fn

staticmethod is also far from the only situation in which you might have a "def" block inside a class but not want to use it as a method: classmethod is the same, at least, and you can write your own decorators that take a function and transform it into something not-a-function. On the flipside, there are legitimate use cases (mostly class decorators) for definining a function outside a class and then using it as a method.

Xenomortis wrote:I just can't see why this can't be implicit and hidden, like other languages.
Which other languages? Ruby uses an @ sigil for instance attributes. JavaScript and PHP require explicit "this". I can't think of any dynamic language that works the way you want.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Sat Jan 11, 2014 2:21 am UTC

Exactly. Python's "explict self" isn't the only reasonable design approach they could have taken, but the principle alternative would have been something like how functions work with globals, where if you want to assign to a global from inside of a function, you have to say global whatever. The analogy would be you'd have to say something like instance whatever. Except that writing to instance variables is probably way more common than writing to globals, so that wouldn't have been un-annoying either.

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

Re: Coding: Fleeting Thoughts

Postby Aaeriele » Sat Jan 11, 2014 3:20 am UTC

I personally like the explicit self; it drives me batty in Java when people intermix class attributes, variables, and function parameters. With the 'self' there you know it's coming from the class rather than a local, and it makes reading things much more straightforward.

It also allows you to pass in an arbitrary 'self' if you so desire, e.g....

Code: Select all

>>> class Foo(object):
...   def bar(self):
...     print self.baz
...
>>> class Qux(Foo):
...   baz = 'meep'
...   def bar(self):
...     print 'wrong!'
...
>>> x = Qux()
>>> x.bar()
wrong!
>>> Foo.bar(x)
meep
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!

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

Re: Coding: Fleeting Thoughts

Postby troyp » Sat Jan 11, 2014 5:52 pm UTC

You, sir, name? wrote:I can't imagine evaluating zen koans without these types.

evaluating zen koans is a noop. :-)

Xenomortis wrote:But I don't think I should be allowed to have a local variable in a class method with the same name as an attribute.

Then you could never change the implementation of a class method without fear your locals will collide with an attribute of some subclass outside your control.

There are other issues, specific to dynamic languages. First, as Nyktos said, there's the question of how Python would know what variables are present in a method. But let's put that aside.

Methods are created when the class is created, but instance attributes are created at instantiation (or later). So what you're really requiring is not "shouldn't be allowed to have a method local with the same name as an attribute", but "shouldn't be allowed to have an attribute with the same name as a method local". Which means that the implementation details of methods would leak into the class interface.

Even worse, methods can be added to a class dynamically. So what happens then if a method local collides with an instance attribute? Is it "whoever calls dibs first gets it"? Why should a legal class modification suddenly become illegal, just because you created an instance of the class first?

Finally, attribute access can be intercepted by __getattribute__() and handled in arbitrary ways. So even without attributes being added from outside, the legal attributes could change at runtime. You could never be sure there'd be no collisions.

And having to specify the object instance in the method signature, but then not provide it explicitly in the method call is weird.

It is provided explicitly in the call. It's just separated from the other arguments (which is reasonable, since it is a special argument). It would be weird to pass the same information twice ( as in obj.mymethod(obj, ...) ) It's true that the (typical) call and definition syntaxes don't quite match up. Maybe it would've been better to use the definition syntax

Code: Select all

def self.mymethod(args): ...
...but that's a minor issue. Also note that the call syntax for an unbound method does match the definition syntax (see below).

It's also unnecessary; a class method without a reference to the instance is invalid, unless the @staticmethod decorator is present - why not just depend on the decorator?

[ aside: you shouldn't say "class method" to mean "a method of a class" in Python. A "class method" is an actual type of method that isn't bound to an instance at all (like a static method in C++). A regular method is called an "instance method" (since it is normally bound to an instance). ]

An instance method without a reference to an instance is perfectly valid. It's called an "unbound instance" and you can call it by giving an instance explicitly as the first argument. (So if you really dislike the definition/call asymmetry you mentioned before, you can always say myclass.foo(myobj, args) instead of myobj.foo(args) (please don't actually do it...)

User avatar
Diadem
Posts: 5654
Joined: Wed Jun 11, 2008 11:03 am UTC
Location: The Netherlands

Re: Coding: Fleeting Thoughts

Postby Diadem » Mon Jan 13, 2014 4:36 pm UTC

Does anyone have any experience with intel TBB for c++?

I'm making an overview of parallelization frameworks for C++. My company is kind of pushing for TBB, but I have my doubts. Honestly, it seems to be a complete mess. I have the strong impression it was written as a cooperative effort by a deranged madman, a 5-year-old child, and drunk chimpanzee. But that's just my impression, and I might be wrong, in which case I'd love to hear it.

How do you do something as basic as:

Code: Select all

int sum = 0;
for (int i = 0; i <= end; i++) {
    sum += somefunction(i);
}
return sum;


It should be as simple as replacing the for with a parallel_for and declaring sum as tbb:atomic<int>. However this doesn't work, as += doesn't exist for atomics. The manual says you should use sum.fetch_and_add, but that function actually also does not exist either. That's with the newest version of tbb. As far as I know there's no way to sum into an atomic variable. Makes atomics kind of useless. The alternative is using a mutex. So a parallel version becomes:

Code: Select all

int sum = 0;
tbb::spin_mutex mutex;
tbb:parallel_for (0, end + 1; [&](int i) {
    int result = somefunction(i);
    {
        tbb:spin_mutex::scoped_lock lock(mutex);
        sum ++ result;
    }
    });
return sum;


Which, impressively, is 7 times slower than my non-parallelized code, and 13 times slower than parallelization with openMP.

Either I am doing something very, very wrong here, or tbb has very few redeeming features.

So I'm wondering what people here think. Anyone who has some cents to add about tbb, or parallelization in c++ in general.
It's one of those irregular verbs, isn't it? I have an independent mind, you are an eccentric, he is round the twist
- Bernard Woolley in Yes, Prime Minister

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

Re: Coding: Fleeting Thoughts

Postby korona » Mon Jan 13, 2014 4:55 pm UTC

Is somefunction a function that takes more than a few nanoseconds to run? Otherwise the communication overhead is greater than the actual computation.
I cannot imagine that TBB doesn't have fetch_and_add. Mutexes are slow in general and in your situation almost all calls will finish concurrently and block the mutex which is bad.

I've only used the parallel data structures of TBB and not the threading related stuff but I had a good impression of the library.

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

Re: Coding: Fleeting Thoughts

Postby Aaeriele » Mon Jan 13, 2014 5:01 pm UTC

http://www.threadingbuildingblocks.org/ ... ic_cls.htm seems to indicate that fetch_and_add exists.
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!

User avatar
Diadem
Posts: 5654
Joined: Wed Jun 11, 2008 11:03 am UTC
Location: The Netherlands

Re: Coding: Fleeting Thoughts

Postby Diadem » Mon Jan 13, 2014 5:03 pm UTC

Yes. The manual claims it exists, but it doesn't. At first I thought perhaps I had an old version, but I checked with the newest version and it's not there either.

and yes, somefunction(i) is very short. That's on purpose. If I make it longer the results get better, but other parallelization platforms still seem to outperform it.
It's one of those irregular verbs, isn't it? I have an independent mind, you are an eccentric, he is round the twist
- Bernard Woolley in Yes, Prime Minister

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Mon Jan 13, 2014 5:42 pm UTC

A few years ago I took a course on multicore programming where we tried out a few different models and APIs for concurrency, including TBB. We didn't really look at atomics, but looked at their stuff for parallel_for (before C++11 lambdas which makes things nicer) and task-based parallelism. Now that OpenMP is supported by GCC, Clang, and MSVC, I don't think I'd use TBB for that sort of thing unless it was in use for the task-based stuff.

But syntax aside (no closures at the time!), I would say I enjoyed the task-based parallelism stuff. I get the sense that's the "right" way to do parallelism much of the time if you assume a single-process, imperative-language setup, and TBB I thought did a pretty good job at it.

Finally, I downloaded the current stable release of TBB and I do see fetch_and_add declarations in atomic.h. It might be worth some effort into figuring out why your compiler thinks they're undefined. I also see operator+=. Those are present in the class internal::atomic_impl_with_arithmetic, which is not a base of the primary atomic<T> template. However, there is a separate atomic<T*> partial specialization that does inherit from internal::atomic_impl_with_arithmetic. I don't know what's going on there, but maybe that gives you a lead as to what's happening. Possibly you have to make an atomic<int*> and give it the address of an existing int or something.

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 » Mon Jan 13, 2014 5:57 pm UTC

.AsParallel() is some magic. I went and looked at the source and I've no clue.

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

Re: Coding: Fleeting Thoughts

Postby Yakk » Mon Jan 13, 2014 6:41 pm UTC

Diadem wrote:Yes. The manual claims it exists, but it doesn't. At first I thought perhaps I had an old version, but I checked with the newest version and it's not there either.

and yes, somefunction(i) is very short. That's on purpose. If I make it longer the results get better, but other palatalization platforms still seem to outperform it.

This source has fetch_and_add:
https://github.com/jckarter/tbb/blob/ma ... b/atomic.h
on int and long and (on 64 bit) long long among other types.

What version, specifically, are you using?
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.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 7 guests