Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
FSMC
Apr 27, 2003
I love to live this lie

mr_jim posted:

This is paraphrased from a bit-array library that I'm working on.

code:
#define array_size(bits) (((bits) - 1) / (sizeof(unsigned int) * 8) + 1)
Any guesses for what array_size(0) returns?

I thought it was going to be 0 instead of -1, since I recently found out it rounds towards to 0, not simply rounding down.

So it just interprets -1 as an unsigned int -1= 2^(4*8)-1. (sizeof(unsigned int) * 8)=4*8. 2^(4*8-)-1/(2^5 +1)~2^27. Took me a bit of time to work out all the bits and pieces.

So the does the sizeof operator always return unsigned ints? I guess I should add a (int)cast infront of it when I use it.

Adbot
ADBOT LOVES YOU

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
It returns size_t.

mr_jim
Oct 30, 2006

OUT OF THE DARK

Unless you're planning on using the result of sizeof() in expression with a signed type, you don't need a cast. And you shouldn't cast it to a signed type unless you're sure it won't overflow, as in this case. sizeof(unsigned int) isn't likely to be out of range for an int. ;)

gcc is following the standard by converting both operands to an unsigned type here, but it caught me by surprise.

By the way, -Wconversion will make gcc warn you when it performs a conversion that can change signs. I wasn't using that and couldn't figure out why I was getting a sudden jump in memory usage during some unit tests.

Edit:

My solution didn't involve a cast either. I just explicitly check for negative or 0 values. This prevents the overflow problem and makes sure array_size(0) return 0 instead of 1.

code:
#define array_size(bits) (bits <= 0 ? 0 : (((bits) - 1) / (sizeof(unsigned int) * CHAR_BIT) + 1)

mr_jim fucked around with this message at 21:58 on Jun 2, 2009

Lexical Unit
Sep 16, 2003

C++:

