|
pokeyman posted:Found this in the PHP manual. It's like the guy knew he wanted some kind of conditional execution, but forgot about the "if" keyword. Looks more like a Lisper missing cond. code:
|
# ¿ Mar 30, 2008 13:09 |
|
|
# ¿ May 5, 2024 06:07 |
|
There's nothing wrong with Hungarian Notation used judiciously, but there's a lot wrong with using it everywhere. It suggests something going badly wrong with your code. Nebby, your examples sound like the code your working on makes poor use of scoping. The problem you think Hungarian Notation solves, I would say, is much better solved by making sure your names stay within about half a page of each other. That is, don't let your functions get so big that the entire context of the name is visible on the screen. If this is impossible, class members for example, the names should be maximally descriptive. This seems to be the point where you struggle, 'apples' is almost always going to be a collection of 'apple' objects, we don't need to tack on 'lst', 'vec' etc to the beginning to tell us this (yes this is an oblique example of Hungarian Notation, however, it's also English). If all else fails you can just go look them up (C-r in my editor). If your class gains so many members it's impossible to keep track of them all in your head then you need to refactor your class, not rename the members. This is why Hungarian everywhere is a bad sign for code, it's the wrong solution to the problem of complexity.
|
# ¿ Mar 30, 2008 21:24 |
|
tef posted:If we're going into unicode territory how about something like ∀ x ∈ l : print x; perhaps? Wouldn't that make the existential operator the perfect mechanism to introduce unification and backtracking into a language? ps. #define ∀(col, op) for_each(col.begin(), col.end(), op)
|
# ¿ Mar 31, 2008 16:48 |
|
Kidane posted:While I am paid to write Perl, I am by no means an expert. However, I'm of the opinion that there is nothing inherent to Perl which requires overly-concise code. No, but concise code is often much easier to read, e.g. code:
As for the Optimus KB; WANT! Really, after having just looked at it I may need a change of underwear.
|
# ¿ Apr 1, 2008 10:22 |
|
more falafel please posted:It's better than Sheesh, M-x indent-region and get on with your life.
|
# ¿ Apr 4, 2008 10:19 |
|
more falafel please posted:That's the problem -- I'm stuck doing that (well, gg=G, but same difference). This code is five years old, and full of crap like this -- defining log macros badly so they need double parentheses, bizarro mixes of Systems Hungarian, CamelCase, camelCase, and under_score_words, depending on this guy's mood that month, static members called m_ivpFoo (instance variable). And of course design problems -- the initializer list for one class's ctor is 124 lines long. Do you not have check-in access? Or is this one of those case where source control would be too large a change? quote:The problem of course is that there's never time -- it's always crunch. Always the way.
|
# ¿ Apr 4, 2008 21:29 |
|
dwazegek posted:No, extension methods are pretty much just syntactic sugar for a call to a method in a referenced static class. Yuck. I'd like to plead on behalf of all maintenance programmers everywhere that you don't do that. Also, why should it not be possible to call a member of a null object? code:
|
# ¿ Apr 7, 2008 22:58 |
|
Really, even POD?
|
# ¿ Apr 7, 2008 23:36 |
|
Ah well, good job this is the coding horrors thread. I'd be interested to know if any compilers actually choke on it. Especially is foo::is_null is defined in an external compilation unit.
|
# ¿ Apr 7, 2008 23:57 |
|
rotor posted:I don't understand how this is supposed to be especially awful for what is, I assume, supposed to be an in-class example or something. for loops, have you heard of them?
|
# ¿ Apr 14, 2008 19:22 |
|
narbsy posted:if you're doing an in-class example and want to make sure everyone knows what you're doing... then this is better than a for loop due to clarity, at least in my opinion. /me shudders with horror at the thought that GUI coding should be taught before for loops.
|
# ¿ Apr 14, 2008 19:26 |
|
I may have found my new favourite control flow structure, the fordowhile loop:code:
|
# ¿ Apr 19, 2008 10:41 |
|
atomic johnson posted:Christ... everybody knows the way to do this is to make them all prime numbers so that you can just factor the resulting number to get the options that were selected. chocojosh posted:Wouldn't the correct solution be to use a bitmask? Define each enum value to 2^i (i = 0,1,2,...,n) and then just check if the relevant is set? Gah, you're both wrong. Obviously the answer is a bloom filter. It's more extensible that way.
|
# ¿ Apr 24, 2008 13:31 |
|
Flobbster posted:And to stroke my e-penis a little, code like what I just wrote above pisses me off. It should be ITYM: code:
|
# ¿ May 7, 2008 18:48 |
|
TSDK posted:It seems to me then that there were 3 WTFs: Interestingly the first fix posted (mentioned in the bug as not the right way to do it), would have been much less damaging. As for the idea of using uninitialised data as a source of entropy, it's risky as it's possible an attacker could control that data. The real WTF is that, from looking at the code, that buffer should have been filled from /dev/urandom and thus never triggered the warnings. It seems that it would only have used uninitialised data if /dev/urandom was unavailable.
|
# ¿ May 14, 2008 11:35 |
|
This one from my previous job just floated into my mind:code:
|
# ¿ Jun 12, 2008 10:16 |
|
0x1C to 0x1F are clear the best bytes to delimit data with.
|
# ¿ Jun 14, 2008 13:21 |
|
0x1C to 0x1F are valid ASCII, and valid utf-8 encodings. Whether or not your text editor will like them I don't know.
|
# ¿ Jun 14, 2008 14:37 |
|
tef posted:Perl had the nice thing of goto &function which was occasionally useful, and also allowed you to label loops so you could do break LOOP_NAME Why the past tense?
|
# ¿ Jun 15, 2008 09:43 |
|
On the subject of exception based control flow:code:
|
# ¿ Jun 18, 2008 10:13 |
|
Tetrad posted:He was literally getting an int out of this class where the address of an int would be in some other class. I've seen strcpy used on std::string, and it worked. The strcpy prototype was brought into scope from gcc header files copied into the project directory tree. With date stamps in the early 90s; this was in 2005. quote:Also a header for a function I wrote when I was in my "templates and boost are awesome" phase (which I'm still in). Nothing wrong that :-)
|
# ¿ Jul 29, 2008 09:29 |
|
code:
|
# ¿ Jul 31, 2008 17:12 |
|
_aaron posted:No, see, this is great, because then you can do things like: I can't help but feel you've missed everything that's wrong with the code I posted.
|
# ¿ Aug 1, 2008 10:12 |
|
Munkeymon posted:Actually, if this: You want Prolog: code:
|
# ¿ Aug 27, 2008 18:50 |
|
Habnabit posted:But except: pass makes me want to strangle people. Sometimes you don't or can't care if an operation fails. code:
|
# ¿ Nov 5, 2008 11:09 |
|
Habnabit posted:Sure, let's go ahead and swallow SystemExit, MemoryError, and KeyboardInterrupt. Yes, yes, and holy gently caress python makes Ctrl-C an exception. Sheesh. But fair enough, go ahead and catch KeyboardInterrupt, pass the rest.
|
# ¿ Nov 5, 2008 14:25 |
|
Scaevolus posted:But it makes sense to handle trapped signals as exceptions-- would you rather there was a different language construct specifically for those sorts of interrupts? Registered interrupt handlers? If the handler decides to exit the regular exit handlers can kick in. This is pretty much a solved problem in C, why Python does it this way is a mystery, and replaces the notion of "function X raised an exception" with "function X or something else raised an exception". You now have to disambiguate the cases.
|
# ¿ Nov 5, 2008 15:33 |
|
tef posted:If you do this in our codebase I will nail your hands to the toilet. It explicitly ignores everything. If you want to ignore everything, this is the mechanism for you.
|
# ¿ Nov 5, 2008 19:03 |
|
Habnabit posted:My whole point is you never, ever want to ignore everything. Again, what if a MemoryError is raised? You really want to ignore when there's no memory available, when something higher up the stack could free some memory? quote:When a signal comes in, python interrupts the currently executing stack frame and pushes a new stack frame with the python signal handler function. This function is passed the interrupted stack frame as well. If you have a better idea of how to do this in an interpreted language, I'd love to hear it.
|
# ¿ Nov 6, 2008 23:20 |
|
Habnabit posted:However, a C extension or python itself can deal with it. quote:I don't think you understand how exceptions work. The exception is not the error; the exception is a signal of an error condition. Discarding the exception does not make the error itself go away. If you can't deal with the error condition, don't pretend you can. quote:I have no idea what "escape signal handlers" means here. And, again, I urge you to explain how this could be dealt with in a cleaner way. A signal, i.e. some message received from outside your program, should be understood to be happening in a parallel context, not just lumped at the top of stack on top of whatever is currently executing. quote:Same thing here. How else would you do this? Python is not php; these things have been thought out carefully, and this was found to be the optimal solution. You seem to think these things were slapped together without any forethought, but you also don't seem to have any suggestions on how it could be better implemented. What? Because someone else thought this was the best solution I have to agree? All hail Hypnoguido, gently caress that. One simple solution is that of giving generators an iterator interface, i.e. while (mygen.has_more()) { do_stuff(mygen.get_next()); }. This has been implemented in other languages that provide generators or generator like objects - W3C Xpath interface, istream_iterator in C++ and GNU Prolog's C interface to name a few. I'm sure these were thought about and not just slapped together. zootm posted:Why not? It creates the correct behaviour in the default case in a transparent and understandable way. And if you have some special handler, you can override the default behaviour simply. Sounds like the best of both worlds.
|
# ¿ Nov 7, 2008 00:42 |
|
Janin posted:"Out of memory" conditions. Python docs posted:exception MemoryError So no, python itself is not guaranteed to be able to recover from a memory error and you can only tear down and dump a stack trace if you want to be safe. quote:That would make Python signal handlers pretty useless, since they wouldn't be able to manipulate the execution state except through global C variables. That's a hell of a lot better than injecting exceptions arbitrarily. It means a bit of code that has specific handlers for different exceptions might be seeing exceptions it has no idea how to recover from because they can be thrown anything. quote:What would get_next() do when there's no more data to be retrieved, if not raise an exception? In a case like this: If the user of function does something with it they're not supposed to then the function should throw. Charles Babbage posted:On two occasions I have been asked, – "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" In one case a member of the Upper, and in the other a member of the Lower House put this question. I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. quote:e: Also, it's impossible to implement has_more() for a function-based generator. quote:To handle other signals, the programmer just needs to install a signal handler. That handler can do whatever it wants, including raising an exception. My argument is that this is a bad thing. Habnabit posted:No, I never said that. You seem to be so adamant that this not the best way to do it, without considering that there might be a reason why it is that way or providing an alternate implementation. Go on then, what is the reason. Other than that the secret chiefs of python believe it to be better? quote:For something like using SIGHUP to reload config files, you can take advantage of the suspended state of the program and update the configuration from within the signal handler. Of course, you'd have to write your code in a way to prevent reloading from a signal handler from interfering with things while you're in the middle of getting information from the configuration, but you'd have to do the same in C anyway. You could even just set a flag that the main loop would acknowledge, or post an event, or something.
|
# ¿ Nov 7, 2008 11:35 |
|
tef posted:Yeah, C's exception handling and generators work completely differently to Python's.
|
# ¿ Nov 7, 2008 12:12 |
|
Janin posted:If generators do their work in has_next(), then what should happen when they raise an exception? Does it appear from the has_next() call? That's awfully awkward. Calling sys.exit() from the middle of a generator is making a mess. Depending on order of evaluation of generators is to rely on spooky action at a distance. If you want side effect but no results, why are you using a generator? The clue is in the name, it generates output. quote:The issue with using a has_next()/get_next() style is that it's not as general as the current system. Any loop implemented with has/get_next() can be wrapped in a generator, but the converse is not true because the behavior of a generator allows unpredictable results. code:
|
# ¿ Nov 9, 2008 00:13 |
|
Janin posted:If you want a language with carefully-managed side effect handling, use Haskell. The imperative nature of Python precludes stateless generators. quote:You are also ignoring that sys.exit() is just an example of a side effect; it could be anything else. Here's a simple coroutine example; quote:how would you implement this using has/get_next()? has_next() wouldn't be able to advance beyond the current point, because the generator requires a value from the yield statement to work. PEP 342 effectively redefined next to mean send(None). Handling this case is just matter of having a "has next when sent a" function. This is easily handled with an optional argument on the has_next(). quote:Shouldn't that code just use for..in? quote:I don't understand what you mean, here. If you mean custom iterables, that's easy to do. If you mean actual custom generator classes, why on earth would you want to? You probably wouldn't, yet the design seems to be geared around making it easier.
|
# ¿ Nov 9, 2008 11:55 |
|
code:
Also, why the hell is the filename a string builder?
|
# ¿ Nov 20, 2008 18:46 |
|
Vanadium posted:It worked for lisp Really? Can't say I've seen evidence of this. Prolog on the other hand...
|
# ¿ Nov 27, 2008 15:46 |
|
Lone_Strider posted:
Be very careful here, there are legitimate reasons to do this. Although unless your db's default isolation level is serializable (and you db properly supports serializable (which it doesn't)) this is the wrong code to do it. There are two reasons to do this: the first is where keys must be unique across multiple tables (i.e. table1.key must never be equal to table2.key), the second is if you require sequential keys. If a transaction that has allocated a key for an identity column rolls back when there is another transaction active that has allocated the key after it, the rolled back transaction will leave a gap in the keys. Of course if your system doesn't need either of these there is no reason for this function to exist and as I said, it's wrong. The fix would be to set the isolation level to at least repeatable read and use an update lock table hint on tblNextID in the SELECT statement.
|
# ¿ Dec 4, 2008 22:11 |
|
rjmccall posted:Language interpreters regularly end up like this, although "large functions" tends to be the least of the craziness. But if you're worried at all about interpreter performance, you really can't afford to make a function call per instruction or AST node. Your compiler should decide when to inline the functions. It's probably better at it than you are.
|
# ¿ Dec 23, 2008 18:29 |
|
rjmccall posted:This is excellent advice for general programming, but it's not so hot for core interpreter loops. Get a better compiler or write it in asm. If you're second guessing your compiler's interpretation of instruction cache usage, you have a problem.
|
# ¿ Dec 23, 2008 21:49 |
|
|
# ¿ May 5, 2024 06:07 |
|
floWenoL posted:Oh, how cute, it's someone who hasn't ever needed to program for performance! Never found I could do much better than just telling gcc to do a profiled build. Unless I did it in asm. If you're playing guess-what-the-compiler-will-do you're wasting time.
|
# ¿ Dec 24, 2008 15:04 |