Coding: Fleeting Thoughts

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

Moderators: phlip, Moderators General, Prelates

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

Re: Coding: Fleeting Thoughts

Postby Jplus » Fri Nov 28, 2014 10:04 pm UTC

ahammel wrote:In other news: I just got a job as a Haskell developer!

Congratulations!
"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)

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

Re: Coding: Fleeting Thoughts

Postby EvanED » Tue Dec 02, 2014 6:17 pm UTC

I fixed two bugs yesterday. And in the process, found an additional bug for each of them. I wrote tests for those two bugs. One of those tests uncovered two more bugs (for an admittedly liberal definition of "bug" in one case).

It's going to be one of those weeks, isn't it?

User avatar
ahammel
My Little Cabbage
Posts: 2135
Joined: Mon Jan 30, 2012 12:46 am UTC
Location: Vancouver BC
Contact:

Re: Coding: Fleeting Thoughts

Postby ahammel » Tue Dec 02, 2014 6:53 pm UTC

Jplus wrote:
ahammel wrote:In other news: I just got a job as a Haskell developer!

Congratulations!
Thanks!

EvanED wrote:I fixed two bugs yesterday. And in the process, found an additional bug for each of them. I wrote tests for those two bugs. One of those tests uncovered two more bugs (for an admittedly liberal definition of "bug" in one case).

It's going to be one of those weeks, isn't it?

By induction, the code base has infinity bugs.
He/Him/His/Alex
God damn these electric sex pants!

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

Re: Coding: Fleeting Thoughts

Postby Yakk » Tue Dec 02, 2014 9:11 pm UTC

Naw, Evan is safe. The language Evan is working on doesn't support lists of infinite length.

Bwahahahahahahaha ha ha ha
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.

KnightExemplar
Posts: 5494
Joined: Sun Dec 26, 2010 1:58 pm UTC

Re: Coding: Fleeting Thoughts

Postby KnightExemplar » Wed Dec 03, 2014 5:56 am UTC

Scott Myers's C++14 update has been released in e-book format (and will soon be in print).

Effective Modern C++
http://shop.oreilly.com/product/0636920033707.do

The "Effective C++" series is amongst my favorite, so I'm pretty excited to read it myself. I'm fairly confident in C++11 stuff, but the C++14 stuff is still new to me.
First Strike +1/+1 and Indestructible.

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

Re: Coding: Fleeting Thoughts

Postby Yakk » Wed Dec 03, 2014 3:03 pm UTC

The TOC reads as a good list of things to update your C++03 habits to C++11/14 habits.
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.

KnightExemplar
Posts: 5494
Joined: Sun Dec 26, 2010 1:58 pm UTC

Re: Coding: Fleeting Thoughts

Postby KnightExemplar » Wed Dec 03, 2014 5:17 pm UTC

Indeed. And if your C++03 is not up to snuff, definitely read the first in the series "Effective C++" from 2005 (which does include some tips on smart pointers from C++TR1)
First Strike +1/+1 and Indestructible.

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

Re: Coding: Fleeting Thoughts

Postby EvanED » Wed Dec 03, 2014 7:32 pm UTC

Evan has a sad because we're on GCC 4.5 (mostly) and VS... 2010? and no --std=c++0x/11.

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

Re: Coding: Fleeting Thoughts

Postby Yakk » Wed Dec 03, 2014 9:04 pm UTC

Developers don't let friends code in C++03.
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? » Thu Dec 04, 2014 11:14 am UTC

Yakk wrote:Developers don't let friends code in C++03.


C++ friends are a bit creepy though. They can touch your privates.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

schapel
Posts: 244
Joined: Fri Jun 13, 2014 1:33 am UTC

Re: Coding: Fleeting Thoughts

Postby schapel » Thu Dec 04, 2014 8:55 pm UTC

EvanED wrote:Evan has a sad because we're on GCC 4.5 (mostly) and VS... 2010? and no --std=c++0x/11.

In my full-time job, I'm stuck with C89 and the newest compiler I can use is GCC 4.3. For a low-paying part-time gig, I use C++11 on GCC 4.8. You can probably guess which one I enjoy working on more.

Some of the C code I run across is cute, like this:

Code: Select all

#define N 40
float values[N], weights[N];
memset(values,  '\0', N * sizeof(values[0]));
memset(weights, '\1', N * sizeof(weights[0]));

