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
Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

IMlemon posted:

One of the core abstractions in the system i'm working on right now.

Java code:
/**
 * This class encapsulates information for a message 
*/
public abstract class BaseMessage {
    
}
Maybe I should find another job.

I'm not seeing anything too awful here. You're creating a base class from which all messages shall derive, even if it does nothing by itself.

Adbot
ADBOT LOVES YOU

Bognar
Aug 4, 2011

I am the queen of France
Hot Rope Guy
There are libraries that depend on empty base classes/interfaces to identify if the object is something the user intends the library to interact with.

Jewel
May 2, 2009

Volmarias posted:

I'm not seeing anything too awful here. You're creating a base class from which all messages shall derive, even if it does nothing by itself.

Generally you'd want the message to have at least one abstract/virtual function or piece of data or SOMETHING though right? Otherwise you don't really have any way to tie together why the messages need a base class in the first place? If it's to store them all in a specific typed queue or something then you still can't do anything with them because you can't call anything that they have in common.

Edit: vvv Would it even make sense as an empty interface? You still need functions or data to interface with.

Jewel fucked around with this message at 14:40 on Oct 18, 2013

pigdog
Apr 23, 2004

by Smythe

Bognar posted:

There are libraries that depend on empty base classes/interfaces to identify if the object is something the user intends the library to interact with.

It would make some sense if it were an empty interface, but an empty abstract class imposes itself on the inheritance chain for no benefit whatsoever.

Bognar
Aug 4, 2011

I am the queen of France
Hot Rope Guy

Jewel posted:

Edit: vvv Would it even make sense as an empty interface? You still need functions or data to interface with.

Not if you're using it solely as an identifier. There are also some other strange things you could do with it in C# with extension methods. If you, for some reason, had functionality that could be used on any object (e.g. a GetHashCode() wrapper) but you only wanted it to appear on certain objects, you could have those objects inherit from an empty interface and define an extension method for that interface.


pigdog posted:

It would make some sense if it were an empty interface, but an empty abstract class imposes itself on the inheritance chain for no benefit whatsoever.

Yeah, an empty abstract class doesn't make too much sense because there's no reason not to use an empty interface at that point.

return0
Apr 11, 2007

Bognar posted:

Not if you're using it solely as an identifier. There are also some other strange things you could do with it in C# with extension methods. If you, for some reason, had functionality that could be used on any object (e.g. a GetHashCode() wrapper) but you only wanted it to appear on certain objects, you could have those objects inherit from an empty interface and define an extension method for that interface.

Doing this totally sucks and is a pet hate of mine. "Marker" interfaces indeed; terrible. If you want a GetHashCode or a ToString on some interface, put it on the interface?

Macichne Leainig
Jul 26, 2012

by VG
I'm not going to post the code as to not reveal industry secrets, but right now I'm looking at a caching fuckup.

Basically, we deal with loans - each loan has its own object and metadata object attached. This guy was caching the metadata the first time you retrieve it from the server, and never updated the cache, which means that every time you went and got a list of loan objects, it was a snapshot of loans that existed at that point in time, not live data.

Yeah, our business end users really freaked out. :psyduck:

Bognar
Aug 4, 2011

I am the queen of France
Hot Rope Guy

return0 posted:

Doing this totally sucks and is a pet hate of mine. "Marker" interfaces indeed; terrible. If you want a GetHashCode or a ToString on some interface, put it on the interface?

The point isn't to force the object to implement the functionality, but to provide the functionality and choose the object to use it on. It's like a gimped C# mixin.

shodanjr_gr
Nov 20, 2007

pigdog posted:

It would make some sense if it were an empty interface, but an empty abstract class imposes itself on the inheritance chain for no benefit whatsoever.

Why? In C++ this is a common pattern for type checking (your message handler accepts a BaseMessage* or whatever) and allows you to add additional functionality to the hierarchy without having to substantially refactor your code later...

Is this a C#-specific peeve?

Amarkov
Jun 21, 2010

shodanjr_gr posted:

Why? In C++ this is a common pattern for type checking (your message handler accepts a BaseMessage* or whatever) and allows you to add additional functionality to the hierarchy without having to substantially refactor your code later...

Is this a C#-specific peeve?

C#, like Java, does not support multiple inheritance from non-interface classes. So making an empty abstract base class severely limits your ability to modify things.

C++ does support multiple inheritance, so it's not an issue. (Does C++ even have the concept of an interface, distinct from a pure virtual class?)

shodanjr_gr
Nov 20, 2007

Amarkov posted:

