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
Volte
Oct 4, 2004

woosh woosh
Variable names should balance clarity and brevity. num_apples is a perfect variable name while n (too short), number_of_apples (too long), and apples (too ambiguous) aren't very good names for the same variable. result is useful in some places. For example if you are checking the return value/error code of many functions it's useful to have a single variable to do it in rather than cluttering the sub with a ton of one-off variables.

Adbot
ADBOT LOVES YOU

Volte
Oct 4, 2004

woosh woosh
Can't you just put the breakpoint on the closing brace?

Volte
Oct 4, 2004

woosh woosh

zootm posted:

In most languages you can't breakpoint on a line that isn't executed; braces are just syntax.
Well it works in Eclipse with MinGW/GDB. :colbert:

Volte
Oct 4, 2004

woosh woosh

JoeNotCharles posted:

What's wrong with this? The only weird thing I can see is that db.Open is called after creating the transaction, but that doesn't have anything to do with your comment so I assume that's just the way the API works.
I think it's because there is only one command being executed in that transaction. In other words, it's not really doing anything.

Volte
Oct 4, 2004

woosh woosh
I came across the original version of the Ken's Labyrinth source code by Ken Silverman of BUILD engine fame (which powered Duke3D) here. It's 8000 LOC in one single C file.

It's pasted here for the time being:
http://rafb.net/p/mrXyPD73.html

:psyduck:

Volte
Oct 4, 2004

woosh woosh

Munkeymon posted:

I was thinking a shorthand just for this case:
code:
if(mode == [1 | 2 | 3 | 5 | 8])
So ==[] is like a new operator that's really just syntactic sugar for the condition checks the compiler is going to emit anyway. That's just a quick example, though. I haven't given much thought to the 'best' way to do it.
Well some languages like Python have an 'in' operator. Like
code:
if mode in [1, 2, 3, 5, 8]

Volte
Oct 4, 2004

woosh woosh
I don't consider it monkeypatching because the extension method does not have any access to internal members of the class or indeed any special privileges that do not exist when the method is implemented normally. The extension method has to be in scope (i.e. imported) in order to use it, and I believe it is translated into a traditional function call in the IL. Obviously, it should be used sparingly and be documented well when it is, but it's how things like LINQ are implemented, and it's a real blessing.

Volte
Oct 4, 2004

woosh woosh
No, because if the number of possibilities is small enough that you would otherwise list them in the code then the performance difference is negligible. If the choices are dynamic or enormous you'll probably (hopefully) be using a list operation anyway (albeit not likely an inline one). Only if you are doing many such checks in rapid succession would the difference manifest itself, in which case you would then optimize accordingly. Of course, you shouldn't be optimizing before you need to anyway. So any drawbacks are in most cases purely theoretical.

Volte
Oct 4, 2004

woosh woosh
The fact that you are printf'ing a string stored in a variable and not a literal means that the value could change in the future without even looking at or knowing about the existence of the printf in question. If it changes to a string which contains a %, it introduces undefined behaviour that could have easily been prevented. It even specifically states this in K&R (page 155).

On the other hand, there's no point in doing it in the python code since there's no vulnerability to start with.

Volte
Oct 4, 2004

woosh woosh
I can't see the code but it's definitely a coding horror.

http://forums.somethingawful.com/showthread.php?threadid=3161210

I just sniffed the packets for this game out of curiosity and it sends raw SQL commands back and forth between client and server, as well as updating the SQL backend every 1 second or so for a heartbeat keepalive type thing. :gonk:

Volte
Oct 4, 2004

woosh woosh
Why wouldn't you just specify a particular shade of dark green in the first place if you cared that much

Volte
Oct 4, 2004

woosh woosh
That looks like generated code to me.

Volte
Oct 4, 2004

woosh woosh

That doesn't mean it isn't generated, I write code generating scripts all the time.

Volte
Oct 4, 2004

woosh woosh

yaoi prophet posted:

Huh, this looks interesting. Any idea why the mentioned languages don't use his approach?
It's because Thompson's implementation is a true regular expression implementation and can only match regular languages. Perl regex with recursion can match at least some context-free languages (messily), and most people who use regex in practice would prefer increased power over performance. It would have been nice if true regular expressions were kept and the expanded type of matching expression given a new name though.

Volte
Oct 4, 2004

woosh woosh

1337JiveTurkey posted:

You have to explicitly tell the standard Java regex to backtrack with (.*?) because it can significantly affect performance.
In all implementations I've used, this just makes the .* act non-greedily. That is, it will make the part of the string that matches (.*) as small as possible, which doesn't require backtracking.

Volte
Oct 4, 2004

woosh woosh

tef posted:

perl regexes are pretty much turing complete, not just context-free


if they were context free it would be possible to bound them in cubic time
They're only Turing complete if you cheat and include arbitrary perl code

Volte
Oct 4, 2004

woosh woosh
Is there some kind of quine theory that reduces creating quines to some sort of mechanical procedure, or did that guy just spend way too much time on it

Volte
Oct 4, 2004