The first memset is a misguided attempt at quickly setting all 40 values to 0.0f. Misguided because using memset saves only a tiny fraction of the routine's total runtime, and also because it assumes an implementation of float that is not guaranteed by the C standard. But it should work correctly on nearly all modern computers.

The second memset really takes the cake! It's a horribly misguided attempt at initializing the weights to 1.0f, but that's not what it does and these initial values are never even used. Quick... what value do the elements of the weights array get initialized to?

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? » Thu Dec 04, 2014 9:07 pm UTC

I think a reasonably modern compiler ought to be able to whip up a nicely vectorized expression for initializing those arrays (although memset is obviously not the way to go :P)
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

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

Re: Coding: Fleeting Thoughts

Postby Xeio » Mon Dec 08, 2014 10:09 pm UTC

Code: Select all

try
{
    selectedNode.Nodes.Insert(selectedNode.Nodes.Count, documentNode);
}
    catch (ArgumentOutOfRangeException)
{
    selectedNode.Nodes.Add(documentNode);
}
Ah... ok...


Unrelated, I hate ICloneable. It's a myriad of developer headaches waiting to be unlocked in a framework interface.

I mean, Serialization is comparatively expensive, but I've never had a runtime problem because the reflector forgot a property.

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

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Mon Dec 08, 2014 10:20 pm UTC

Isn't the general advice when using ICloneable "don't do it"?
Image

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

Re: Coding: Fleeting Thoughts

Postby Xanthir » Tue Dec 09, 2014 6:24 pm UTC

schapel wrote:

Code: Select all

#define N 40
float values[N], weights[N];
memset(values,  '\0', N * sizeof(values[0]));
memset(weights, '\1', N * sizeof(weights[0]));

The second memset really takes the cake! It's a horribly misguided attempt at initializing the weights to 1.0f, but that's not what it does and these initial values are never even used. Quick... what value do the elements of the weights array get initialized to?

Assuming I understand what's happening, it's setting every byte of every float to 0x01. Because the bytes are uniform we don't have to worry about endianness, so it's equal to (2^-125)*(0x1.020202), which is definitely not 1.0f. ^_^
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

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

Re: Coding: Fleeting Thoughts

Postby korona » Tue Dec 09, 2014 7:41 pm UTC