Exhibit 1
code:
string foo = returns_a_string ();
if (!strcmp (foo.c_str (), "test")) { ...
// foo used nowhere else in the program
Exhibit 2
code:
struct bar {
	int uid;
	/* megaobject */
};
map<int, bar> m; // global

void handle_event_a(some data)
{
	while(/* we still have some data to consume */)
	{
		bar new_bar;
		// extract a bar from the remaining data, store it in new_bar
		m[new_bar.uid] = new_bar;
	}
}

// at no point in the program is m used like a map
// the only methods ever used on m are operator[](), begin(), and end().
// operator[]() is never called on the map besides to insert/update data, seen above.
// here is where begin() and end() are used:
void handle_event_b()
{
	vector<bar> bars;
	map<int, bar>::iterator i;
	for (i = m.begin (); i != m.end (); i++)
	{
		bars.push_back (i->second);
	}
	do_stuff (bars);
}

void do_stuff(vector<bar> bars)
{
	// simply iterates over bars to do stuff
}
Exhibit 3
code:
int pants_size = 0; // global
int pants_type = 0; // global

void handle_pants_event(int size, int type)
{
	if (pants_size == 0 && pants_type == 0)
	{
		pants_type = type;
		pants_size = size;
	}
	else if (pants_size != size || pants_type != type)
	{
		pants_type = type;
		pants_size = size;
	}
}
Exhibit 4
code:
void handle_input(void* data, unsigned long size)
{
	object o = object::parse (data, size); // allocates and copies memory
	
	switch (o.type)
	{
		case 1: foo (data, size); break;
		case 2: bar (data, size); break;
		case 3: baz (data, size); break;
	}
}

void foo(void* data, unsigned long size)
{
	object o = object::parse (data, size);
	if (o.type != 1) return;
	// use object o
}

void bar(void* data, unsigned long size)
{
	object o = object::parse (data, size);
	if (o.type != 2) return;
	// use object o
}

void baz(void* data, unsigned long size)
{
	object o = object::parse (data, size);
	if (o.type != 3) return;
	// use object o
}
:psyduck:

Lexical Unit fucked around with this message at 21:57 on Jun 2, 2009

Factor Mystic
Mar 20, 2006

Baby's First Post-Apocalyptic Fiction

Lexical Unit posted:

Exhibit 3
code:
int pants_size = 0; // global
int pants_type = 0; // global

void handle_pants_event(int size, int type)
{
	if (pants_size == 0 && pants_type == 0)
	{
		pants_type = type;
		pants_size = size;
	}
	else if (pants_size != size || pants_type != type)
	{
		pants_type = type;
		pants_size = size;
	}
}

I do not understand how even the most novice day 1 programmer could look at this and think it is okay.

Lexical Unit
Sep 16, 2003

In the real code there's more like 20 lines in each branch... they are exactly the same. For a good long second I didn't realize what I was looking at because I couldn't believe what I saw.

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...
"handle_pants_event" is the greatest function name ever and I am going to use it all the time now.

Mill Town
Apr 17, 2006

Ryouga Inverse posted:

"handle_pants_event" is the greatest function name ever and I am going to use it all the time now.

pants event raised, handling it, brb

BigRedDot
Mar 6, 2008

In all fairness though, one person who wrote some of that code will freely admit that he is not a programmer, and that he doesn't particularly want to program or know why he is occasionally tasked to do so when it's not really part of his job.

The real horror is that we hire so many non-programmers to do programming.

BigRedDot fucked around with this message at 05:51 on Jun 3, 2009

Inverse Icarus
Dec 4, 2003

I run SyncRPG, and produce original, digital content for the Pathfinder RPG, designed from the ground up to be played online.

Mill Town posted:

pants event raised, handling it, brb

throw new PantsHandlingException(pantsType);

BigRedDot posted:

The real horror is that we hire so many non-programmers to do programming.

I've heard a lot of this from people who don't work for high-tech companies. A lot of non-CS and non-Match majors end up coding, and that scares the hell out of me.

Thankfully my job's not like that.

BigRedDot
Mar 6, 2008

Inverse Icarus posted:

I've heard a lot of this from people who don't work for high-tech companies. A lot of non-CS and non-Math majors end up coding, and that scares the hell out of me.

Thankfully my job's not like that.
We work in a research lab. The physicists and such mostly use matlab (or python/numpy/scipy if they are feeling frisky) for prototyping and algorithm development. For whatever reason, we hire lots of EE fresh-outs and other people without much relevant programming experience to do coding for production systems on billion-dollar platforms.

BigRedDot fucked around with this message at 06:28 on Jun 3, 2009

Trabisnikof
Dec 24, 2005

Inverse Icarus posted:

throw new PantsHandlingException(pantsType);


I've heard a lot of this from people who don't work for high-tech companies. A lot of non-CS and non-Match majors end up coding, and that scares the hell out of me.

Thankfully my job's not like that.

Political Science major programmer checking in

Zombywuf
Mar 29, 2008

Lexical Unit posted:

C++:

Exhibit 1
code:
string foo = returns_a_string ();
if (!strcmp (foo.c_str (), "test")) { ...
// foo used nowhere else in the program
I've seen worse:
code:
string foo = returns_a_string ();
if (!strcmp (foo, "test")) { ...
Why yes the code did have ancient versions of the g++ header files baked into it because it crashed with later versions because "the later versions of g++ are crap."

quote:

Exhibit 2
Well, that code will order the incoming events by uid...

Zombywuf
Mar 29, 2008

Oh IMAP, how do I hate thee, let me count the ways:
  • Thou art asynchronous and stateful.
  • Thou canst use a different mailbox hierarchy separator in every session.
  • Thou dost use a modified version of UTF-7 to store mailbox names. That's correct, there is a UTF-7, and IMAP4 uses a modified version of it for international mailbox names.
  • Whether thine client should decode mailbox names before or after parsing yon mailboxes path is not specified.
  • Thou sendst status updates at any time, even in the middle of sending responses to other commands.
  • Thou canst remove write access from thine client in the middle of an operation, at any time.
  • Thine specified behavior is often implementation defined.
  • Thou hast hierarchal folders and namespaces. The behavior of thine namespaces are implementation defined.

Lexical Unit
Sep 16, 2003

Zombywuf posted:

Well, that code will order the incoming events by uid...
Yeah, except that ordering these objects by their uid provides no utility what-so-ever. He's essentially using a map as a set. And then he copies the objects into a vector for no reason because he could just iterate over the global map/set if he wanted. And just to top it off he sends the newly created vector to another function by value. And all that function does is iterate over all the objects... so why create the vector in the first place? It boggles the mind.

I didn't show it but later he takes that same vector, takes the reference of it to get a pointer to it, and passes around that pointer to other functions. If you look in the revision history you can see that in the past he was just passing around the vector by value until finally the program ground to a halt and he changed to passing around pointers to the vector to speed the program up.

Fecotourist
Nov 1, 2008

Mill Town posted:

pants event raised, handling it, brb

Warning: FIFO full, pants dropped

Sebbe
Feb 29, 2004

Fecotourist posted:

Warning: FIFO full, pants dropped

I believe you're looking for FIPO; First In, Pants Off.

oldkike
Jan 10, 2003

hey

www.pleasegimmeadollar.com
Working on something containing this earlier today...

code:
try
{
   pthread_cond_broadcast(&mCond);
}
catch(...)
{
}

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
idgi

(USER WAS PUT ON PROBATION FOR THIS POST)

zombienietzsche
Dec 9, 2003
Unhandled exception?

I'm dealing with something that's even worse :( A C# website assumes the user is logged in, and then checking each and every time the member's properties are referenced for a NullReferenceException. Then it redirects them to the login page gaahhhh

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
pthread_cond_broadcast, and the pthreads library in general, is a C API; its calls should never throw exceptions. Moreover, even if pthreads calls did throw exceptions in general, pthread_cond_broadcast never fails on valid input: the spec lists only one failure mode, when you give it an uninitialized condition variable. Moreover, the call is surrounded by a generic exception-silencer, which raises the question of why: either (a) an exception from that spot once caused some bug that some fool can't be bothered to properly fix (impossible here) or (b) the program and someone can't happen again or (b) someone just instinctually adds them around most every call they make without thinking about it.

rjmccall fucked around with this message at 17:40 on Jun 4, 2009

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.

rjmccall posted:

(b) the program and someone can't happen again or
What was this supposed to say?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Mustach posted:

What was this supposed to say?

Editing mistake. Edited for justice.

king_kilr
May 25, 2007
web2py. The English language doesn't contain the appropriate verbiage to describe this, but I'll give it a shot.

1) Web based code editing tool. That is you edit your code in a web browser, and it just runs. The security risks... I can't even describe.
2) It automatically imports stuff, in many languages this may be the norm, but in Python it's the antichrist, this is advertised as a feature.
3) It automatically does migrations for you, as in it detects a change in your models, and it just changes the DB accordingly.
4) It encourages a disk based session store and in memory caching.
5) It has various DOM helper classes, basically so you can write html in python with stuff like HTML(HEAD(TITLE("my title")), BODY(DIV(A))) etc....

