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
Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."

dwazegek posted:

Wouldn't compile though, you can't reference the current instance during initialization.

You sure about that? In Java, at least, instance variables are initialized from top to bottom as they appear in the source.

Adbot
ADBOT LOVES YOU

GROVER CURES HOUSE
Aug 26, 2007

Go on...

Internet Janitor posted:

You sure about that? In Java, at least, instance variables are initialized from top to bottom as they appear in the source.

Yes, but C# is a good language.

wellwhoopdedooo
Nov 23, 2007

Pound Trooper!

Broken Knees Club posted:

Yes, but C# is a good language.

boosh

ninjeff
Jan 19, 2004

dwazegek posted:

Wouldn't compile though, you can't reference the current instance during initialization.

Although you can do it with static members. I thought he was referring specifically to instance members, but I must've misread.

Looks like Terraria was written in VB then?

Hughlander
May 11, 2005

Geekner posted:

I could only imagine some variables refer to previous variables, and rely on the specific initialization order.

int ONE = 1;
int TWO = ONE+ONE;
ect.

Wouldn't the reflector just name those in alphabetical order since that's the way they're initialized? IE: One becomes int aInt, TWO becomes int bInt or however reflector names it's variables?

quiggy
Aug 7, 2010

[in Russian] Oof.



For an example like that I can see where the author's coming from, but in the case of Terraria it's a game where the coders still want to add more content patches and such. To go with the example from the article, if the author wanted to add in the ability to draw sheep, he'd just have to write a DrawSheep subroutine, but the other guy would have a lot more work ahead of him. If you know you're not expanding or maintaining your code then by all means write terrible code if it works, but if you're trying to release code to the public now that you want to keep updating, you need to make it easy to update.

dwazegek
Feb 11, 2005

WE CAN USE THIS :byodood:

Hughlander posted:

Wouldn't the reflector just name those in alphabetical order since that's the way they're initialized? IE: One becomes int aInt, TWO becomes int bInt or however reflector names it's variables?

No, the names of class members, even private ones, are preserved in the MSIL, so reflector can just show them as they're named.

I guess it should also be possible to see in which order they're initialized, and order them based on that, but I guess reflector never implemented that feature.

Zhentar
Sep 28, 2003

Brilliant Master Genius

YeOldeButchere posted:

Someday I hope we'll get to crack open Dwarf Fortress and get something even worse. I imagine it will be like opening the Ark of the Covenant.

I've done a fair bit of DF memory hacking and reversing. The greater difficulty of decompiling C++ of course means I didn't see nearly as much of it as people get with Minecraft or Terraria, but I never saw anything I really felt would be worthy of this thread.

It's definitely not awesome code; there are some huge monolithic functions, particularly for things like generating text for displays. The code page 437 stuff is a bit weird; strings are stored in cp-437, but in all the cases I came across they were converted to cp-1252 before use; I'm not sure that it's anything more than vestigial weirdness at this point. The creature class is ridiculously huge; if I remember correctly, last I looked at it each instance took 2000 bytes of memory with something like 70-100 vectors in it. In a number of cases there were pairs or triplets of vectors rather than a single vector of a struct or class (of course, those pairs or triplets could have been part of a struct themselves). It's clearly not unmaintainably convoluted though, since substantial changes have been made to it in several releases.

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...

dwazegek posted:

No, the names of class members, even private ones, are preserved in the MSIL, so reflector can just show them as they're named.

I guess it should also be possible to see in which order they're initialized, and order them based on that, but I guess reflector never implemented that feature.

I still don't see how this is possible. Code that initializes variables won't be reordered, it's just the declarations that are reordered - variable initialization is code, declarations are just syntax. And you can't make auto-initializers depend on other instance variables, as previously stated. I'm pretty sure that auto-initializers are just syntactic sugar that the compiler writes into the constructor anyway, so Reflector would just see the constructor code, not the fact that they're auto-initializers.

It shouldn't matter where you declare the instance variables in the class file, because the C# compiler does multiple passes. I declare instance variables all the time in random places when I'm writing parts of a class that really should be their own class, and you can forward-reference fine.

So I'm not sure how this feat is achieved. I don't have a copy of Terraria to decompile myself, but I'm sure that Reflector isn't giving us the full story...

Pollyzoid
Nov 2, 2010

GRUUAGH you say?
Posting a horror made by me, since it also happens to be slightly on topic. It's from my short-lived C# Minecraft library, networking API to be exact. Around that time I was experimenting with the fun parts of C#, like reflection, to see how much I could automate.

Some packet definition first...
code:
[Packet(ID = Packet.Login, Side = PacketSide.Client)]
public class LoginRequest : PacketBase
{
    public int Protocol { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public long MapSeed { get; set; }
    public byte Dimension { get; set; }
}
This part is from PacketBase and it's responsible for reading/writing packets from/to streams.

code:
public virtual void Read(NetworkStreamMC stream)
{
    foreach (var p in GetType().GetProperties().OrderBy(p => p.MetadataToken).Where(p => typeof(PacketBase).GetProperty(p.Name) == null))
    {
        p.SetValue(this, stream.GetType().InvokeMember(p.PropertyType.Name, BindingFlags.InvokeMethod, null, stream, null), null);
    }
}

protected virtual void Write(PacketWriter writer)
{
    foreach (var p in GetType().GetProperties().OrderBy(p => p.MetadataToken).Where(p => typeof(PacketBase).GetProperty(p.Name) == null))
    {
        writer.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, writer, new[] { p.GetValue(this, null) });
    }
}
So yes, I made the declaration order matter, thanks to ordering by MetadataToken which (supposedly) guarantees the order on all computers. In addition, it uses plenty of dynamic method invocation to read and write to the stream.
Most importantly, it was fun as hell to write and it cut the redundant manual read/write code from packets significantly. Although I'm surprised that it worked without problems.

beuges
Jul 4, 2005
fluffy bunny butterfly broomstick

Ryouga Inverse posted:

I still don't see how this is possible.

code:
    class blah
    {
        static int x = 1;
        int a = ++x;
        int b = ++x;
        int c = ++x;
    }

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...
Well, I learned something new and horrible about C# today.

ed:


Pollyzoid posted:

So yes, I made the declaration order matter, thanks to ordering by MetadataToken which (supposedly) guarantees the order on all computers. In addition, it uses plenty of dynamic method invocation to read and write to the stream.
Most importantly, it was fun as hell to write and it cut the redundant manual read/write code from packets significantly. Although I'm surprised that it worked without problems.

Actually I can't tell whether this is horrible or awesome. It kind of walks a fine line between elegance and insanity.

Dessert Rose fucked around with this message at 19:25 on May 26, 2011

Smugdog Millionaire
Sep 14, 2002

8) Blame Icefrog
It appears that field ordering in C# source is 'reflected' (heh heh heh) in the field ordering returned by reflection calls.
http://dotnetpad.net/ViewPaste/pnR8rOileUWDyUHNWOHMjQ

I don't know if that's coincidental or guaranteed, though.

dwazegek
Feb 11, 2005

WE CAN USE THIS :byodood:

Ryouga Inverse posted:

I'm pretty sure that auto-initializers are just syntactic sugar that the compiler writes into the constructor anyway, so Reflector would just see the constructor code, not the fact that they're auto-initializers.

It's not entirely syntactic sugar. Initializers of derived types run in the reverse order that the constructors run in, so there is a difference between initializing a member in the declaration or initializing it in the constructor. Of course, if you actually write code that depends on this behavior, it's most likely also a horror.

Say you have a class C that derives from B, that derives from A. If you instantiate C, then the initializers run in the order C, B, A, followed by the constructors in the order A, B, C.

You're right that this is achieved by sticking all the code in the constructor though, just not in a way that reflector can translate back to C#. Class C's constructor would look like

1. C's initializers
2. Call B's Constructor
3. C's constructor code

Pollyzoid
Nov 2, 2010

GRUUAGH you say?

Smugdog Millionaire posted:

It appears that field ordering in C# source is 'reflected' (heh heh heh) in the field ordering returned by reflection calls.
http://dotnetpad.net/ViewPaste/pnR8rOileUWDyUHNWOHMjQ

I don't know if that's coincidental or guaranteed, though.

This is what MSDN says:

quote:

The GetFields method does not return fields in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which fields are returned, because that order varies.

However, when I was playing around with reflection, I stumbled upon this:

David M Morton posted:

what it does is order the fields by the MetaDataToken, so they're guaranteed to be in the same order as you wrote them into the code

The guy is a moderator and an MVP on MSDN, so I trusted him on that.

Smugdog Millionaire
Sep 14, 2002

8) Blame Icefrog

Pollyzoid posted:

The guy is a moderator and an MVP on MSDN, so I trusted him on that.

Does that mean 'guaranteed by the current implementation' or 'guaranteed as part of the software contract'?

ozymandOS
Jun 9, 2004

Smugdog Millionaire posted:

Does that mean 'guaranteed by the current implementation' or 'guaranteed as part of the software contract'?

According to MSDN, the former. Depending on an implementation detail is a bad idea.

Scaevolus
Apr 16, 2007

Not to mention that reflection is very slow, so it's not the kind of thing you'd want to be doing in your networking code.

PhonyMcRingRing
Jun 6, 2002

Scaevolus posted:

Not to mention that reflection is very slow, so it's not the kind of thing you'd want to be doing in your networking code.

Reflection's not as terribly slow if you cache what it was you were looking for. Still not a good idea for anything requiring low latency/overhead.

Opinion Haver
Apr 9, 2007

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

Opinion Haver fucked around with this message at 07:57 on May 27, 2011

dwazegek
Feb 11, 2005

WE CAN USE THIS :byodood:

PhonyMcRingRing posted:

Reflection's not as terribly slow if you cache what it was you were looking for. Still not a good idea for anything requiring low latency/overhead.

If performance is a problem, you can use the System.Linq.Expressions stuff to generate a bunch delegates that do the real work. The initial cost could be pretty high, but even that can be mitigated by compiling it to an assembly and saving that to disk.

If you're on an older version of .NET that doesn't have Expressions, you could use System.Reflection.Emit to do the same thing, but that's a quite a bit more work.

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

God of Mischief
Oct 22, 2010
code:
public String displayCenter(ResultSet resultset)
{
    String var = "";
    var = var + 
          "<br /><div style=\"position:relative;width:auto;height:250px;padding:5px;background-color:red; \">" +
          "<div class=\"bookCoverImage\">" +
          "<img class=\"bookCoverImage\"src=\"" + resultset.getString(5) + "\" alt=\"book cover\"/>" +
          "</div>" +            
          "<div style=\"position:relative; left:160px;top:-190px; height:200px;width:800px;background-color:yellow; \">" +
          resultset.getString(1) + "<br />" +  //title
          resultset.getString(2) + " " + resultset.getString(3) +  "<br />" + //author
          "<div style=\"font-size:12px; width:auto;\">" +            
          resultset.getString(4) + "<br />" + //description
          "</div>" + "</div>" +
          "<div style=\"position:relative;right:-860px;top:-180px;width:100px;background-color:pink; \">$" + 
          resultset.getString(6) +
          " " + "<button type=\"button\">Add</button>" + "</div>" +"</div>";

    return var;
}
Granted, this is just a school project, but I do not think my team understands the concept of keeping code separate from presentation from styling from sql from... :bang:

Impotence
Nov 8, 2010
Lipstick Apathy
course you can

code:
<style>
div[style='position:relative; left:160px;top:-190px; height:200px;width:800px;background-color:yellow'] {
	position: absolute!important; 
	top: 0!important; 
	left: 0!important; 
	height:800px!important;
	width: 200px!important;
	background-color: green!important;
}
</style>

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.
FizzBuzz is very important, therefore it deserves its own language primitive: http://code.google.com/p/hq9pf/