woosh woosh
I think maybe that IBM JSON thing is because whatever that web service is, it works with XML internally, but can interoperate with JSON externally. So clients can send JSON to it and it will work transparently. At least that's the impression I got. It's ugly, but interop usually is.

Volte
Oct 4, 2004

woosh woosh

schnarf posted:

All hail my terrible C++ template metaprogramming FizzBuzz: http://codepad.org/MCDF6Zt9. There's no runtime branching.
:smug:

I'd really like to concatenate the entire string at compile time, but that seems hard.
I did that with variadic C++0x templates once. You could open the EXE in a hex editor and see the answer. Unfortunately I think it's lost forever.

Volte
Oct 4, 2004

woosh woosh

yaoi prophet posted:

An anonymous person sends a type-level Fizzbuzz complete with an example run.

Mine is better. You have to compile it with -fcontext-stack=105 for it to work. Yes, I am a monster.

code:
{-# LANGUAGE FlexibleInstances, UndecidableInstances, TypeSynonymInstances, OverlappingInstances, ScopedTypeVariables, MultiParamTypeClasses #-}

data Z
data S n

class Nat n where toInt :: n -> Int

instance Nat Z where toInt n = 0
instance Nat a => Nat (S a) where toInt n = 1 + (toInt (undefined::a)) 

type PlusTen n = S (S (S (S (S (S (S (S (S (S n)))))))))

type One = S Z
type Two = S One
type Three = S Two
type Four = S Three
type Five = S Four
type OneHundred = PlusTen (PlusTen (PlusTen (PlusTen (PlusTen (PlusTen (PlusTen (PlusTen (PlusTen (PlusTen Z)))))))))

class (Nat a, Nat b, Nat c) => FizzBuzz a b c where doit :: (a,b,c) -> String

instance (Nat a, Nat c) => FizzBuzz a Z c where doit _ = "fizz"
instance (Nat a, Nat b) => FizzBuzz a b Z where doit _ = "buzz"
instance Nat a => FizzBuzz a Z Z where doit _ = "fizzbuzz"
instance (Nat a, Nat b, Nat c) => FizzBuzz a b c where doit _ = show $ toInt (undefined::a)

class Loop a b c d where loopit :: (a,b,c,d) -> String

instance (FizzBuzz OneHundred b c) => Loop a b c Z where loopit _ = doit (undefined::(OneHundred,b,c))
instance (FizzBuzz a Z (S c), Loop (S a) Two c d) => Loop a Z (S c) (S d) where loopit _ = (doit (undefined::(a,Z,S c))) ++ " " ++ (loopit (undefined::(S a,Two,c,d)))
instance (FizzBuzz a (S b) Z, Loop (S a) b Four d) => Loop a (S b) Z (S d) where loopit _ = (doit (undefined::(a,S b,Z))) ++ " " ++ (loopit (undefined::(S a,b,Four,d)))
instance (FizzBuzz a Z Z, Loop (S a) Two Four d) => Loop a Z Z (S d) where loopit _ = (doit (undefined::(a,Z,Z))) ++ " " ++ (loopit (undefined::(S a,Two,Four,d)))
instance (FizzBuzz a (S b) (S c), Loop (S a) b c d) => Loop a (S b) (S c) (S d) where loopit _ = (doit (undefined::(a,S b,S c))) ++ " " ++ (loopit (undefined::(S a,b,c,d)))

main = putStrLn $ loopit (undefined::(One,Two,Four,OneHundred))

Volte fucked around with this message at 10:52 on May 27, 2011

Volte
Oct 4, 2004

woosh woosh

qntm posted:

I thought I was the only one who did this!
It's the standard practice in Haskell:
code:
data Person = Person { firstName  :: String
                     , lastName   :: String
                     , address    :: Address
                     }

Volte
Oct 4, 2004

woosh woosh
If you ever come to a point where you think "A goto would be really easy here, otherwise I'll have to add in a bunch of boolean flags and conditionals to code my way around it" and then add in the boolean flags and conditionals, you're doing it wrong

Volte
Oct 4, 2004

woosh woosh

Factor Mystic posted:

Welp.


You know what, "write a bit of sane database interface code" should replace FizzBuzz as the idiot filter.
The only winning move is not to play

Volte
Oct 4, 2004

woosh woosh
OCaml uses the semicolon for the same purpose, but it does not do anything weird with returning unit.
code:
# let a = (printf "hello "; 5;);;
hello val a : int = 5
Also, pattern matching should be exhaustive unless maybe if the return type of the match is unit, otherwise you're leaving yourself open to domain errors that could be easily detected at compile time. Can the rust alt statement return values, or do they just execute statements like a switch statement in C?

Volte
Oct 4, 2004

woosh woosh

w00tz0r posted:

"So each node has either a branch or a leaf..."
"Wait, what?"

code:
class Tree
{
   ...

private:
   Node *_root;
};

struct Branch { };
struct Leaf { };

struct Node
{
    Node(Branch *branch);
    Node(Leaf *leaf);

    union NodeType
    {
        Branch *branch;
        Leaf *leaf;
    }

    NodeType nodeType;
    bool     isBranch;
};
I guess that's one way to construct a tree.
Haskell programmer found

Volte
Oct 4, 2004

woosh woosh
I think Branch and Leaf are supposed to inherit from Node or something. Either that or it's an incomplete paste and Branch is what contains the pointer to the subtree. Still really dumb though

Volte
Oct 4, 2004

woosh woosh
Instead of thinking of the braces as belonging to the if statement, think of them as denoting a single compound statement (because that's what they do, in the grammar). A branch of an if-statement always contains a single statement, but that single statement may be a compound statement. It's not that hard to get used to verifying that the statement after an 'if' is of the correct form.

On the other hand, if the code style that you have to follow specifies that you need to use single-statement blocks, then that trumps all the other reasons you can think of for not doing it.

Volte
Oct 4, 2004

woosh woosh
What kind of a noob language are you using that has assignment?

Volte
Oct 4, 2004

woosh woosh

yaoi prophet posted:

I bet you thought you'd seen all the weird coding styles people use. Well, I bet you've never seen semicolons at the start of lines.
Haskell programmer found

Volte
Oct 4, 2004

woosh woosh

Zombywuf posted:

You know that the letters on the top of the keys aren't anything to do with the characters on the screen right?
They have something to do with them, in the brains of the average person. Does anyone know all the symbols that OS X lets you type with the option key? Because I sure don't, but I might if they were written on the keys.

Volte
Oct 4, 2004

woosh woosh

Cocoa Crispies posted:

If your functions are taller than about ten (twenty is pushing it) lines that's the problem.
This is complete cargo cult bullshit.

Volte
Oct 4, 2004

woosh woosh
If you take one-time-only code and turn it into a nullary function, all you're doing is making it hard to follow for no reason. Just put a comment for gently caress sakes.

Volte
Oct 4, 2004

woosh woosh
There's no need to refactor Q_rsqrt at all. The only things missing are comments that actually explain how it works. "Self-documenting code" doesn't mean that your code should literally have the explanations of how it works in the names of the variables and functions.

Volte
Oct 4, 2004

woosh woosh

Zombywuf posted:

When I see a codebase full of 3000 line functions my preferred blanket rule is "Don't do that or I'll break your fingers." Good style doesn't fix bad code, but bad style makes it worse.
It is my belief that (possibly catalogued in some arcane Greek tome) there are some numbers between 10 and 3000.

Volte
Oct 4, 2004

woosh woosh
Catching all exceptions in Main is fine for production releases, since in the event of a fatal error you can display a pretty error message with an appropriate amount of information rather than whatever normally happens (like a generic .NET exception message followed by an illegal operation), plus you can generate an error report that can be sent in.

Catching all exceptions anywhere other than Main is really dumb though.

Volte
Oct 4, 2004

woosh woosh
Also, if you're rethrowing the exception then you're effectively not catching it at all, from a control flow point of view. You're just temporarily intercepting it and doing some debugging side effects.

Volte
Oct 4, 2004

woosh woosh

Zhentar posted:

Nope, this is still wrong. .NET offers several events (such as AppDoman.UnhandledException) that you can (and should) hook to achieve this, without writing a try/catch at all.
I could be wrong but I'm pretty sure that will not prevent the standard .NET exception window from showing. You can use both at the same time but I'm pretty sure you need try/catch to actually absorb the exception and display something more user-friendly.

Volte
Oct 4, 2004

woosh woosh

Zhentar posted:

You are, in fact, wrong. I've used them to replace the standard .NET popup with automatic error reporting.
I stand corrected (your avatar goes well this this post).

Volte
Oct 4, 2004

woosh woosh

Star War Sex Parrot posted:

People are allowed to not like YOSPOS, but in my experience it just means that you take yourself too seriously.
Actually it's because their idea of funny is posting "gas and ban the op" or "this thread is garbage" or some low effort pun about how the OP should get out in literally every thread

edit: my mental picture of YOSPOS posters is slightly chubby teens wearing cardigans and shades while sneering

Adbot
ADBOT LOVES YOU

Volte
Oct 4, 2004

woosh woosh

Golbez posted:

How much is Hungarian notation despised here? I had to adopt it due to my predecessor and I've kept using it, though not in objects (it seems weird to get $oObject->sName instead of $oObject->Name), and as we move towards an OO framework in PHP I suspect that will drop off each more. It does give me a nice built in temp variable name though - $s for strings, $b for booleans, $a for arrays, etc. $i is for integers but that's one thing that's already in common use, in for loops at least.

It also allows for reusing names among types. For example, $sQuery is the database query, $rQuery is the resource that it gets, instead of having to do $query and $result or what not.

Not that I'm saying I love it, and the only reason I'm doing it is legacy, but ... it doesn't seem too horrible. I'm sure I'll still use $a, $s, etc. for temp variables after I move on from it.
Unless you're in some kind of untyped wasteland, that sort of naming convention is completely useless. That's not even Hungarian notation. Hungarian notation affixes some kind of meaningful descriptor to the variable (like cnt for counter, fh for file handle, etc), not the type.

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