Also, the creator, Massimo di Pierro, just about trolls the internet (reddit in particular) advertising it. Plus he thinks the only difference between Python and Java is "exec".

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"
web2py is the TimeCube of Python web libraries. Here's a quote from the creator, from a reddit thread:

quote:

It looks like you have not even read the slides that started this thread:

  • web2py does not automatically import your modules. web2py executes (exec) your code instead of importing it, and it executes it in a context where extra symbols are defined. "exec" is the only function that distinguishes an interpreted language form a compiler one. If you do not use it you are programming as if you were programming in Java.
  • The web2py admin interface is not less secure than an SSH shell or the Django admin. Actually it is more secure than the Django admin since forces secure cookies and only works if it detects HTTPS or localhost.
  • Web2py does migrations, Django does not. You may not like or need this feature but you cannot call it a downside. Rails does migrations too.
  • You say "Web2Py encourages people to use the same threads used to serve Python application logic to serve media". Not at all. How did you come to this conclusion? On my server apache serves static files. web2py can do it and some times there are advantages to it. web2py does it if cherry wsgiserver is the only web server available. It can serve range request, partial content and if-modified-since. It can also enforced role based access control on media files.

I do not promote web2py because web2py is not for sale. I advocate web2py, like you advocate Django, like other people advocate using Clojure, Hanskell, etc.

That is what reddit is for.

You do not like when people advocate something that compete with technologies that pay your bill. I do not make money from web2py (I teach). You probably make money from Django development or consulting (don't you?).

Anyway, I have not time for this either. Once again you posted misconceptions and I was forced to rectify them. Now accuse me of trolling again.

Once again. I LOVE Django. It is an excellent piece of software. I advocate web2py because there are a lot very insecure systems out there (not because of Django, mostly because of PHP) and this is a major concern of mine. I think we can agree on this last statement and bring the discussion to an end.

Obviously the best way to replace PHP sites is to use a deformed, half-PHP half-Python language written by somebody who doesn't know what an interpreter is, or why allowing web-accessible execution of arbitrary code is a bad idea.

oh and

quote:

I tech computer science and security classes at the master and PhD level. I teach programmers and architects from major software development companies. Some of my software development research (not web2py) is funded by the Department of Energy. I think I know a thing or two about software development. I am not saying I know more than you do. I do not pretend to know you. I am just saying you do not look professional when you talk this way.
:smithicide:

Flobbster
Feb 17, 2005

"Cadet Kirk, after the way you cheated on the Kobayashi Maru test I oughta punch you in tha face!"

king_kilr posted:

5) It has various DOM helper classes, basically so you can write html in python with stuff like HTML(HEAD(TITLE("my title")), BODY(DIV(A))) etc....

I hate this poo poo in any language that does it. When I started writing my first Rails app, I'm just writing the usual <% %> templates, and everyone in all the Rails forums/blogs that I read to learn was like "You've gotta use haml, haml haml haml, I love to stick my dick in haml and then when I'm done I flip over so haml can top me too, oh godddddd haml".