operator-> in C++ is required to return a pointer (is that true? I didn't look up the standard). Is there any idiom that allows me to return a temporary object? In particular I want to build an iterator that constructs objects on-the-fly and supports operator->.

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

Re: Coding: Fleeting Thoughts

Postby Yakk » Wed Dec 10, 2014 3:48 am UTC

A a; a->b is defined in C++ as:
if A is a pointer type, (*a).b
otherwise, it is a.A::operator->()->b

So no, operator-> need not return a pointer. But it is defined recursively, and the only terminating case is a pointer.

So what you would be looking for is some kind of syntax whereby operator-> was given an associated struct type, and it would be invoked with a pointer-to-member in that struct, could be imagined, with operator-> proceeding to return a reference to the member in question. This does not exist in C++.
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.

jareds
Posts: 436
Joined: Wed Jan 03, 2007 3:56 pm UTC

Re: Coding: Fleeting Thoughts

Postby jareds » Wed Dec 10, 2014 3:51 am UTC

korona wrote:operator-> in C++ is required to return a pointer (is that true? I didn't look up the standard). Is there any idiom that allows me to return a temporary object? In particular I want to build an iterator that constructs objects on-the-fly and supports operator->.

If selected for an expression obj->member, obj.operator->() must return a type such that (obj.operator->())->member is legal. This may be another class type with operator-> overloaded, but logically it must eventually terminate in a pointer.

So, you could just return a std::unique_ptr. If, by "temporary object", you mean that you specifically just want to be copying it around by value, you could return a simple wrapper along these lines:

Code: Select all

template <class T>
struct wrapper {
  T elem;
  wrapper(T elem) : elem(elem) { }
  T* operator->() { return &elem; }
};

EDIT: If you want to construct the members on the fly, then what Yakk said. If you want to construct the object on the fly, then what I said. (However, I don't really know why you wouldn't just have a scratch member of type T in the iterator and return a pointer to that.)

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? » Sun Dec 14, 2014 9:13 pm UTC

I don't think you'll want to use the heap for temporary objects if there's anything you can do to avoid it. Heap fragmentation is a serious thing :-/
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

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

Re: Coding: Fleeting Thoughts

Postby Diadem » Tue Dec 16, 2014 11:02 am UTC

I don't know who created TeamCenter, but I want that person to die a slow and painful death.

No, actually, that would be too good, far too good, for them. I want them to work with their own product for at least 5 minutes. A worse punishment I can not imagine.
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

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 Dec 20, 2014 4:24 pm UTC

This was a pretty good talk: https://www.youtube.com/watch?v=4OCUEgSNIAY
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

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

Re: Coding: Fleeting Thoughts

Postby Xeio » Fri Jan 02, 2015 8:18 pm UTC

Sometimes I fix a bug, and all I can think is, how did this ever work?

The bit of code that allowed it to work was literally not present.

And yet, up until last week, it worked fine for months.

I think I don't ever want to know what side effects go on deep inside the UI data binding layers of our code. Ever.

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

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Fri Jan 02, 2015 9:37 pm UTC

That, I believe, is a result of the Fundamental Failure Mode Theorem, coupled with layers of redundancy.
http://blogs.msdn.com/b/oldnewthing/arc ... 98400.aspx
Image

User avatar
roflwaffle
Posts: 360
Joined: Wed Jul 01, 2009 6:25 am UTC

Re: Coding: Fleeting Thoughts

Postby roflwaffle » Sat Jan 03, 2015 9:34 pm UTC

Good blog post! I see that every day at work. We have a ton of logging to find problems, but for the most part we don't have anything or anyone monitoring those logs, so it's generally customers who point out when something is broken.

Rysto
Posts: 1459
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: Coding: Fleeting Thoughts

Postby Rysto » Mon Jan 05, 2015 1:30 am UTC

The only thing worse than Fundamental Failure Mode is when the program implicitly depends on the system being in a failure mode in order to actually function correctly.

User avatar
phlip
Restorer of Worlds
Posts: 7551
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Thu Jan 08, 2015 5:54 am UTC

FT:

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}

if (error)
    ┻━┻︵​╰(°□°╰);

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
Dinosaur!
Posts: 30
Joined: Fri Dec 14, 2012 8:45 pm UTC
Location: Santa Cruz, CA

Re: Coding: Fleeting Thoughts

Postby Dinosaur! » Thu Jan 08, 2015 8:39 am UTC

phlip wrote:FT:

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}

if (error)
    ┻━┻︵​╰(°□°╰);

<_<

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

Re: Coding: Fleeting Thoughts

Postby Yakk » Thu Jan 08, 2015 3:59 pm UTC

Why do you need the explicit cast to int?
...

Should the type of my lambda recurser be
* := (*,X)->Y
recurse := *->(X->Y)
or
recurse := (((X->Y),X)->Y)->(X->Y)?

These both do the same thing, in the end. The body of the source lambda differs.

In the first case, it looks like

Code: Select all

  [](auto&& self, Args... args) {
    if (base_case) return base_result;
    return self( self, modified_args... );
  }

in the second case the source lambda looks like:

Code: Select all

  [](auto&& self, Args... args) {
    if (base_case) return base_result;
    return self( modified_args... );
  }

The difference is in the recursive call. Should it be self(self, args...) or self(args...)? In the first case, self ends up being actually itself. In the second case, self ends up being the recursive wrapper around itself.

...

What C++ operator best expresses "modify the following lambda"? >> maybe? -? %? ->*?

Ie, instead of recurse( lambda ), I am thinking recurse->*lambda or recurse<<lambda or recurse>>lambda. Get rid of that trailing ).
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
Xenomortis
Not actually a special flower.
Posts: 1421
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Fri Jan 09, 2015 3:35 pm UTC

C++ question. Is this defined?

Code: Select all

std::map<int, int> testMap;
testMap[0]++;
std::cout << testMap[0] << std::endl;

i.e. is this certain to print "1"?

As far as I can tell, the answer is "yes". The value is zero-initialised, std::map performs value-initialisation (which is zero-initialization for basic types) when a key that doesn't exist is requested and will add the key-value pair to itself (even if no assignment is made).
Image

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

Re: Coding: Fleeting Thoughts

Postby EvanED » Fri Jan 09, 2015 4:13 pm UTC