Moxie Omen
Mar 15, 2008

Found this at work a while ago

code:
function prettyDate ($date) {
   $pos1 = strpos($date,'-');
   $pos2 = strrpos($date,'-');
   $day = substr($date,0,$pos1);
   $month = substr($date,$pos1+1,$pos2-$pos1-1);
   $year = substr($date,$pos2+1);
   $month = strtolower($month);
   $month =ucfirst($month);
   $pretty_date = $month." ".$day.", ".$year;
   return $pretty_date;
}
hmmmmmmmmmmmmmmm........

GROVER CURES HOUSE
Aug 26, 2007

Go on...
http://thedailywtf.com/Comments/The-Disgruntled-Bomb.aspx?pg=2#340740

pre:
C#:

static SomeCtor()
{
  typeof(string).GetField("Empty").SetValue(null, " ");
}
//Sets string.Empty to a space character
:2bong:

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Anyone who actually uses string.Empty deserves it.

GROVER CURES HOUSE
Aug 26, 2007

Go on...

Plorkyeran posted:

Anyone who actually uses string.Empty deserves it.

Also, this is what String.Empty looks like on the inside: public static readonly string Empty = "";

Static readonly? :smug:

dwazegek
Feb 11, 2005

WE CAN USE THIS :byodood:

Broken Knees Club posted:

Also, this is what String.Empty looks like on the inside: public static readonly string Empty = "";

Static readonly? :smug:

Which is weird because you can't change non-static readonly members through reflection, only static ones.

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.
http://forums.somethingawful.com/showthread.php?=PHPE9568F34-D428-11d2-A769-00AA001ACF42
php. Works on any PHP script.

Zend logo: PHPE9568F35-D428-11d2-A769-00AA001ACF42
PHP credits: PHPB8B5F2A0-3C92-11d3-A3A9-4C7B08C10000

NotShadowStar
Sep 20, 2000
What the hell is going on there? That a bizarre dark corner of PHP I haven't seen yet.

Meat Beat Agent
Aug 5, 2007

felonious assault with a sproinging boner

NotShadowStar posted:

What the hell is going on there? That a bizarre dark corner of PHP I haven't seen yet.

http://phpsadness.com/?page=sad/11

I don't know if it's been linked here before but that entire site is full of things well worthy for the coding horrors thread. Some of them are obvious (like inconsistencies in function names/arguments) but some of them are just bizarre.

I like this one:

quote:

E_ALL doesn't actually mean "E_ALL"; it means E_ACTUALLY_ALL & ~E_STRICT. Shouldn't it be named E_ALMOST_ALL instead? Why isn't there a real E_ALL named constant?

Meat Beat Agent fucked around with this message at 16:38 on May 28, 2011

ToxicFrog
Apr 26, 2008


I'm fond of "exception thrown without stack frame" myself. :wtc:

GROVER CURES HOUSE
Aug 26, 2007

Go on...
My favorite: Can point a variable into raw memory with array_walk and debug_backtrace.

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.

daft punk railroad posted:

http://phpsadness.com/?page=sad/11

I don't know if it's been linked here before but that entire site is full of things well worthy for the coding horrors thread. Some of them are obvious (like inconsistencies in function names/arguments) but some of them are just bizarre.
The site is also a horror itself: http://phpsadness.com/?page=sad/18

quote:

Static variables in methods are bound to the class, not instances of the class.

That is, if a method has a static variable, it is global to all instances of the class.
Well, yeah. What the gently caress did you think static variables were for?

tef
May 30, 2004

-> some l-system crap ->
php programmers can't even whine about the language right, let alone use it well

NotShadowStar
Sep 20, 2000
Holy poo poo they are literally using goto: in the C code of PHP

http://phpsadness.com/?page=sad/36

krisis
Oct 25, 2003

i have a light case of asparagus.
Using goto isn't a horror in itself. It's nice when you need to do cleanup on the way out of a function when aborting, like in that snippet.

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.
That #if for the first argument's type probably has some monstrous consequences elsewhere.

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