C#, like Java, does not support multiple inheritance from non-interface classes. So making an empty abstract base class severely limits your ability to modify things.

C++ does support multiple inheritance, so it's not an issue. (Does C++ even have the concept of an interface, distinct from a pure virtual class?)
Ah fair enough, that makes perfect sense then. And yes C++ does not have a distinct interface concept.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

IMlemon posted:

Java code:
/**
 * This class encapsulates information for a message 
*/
public abstract class BaseMessage {
    
}

Every once in a while I find myself using the word "encapsulates" in the description of a class.

Then I stop and go outside for a few minutes.

JawnV6
Jul 4, 2004

So hot ...
Enclassulate

Fuck them
Jan 21, 2011

and their bullshit
:yotj:
Those services and VMs that got obliterated back when I said the client for my company lost those services and VMs?

Still down :toot:

ALWAYS HAVE BACKUPS YA GOOFS

Dr Monkeysee
Oct 11, 2002

just a fox like a hundred thousand others
Nap Ghost

Bognar posted:

Not if you're using it solely as an identifier. There are also some other strange things you could do with it in C# with extension methods. If you, for some reason, had functionality that could be used on any object (e.g. a GetHashCode() wrapper) but you only wanted it to appear on certain objects, you could have those objects inherit from an empty interface and define an extension method for that interface.

If you just want an identifier you'd probably want to use an attribute in C#. An added advantage is it implies no inheritance or implementation relationship at all.

This wouldn't allow you to do weird backdoor mixins like you proposed but that seems like kind of a horror anyway.

Bognar
Aug 4, 2011

I am the queen of France
Hot Rope Guy

Monkeyseesaw posted:

If you just want an identifier you'd probably want to use an attribute in C#. An added advantage is it implies no inheritance or implementation relationship at all.

Yep, Microsoft has a static analysis rule that states exactly that.

http://msdn.microsoft.com/en-us/library/ms182128%28v=VS.100%29.aspx

quote:

If your design includes empty interfaces that types are expected to implement, you are probably using an interface as a marker or a way to identify a group of types. If this identification will occur at run time, the correct way to accomplish this is to use a custom attribute. Use the presence or absence of the attribute, or the properties of the attribute, to identify the target types. If the identification must occur at compile time, then it is acceptable to use an empty interface.

And yet, Microsoft doesn't follow their own advice (as usual).

http://msdn.microsoft.com/en-us/library/system.web.sessionstate.irequiressessionstate.aspx

http://msdn.microsoft.com/en-us/library/system.web.ui.inamingcontainer.aspx

1337JiveTurkey
Feb 17, 2005

A marker interface with a type parameter could be useful in some circumstances. If a class extends the interface with a specific type, then that type can be retrieved at runtime through reflection. It can also be used to restrict the types of interfaces extending the interface. For instance:

code:
interface Parent<PT>
interface Child1<C1T> extends Parent<C1T>
interface Child2<C2T> extends Parent<C2T>
class ChildImpl1 implements Child1<Foo>, Child2<Foo> // Allowed
class ChildImpl2 implements Child1<Foo>, Child2<Bar> // Disallowed

Dr Monkeysee
Oct 11, 2002

just a fox like a hundred thousand others
Nap Ghost
That sounds like something a C++ programmer would attempt when forced to use C#. You could probably accomplish the same thing, clearer, using type constraints.

ManoliIsFat
Oct 4, 2002


If instead of an empty abstract class or interface, if you decorated all your classes with an attribute...how are you forcing whatever consistency or grouping you want? Reflection all over the place to check? I guess you could make an extension method to object that boolean returns if it has that attribute or not.

ManoliIsFat fucked around with this message at 01:44 on Oct 19, 2013

Sedro
Dec 31, 2008

ManoliIsFat posted:

If instead of an empty abstract class or interface, if you decorated all your classes with an attribute...how are you forcing whatever consistency or grouping you want? Reflection all over the place to check? I guess you could make an extension method to object that boolean returns if it has that attribute or not.
You can choose whether an attribute is inherited or not. A marker interface is always inherited. You are using reflection either way.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
Well, you can enforce at compile-time that a function only accepts parameters with the appropriate marker interface.

dwazegek
Feb 11, 2005

WE CAN USE THIS :byodood:

Jabor posted:

Well, you can enforce at compile-time that a function only accepts parameters with the appropriate marker interface.

Yeah, the last sentence of the explanation of the FxCop rule even mentions this.

There's pros and cons to either way of doing it and it's up to the developer to decide which is best for them. This FxCop rule should only be seen as a reminder that you might want to use an attribute, not as a "X is the wrong way, Y is the right way" type of rule.

omeg
Sep 3, 2012

Keep in mind that checking for attributes is literally orders of magnitude slower than classes/interfaces. But that shouldn't matter unless you write game components or something.

1337JiveTurkey
Feb 17, 2005

Monkeyseesaw posted:

That sounds like something a C++ programmer would attempt when forced to use C#. You could probably accomplish the same thing, clearer, using type constraints.

You could accomplish something similar using type constraints, but not that in particular. There's no inheritance relationship between Child1 and Child2, so implementing one doesn't require implementing the other. There's no limitations on the type parameters for either the parent or child interfaces. However iff you implement both child interfaces, then they must be the same type. This technique can be used in addition to type constraints to impose additional constraints on implementers of multiple interfaces.

code:
interface Parent<PT>
interface Child1<C1T extends Comparable<C1T>> extends Parent<C1T>
interface Child2<C2T extends AutoCloseable> extends Parent<C2T>

class Foo implements Comparable<Foo>, AutoCloseable
class Bar implements Comparable<Foo>
class Baz implements AutoCloseable

class ChildImpl1 implements Child1<Foo>, Child2<Foo> // Allowed (same type implements both interfaces)
class ChildImpl2 implements Child1<Foo>, Child2<Bar> // Disallowed (different types)
class ChildImpl3 implements Child1<Bar>, Child2<Bar> // Disallowed (not AutoCloseable)
class ChildImpl4 implements Child1<Baz>, Child2<Baz> // Disallowed (not Comparable<Foo>)

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.
XKCD from a few days ago:



The mouseover text is especially horrible. :cheeky:

quote:

If replacing all the '3's doesn't fix your code, remove the 4s, too, with 'ceiling(pi) / floor(pi) * pi * r^floor(pi)'. Mmm, floor pie.

KaneTW
Dec 2, 2011

It's... a joke? Why would you post it here?

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.

KaneTW posted:

It's... a joke? Why would you post it here?

Coding horrors: post the code that makes you laugh (or cry)

It seemed funny, and it is code. :smith: Its made up, but so are some of the things we post here. If you found that in the wild, it'd be a horror.

megalodong
Mar 11, 2008

Well xkcd is certainly a horror.

KaneTW
Dec 2, 2011

Zaphod42 posted:

Coding horrors: post the code that makes you laugh (or cry)

It seemed funny, and it is code. :smith: Its made up, but so are some of the things we post here. If you found that in the wild, it'd be a horror.

Fair enough, I just thought you were one of the people who go "XKCD is bad :hurr:"

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
It is though, and that "joke" proves it

Dr Monkeysee
Oct 11, 2002

just a fox like a hundred thousand others
Nap Ghost
XKCD is ok and his what if blog is pretty interesting. He hasn't done a comic about ~*Megan*~ in years so the strip is basically free of goony desperation.

Sinestro
Oct 31, 2010

The perfect day needs the perfect set of wheels.
They got married and then she got cancer.

dwazegek
Feb 11, 2005

WE CAN USE THIS :byodood:
code:
public bool IsNumeric(object numberString)
{
  char[] ca = numberString.ToString().ToCharArray();
  for (int i = 0; i < ca.Length; i++)
  {
    if (!char.IsNumber(ca[i]))
      if (ca[i] != '.')
        return false;
  }
  if (numberString.ToString().Trim() == "")
    return false;
  return true;
}
I guess it's not the worst piece of code in the world, but still...

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

an insufferable third party posted:

How am I supposed to know if my API works if you haven't given me a working consumer of it?

Today is going to be fun. :downsgun:

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

leper khan posted:

Today is going to be fun. :downsgun:

"How do I know my consumer will work if you don't give me an API?"

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

leper khan posted:

Today is going to be fun. :downsgun:

The correct answer is "your unit tests".

Bognar
Aug 4, 2011

I am the queen of France
Hot Rope Guy

Ithaqua posted:

The correct answer is "your unit tests".

I'm just gonna take a stab in the dark and assume that they don't have any.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
If a third-party is asking about your API, I think it's more than acceptable to ask for an example of a consumer for documentation and evaluation purposes.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
So, running over some old code dealing with our calendar API and found CamelDiscoDiary.

That's a drat good class name.

Adbot
ADBOT LOVES YOU

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

Suspicious Dish posted:

If a third-party is asking about your API, I think it's more than acceptable to ask for an example of a consumer for documentation and evaluation purposes.

A third party is /providing/ an API, and doesn't know if it works because I haven't provided them a working client application.

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