Xenomortis wrote:C++ question. Is this defined?

Code: Select all

std::map<int, int> testMap;
testMap[0]++;
std::cout << testMap[0] << std::endl;

i.e. is this certain to print "1"?

As far as I can tell, the answer is "yes". The value is zero-initialised, std::map performs value-initialisation (which is zero-initialization for basic types) when a key that doesn't exist is requested and will add the key-value pair to itself (even if no assignment is made).
Yup. AFAIK, that's all correct. I depend on stuff like that a fair bit actually, though almost always with class types instead of primitives as values.

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

Re: Coding: Fleeting Thoughts

Postby korona » Fri Jan 09, 2015 8:40 pm UTC

Yeah, it is completely standards-compliant.

User avatar
phlip
Restorer of Worlds
Posts: 7551
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Sat Jan 10, 2015 1:14 pm UTC

It actually takes a surprising number of steps to get the rules that say "int() == 0", but yes, an int-valued map will be zero-initialised.
23.3.1.2 map element access
T& operator[](const key_type& x);
[...]
If there is no key equivalent to x in the map, inserts value_type(x, T()) into the map.

8.5 Initializers
8 An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.

5 To value-initialize an object of type T means:
[...]
— otherwise, the object is zero-initialized

5 To zero-initialize an object of type T means:
[...]
— if T is a scalar type, the object is set to the value 0 (zero), taken as an integral constant expression, converted to T

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

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

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Sun Jan 11, 2015 10:50 pm UTC

Two days to solve one Project Euler problem and bring the runtime down to a couple of seconds.
Ok, fine.
But I just know I'd have been done ages ago if I did it in Python...
And it'd probably be faster!
Image

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

Re: Coding: Fleeting Thoughts

Postby Jplus » Mon Jan 12, 2015 10:13 pm UTC

Faster? What language did you solve it in? Intercal?
"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
Xenomortis
Not actually a special flower.
Posts: 1421
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Mon Jan 12, 2015 10:19 pm UTC

Well, it would probably have been fine if I downloaded Boost.
But I wrote a 128-bit integer class instead.
In C++.
Badly.
Image

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

Re: Coding: Fleeting Thoughts

Postby Xeio » Thu Jan 15, 2015 8:33 pm UTC

"Error: Failed to call fully qualified method."

"Reason: The operation completed successfully"

:evil:

speising
Posts: 2266
Joined: Mon Sep 03, 2012 4:54 pm UTC
Location: wien

Re: Coding: Fleeting Thoughts

Postby speising » Thu Jan 15, 2015 8:39 pm UTC

oh, yeah, the famous "error 0".

User avatar
mosgi
Posts: 46
Joined: Thu Jul 17, 2014 8:19 pm UTC
Location: Somewhere in your past light cone

Re: Coding: Fleeting Thoughts

Postby mosgi » Sun Jan 18, 2015 8:42 pm UTC

FT: This video is super cool!

It is an hour+ long, so here's my rough summary: they took a method originally used for sampling from (complicated, high-dimensional) probability distributions and turn it into a directed search algorithm (for complicated, high-dimensional search domains). Then they built an optimiser on top of that, which manages to do some truly impressive things (eg, returning a completely different implementation from the one you fed it, but still giving bit-for-bit identical results), especially with floating-point code (where you can ask for perfectly equal answers or just "close enough", where you get to decide what that means. And in the latter case it manages to speed up Intel's hand-optimised maths library by a factor of 2-3!)

But I can also see so many other things you could do with the same basic algorithm. Maybe this could be my first project for playing around with Rust...
(they pronouns please)

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

Re: Coding: Fleeting Thoughts

Postby Xeio » Sun Jan 18, 2015 11:19 pm UTC

Turns out the success error was caused by Active Directory. </shock>

Something something, rebooting domain servers and forgot to reboot one of the app servers, something something.

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

Re: Coding: Fleeting Thoughts

Postby Yakk » Mon Jan 19, 2015 3:32 pm UTC

Xenomortis wrote:Well, it would probably have been fine if I downloaded Boost.
But I wrote a 128-bit integer class instead.
In C++.
Badly.

Look on the bright side: Do that 127 more times and you'll have learned a lot!
Spoiler:
Namely, don't write your own 128 bit integer class
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 6 guests