Do we really need a completely different markup language that obfuscates the HTML that most people doing web development would already know? It gets translated into HTML anyway, so what's the point of introducing yet another tier of crap for a user to have to learn when regular ol' template substitution suffices?



Anyone who tells me that the box on the left is better or more legible than the box on the right can suck it.

I like Rails from the couple projects I've been involved with that use it, but it's the reinvent-the-wheel-for-no-reason mentality of these edge projects (and the flamingly pretentious copy on their websites) that ruin it for the rest of us.

Janin posted:

:smithicide:

The fact that that guy uses "I te[a]ch computer science at the Ph.D. level" as an indicator that he knows a thing or two about software development makes me wonder if he's ever interacted with any other computer science professors, ever. Out of my entire department, I can name about two or three who can actually write kick-rear end code. The others either just manage to get by, or avoid it altogether.

Flobbster fucked around with this message at 19:17 on Jun 4, 2009

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"

Flobbster posted:

I hate this poo poo in any language that does it. When I started writing my first Rails app, I'm just writing the usual <% %> templates, and everyone in all the Rails forums/blogs that I read to learn was like "You've gotta use haml, haml haml haml, I love to stick my dick in haml and then when I'm done I flip over so haml can top me too, oh godddddd haml".

Do we really need a completely different markup language that obfuscates the HTML that most people doing web development would already know? It gets translated into HTML anyway, so what's the point of introducing yet another tier of crap for a user to have to learn when regular ol' template substitution suffices?
It's very useful for generating (X)HTML from within helper code. Instead of having to manually concatenate strings and hoping to get the escape/closing/content rules right, you can just build the markup with functions.

It's a bad idea for page templates though, and web2py's implementation is *especially* bad because it just throws the template into `exec()`.

paradigmm
May 28, 2002

by Y Kant Ozma Post
code:
 /******************************** 
Welcome action 
********************************/ 
public function welcomeAction () { 
 
/******************************** 
Set variables 
********************************/ 
$page = array (); 
 
/******************************** 
Get page content 
********************************/ 
$page = new PageContent ('/careers/welcome'); 
 
/******************************** 
Assign to template 
********************************/ 
$this->view->assign ('page', $page->pageContent); 
 
} 
literally thousands of these gems

Bizarro Buddha
Feb 11, 2007
code:
 // Ah, it's a fluffly one, close nice 

Seth Turtle
May 6, 2007

by Tiny Fistpump

Bizarro Buddha posted:

code:
 // Ah, it's a fluffly one, close nice 

Stop looking at my code.

Inverse Icarus
Dec 4, 2003

I run SyncRPG, and produce original, digital content for the Pathfinder RPG, designed from the ground up to be played online.

Bizarro Buddha posted:

code:
 // Ah, it's a fluffly one, close nice 

I wrote comments in haiku one semester, after finding out that my Data Structures and Algorithms professor had a haiku page on his school website. I figured he'd get a kick out of it, but he never said anything about it. He probably just had his TAs grade everything :(

code:
// checking below me
// no children to right or left
// i am a leaf node
(I also wrote in all lowercase at all times because I thought it made me ~*UNiQUe*~)

Nigglypuff
Nov 9, 2006


BUY ME BONESTORM
OR
GO TO HELL

Inverse Icarus posted:

code:
// checking below me
// no children to right or left
// i am a leaf node

This is so beautiful.

tef
May 30, 2004

-> some l-system crap ->

Inverse Icarus posted:

code:
// checking below me
// no children to right or left
// i am a leaf node

this for new cobol title

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...

Inverse Icarus posted:

code:
// checking below me
// no children to right or left
// i am a leaf node

This is the exact opposite of a coding horror.

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

tef posted:

this for new cobol title

CanSpice
Jan 12, 2002

GO CANUCKS GO
code:
/* not really sure what I'm doing here... */

mr_jim
Oct 30, 2006

OUT OF THE DARK

CanSpice posted:

code:
/* not really sure what I'm doing here... */

At least they're being honest.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

tef posted:

this for new cobol title

Bizarro Buddha
Feb 11, 2007

tef posted:

this for new cobol title

toot toot all aboard

Adbot
ADBOT LOVES YOU

Wazzerphuk
Feb 9, 2001

Hating Chelsea before it was cool
Winner of the PWM POTM for September
Winner of the PWM POTM for January
Co-Winner of the PWM POTM for March
php:
<?
    // Load templates
    $templates_ok = true;
?>
I've found this in a few places now. The variable isn't referred to ever again, and there isn't any code to do with templates either side of it.

I guess it's reassuring to know that they're ok, in any case :)

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply