Author Topic: Tell me what's wrong with this code and win a keycap!  (Read 9521 times)

0 Members and 1 Guest are viewing this topic.

Offline keyboardlover

  • Thread Starter
  • Posts: 4022
  • Hey Paul Walker, Click It or Ticket!
    • http://www.keyboardlover.com
Tell me what's wrong with this code and win a keycap!
« on: Fri, 05 August 2011, 14:51:12 »
A kid at work found this today (posted online in a tutorial) and we were cracking up about it.

[ Attachment Invalid Or Does Not Exist ] 23375[/ATTACH]

Explain why this code is terrible and you win a keycap of my choosing!

Edit: Congrats to daemonraccoon and hashbaz! PM me your addresses and I'll send you each a keycap of my choosing.

Also, two big THUMBS UP to findecanor who made a lot of great points about software design!

daemonraccoon's answer was pretty near exactly what I was looking for:
Quote from: DaemonRaccoon;393744
Then the fact that it [the save method] is declared inside of its class and therefore local in scope.


hashbaz got 2nd place since he essentially said this in different words:
Quote from: hashbaz;393756
Thinking about the MVC clue and daemonraccoon's comment ... is it that the Save() method is too specific to be useful to a controller class? (assuming that the controller deals with many object types, including People).


Findecanor was very close but he didn't really explain the "why":
Quote from: Findecanor;393727

I would also make "save()" a virtual member function with basic functionality in a superclass.
« Last Edit: Fri, 05 August 2011, 19:30:04 by keyboardlover »

Offline hashbaz

  • Grand Ancient One
  • * Moderator Emeritus
  • Posts: 5057
  • Location: SF Bae Area
Tell me what's wrong with this code and win a keycap!
« Reply #1 on: Fri, 05 August 2011, 15:10:15 »
Is this C#?  I'm guessing the getters and setters are redundant, since these members are public already and are all intended to be read-write.

Offline keyboardlover

  • Thread Starter
  • Posts: 4022
  • Hey Paul Walker, Click It or Ticket!
    • http://www.keyboardlover.com
Tell me what's wrong with this code and win a keycap!
« Reply #2 on: Fri, 05 August 2011, 15:12:12 »
Yes it's C# and the getters and setters are not the issue.

It's not super obvious at first but once you realize it you have to laugh.
« Last Edit: Fri, 05 August 2011, 15:15:33 by keyboardlover »

Offline keyboardlover

  • Thread Starter
  • Posts: 4022
  • Hey Paul Walker, Click It or Ticket!
    • http://www.keyboardlover.com
Tell me what's wrong with this code and win a keycap!
« Reply #3 on: Fri, 05 August 2011, 15:13:45 »
Quote from: ripster;393702
Beware.  He might send you a \KL key.

Fixed the slash for you.

Offline hashbaz

  • Grand Ancient One
  • * Moderator Emeritus
  • Posts: 5057
  • Location: SF Bae Area
Tell me what's wrong with this code and win a keycap!
« Reply #4 on: Fri, 05 August 2011, 15:22:19 »
Hmmm.  From Microsoft's documentation: "Static methods and properties cannot access non-static fields and events in their containing type."  The Save() method is therefore useless?

Offline keyboardlover

  • Thread Starter
  • Posts: 4022
  • Hey Paul Walker, Click It or Ticket!
    • http://www.keyboardlover.com
Tell me what's wrong with this code and win a keycap!
« Reply #5 on: Fri, 05 August 2011, 15:29:20 »
Quote from: hashbaz;393709
Hmmm.  From Microsoft's documentation: "Static methods and properties cannot access non-static fields and events in their containing type."  The Save() method is therefore useless?

That is correct, but not the issue. There are no events in the example I gave.

It's really much simpler than that.

Offline Dox

  • Posts: 312
Tell me what's wrong with this code and win a keycap!
« Reply #6 on: Fri, 05 August 2011, 15:33:14 »
The Save method should just be
public static void Save()
{
    //TODO : Add Some Stuff To Save person...
}
ErgoDox x2 | DoxKB x2 |   IBM SSK   | HHKB pro2

Offline keyboardlover

  • Thread Starter
  • Posts: 4022
  • Hey Paul Walker, Click It or Ticket!
    • http://www.keyboardlover.com
Tell me what's wrong with this code and win a keycap!
« Reply #7 on: Fri, 05 August 2011, 15:33:49 »
Nope, it's simpler than that.

(Well not as simple as Ripster's idea).

Offline hashbaz

  • Grand Ancient One
  • * Moderator Emeritus
  • Posts: 5057
  • Location: SF Bae Area
Tell me what's wrong with this code and win a keycap!
« Reply #8 on: Fri, 05 August 2011, 15:36:08 »
I dunno man.  I mean, Save() doesn't need to be static at all.  Does C# have "saving" built in?

Offline kps

  • Posts: 410
Tell me what's wrong with this code and win a keycap!
« Reply #9 on: Fri, 05 August 2011, 15:36:43 »
Quote from: keyboardlover;393686
Explain why this code is terrible and you win a keycap of my choosing!


It's C#.

Offline Dox

  • Posts: 312
Tell me what's wrong with this code and win a keycap!
« Reply #10 on: Fri, 05 August 2011, 15:36:53 »
Well, if that method is intended to save the current Person instance, the Person parameter is totally useless.
ErgoDox x2 | DoxKB x2 |   IBM SSK   | HHKB pro2

Offline keyboardlover

  • Thread Starter
  • Posts: 4022
  • Hey Paul Walker, Click It or Ticket!
    • http://www.keyboardlover.com
Tell me what's wrong with this code and win a keycap!
« Reply #11 on: Fri, 05 August 2011, 15:39:19 »
Quote from: kps;393722
It's C#.

Hahaha

Keep trying guys =)

Offline Findecanor

  • Posts: 5082
  • Location: Koriko
Tell me what's wrong with this code and win a keycap!
« Reply #12 on: Fri, 05 August 2011, 15:42:11 »
I am not very good at C# so there might be something that I missed, but if it was like this in C++, Java, Ruby, Python or any other language that I know, I would never design it like this.
The name "Person" is not what I would choose for a class with this interface. The name is too generic.
Either you make a class whose name, interface and functionality reflect the role that the person has in the system, or you make the "Person" instances be minimal objects and have other objects for roles and/or relations that refer to them.

Second... The address fields are much too restrictive. I would put the fields inside an "Address" class and add fields for "state" and "country".

Third, it would also be good to have some kind of validation, so that you don't "save" empty instances. Put validation of addresses inside the "Address" class.

I would also make "save()" a virtual member function with basic functionality in a superclass.

When dealing with resources outside your program -- and persistent storage is outside your program -- then you must also be able to handle that the operation could fail. At the very least, make "save()" return an error code, but it might be more appropriate in this context to thrown an exception.

Anyway.. This is from an example, so some may be excused -- if they are outside the point of the example. If this code snippet had been at the centre of the example, then it would have been more serious.
« Last Edit: Fri, 05 August 2011, 15:50:55 by Findecanor »
🍉

Offline ch_123

  • * Exalted Elder
  • Posts: 5860
Tell me what's wrong with this code and win a keycap!
« Reply #13 on: Fri, 05 August 2011, 15:43:54 »
Why would you have a static method for the object type defined in the class?

Offline keyboardlover

  • Thread Starter
  • Posts: 4022
  • Hey Paul Walker, Click It or Ticket!
    • http://www.keyboardlover.com
Tell me what's wrong with this code and win a keycap!
« Reply #14 on: Fri, 05 August 2011, 15:47:29 »
Findecanor is the closest yet. He mentioned part of the issue.

Here's a hint, if you're familiar with "model" centered design (mvc, mvvm, etc.) If person is your model, think about how you're going to use that model elsewhere in your application. How are you going to handle making changes to it?

Right now I like Findecanors explanation best. I'll give you guys some more time and explain why later.
« Last Edit: Sat, 06 August 2011, 16:07:56 by keyboardlover »

Offline keyboardlover

  • Thread Starter
  • Posts: 4022
  • Hey Paul Walker, Click It or Ticket!
    • http://www.keyboardlover.com
Tell me what's wrong with this code and win a keycap!
« Reply #15 on: Fri, 05 August 2011, 15:56:14 »
Also, as Findecanor alluded to, it's a design issue. The issue has nothing to do with syntax, framework, etc.

Offline DaemonRaccoon

  • Posts: 333
Tell me what's wrong with this code and win a keycap!
« Reply #16 on: Fri, 05 August 2011, 15:58:38 »
The fact that the Save() is static?
122-Key Model F 6110345 1985-03-01 | Model M SSK 1391472 1991-01-22 | Rosewill RK-9000 v1 | KBC Poker X | Filco FKBN87M/PWE2

Offline keyboardlover

  • Thread Starter
  • Posts: 4022
  • Hey Paul Walker, Click It or Ticket!
    • http://www.keyboardlover.com
Tell me what's wrong with this code and win a keycap!
« Reply #17 on: Fri, 05 August 2011, 16:02:14 »
Nope.

Offline Findecanor

  • Posts: 5082
  • Location: Koriko
Tell me what's wrong with this code and win a keycap!
« Reply #18 on: Fri, 05 August 2011, 16:05:41 »
My other points are still valid -- not just the ones that you are referring to. ;)

Yes, the class is incomplete. If the data is supposed to live in persistent storage as a (relational or non-relational) table then you also need some way of loading the data entries. Perhaps some lookup function. That is... if the purpose of the "save()" function isn't just to dump the state to be read while debugging the program.
🍉

Offline DaemonRaccoon

  • Posts: 333
Tell me what's wrong with this code and win a keycap!
« Reply #19 on: Fri, 05 August 2011, 16:06:45 »
Then the fact that it is declared inside of its class and therefore local in scope.
122-Key Model F 6110345 1985-03-01 | Model M SSK 1391472 1991-01-22 | Rosewill RK-9000 v1 | KBC Poker X | Filco FKBN87M/PWE2

Offline keyboardlover

  • Thread Starter
  • Posts: 4022
  • Hey Paul Walker, Click It or Ticket!
    • http://www.keyboardlover.com
Tell me what's wrong with this code and win a keycap!
« Reply #20 on: Fri, 05 August 2011, 16:08:56 »
Quote from: DaemonRaccoon;393744
Then the fact that it is declared inside of its class and therefore local in scope.

What is "it" - the save method?

Offline ch_123

  • * Exalted Elder
  • Posts: 5860
Tell me what's wrong with this code and win a keycap!
« Reply #21 on: Fri, 05 August 2011, 16:13:20 »
Is this the entirety of the class definition? If so, where's the constructor?

(Unless there's some sort of implicit thing going on, I only have very basic experience with C#)

Offline DaemonRaccoon

  • Posts: 333
Tell me what's wrong with this code and win a keycap!
« Reply #22 on: Fri, 05 August 2011, 16:14:55 »
Quote from: keyboardlover;393746
What is "it" - the save method?

Yes.
122-Key Model F 6110345 1985-03-01 | Model M SSK 1391472 1991-01-22 | Rosewill RK-9000 v1 | KBC Poker X | Filco FKBN87M/PWE2

Offline keyboardlover

  • Thread Starter
  • Posts: 4022
  • Hey Paul Walker, Click It or Ticket!
    • http://www.keyboardlover.com
Tell me what's wrong with this code and win a keycap!
« Reply #23 on: Fri, 05 August 2011, 16:14:59 »
No, but the lack of constructor is regardless of the issue.

Edit: DaemonRaccoon got it. Congrats!

Offline hashbaz

  • Grand Ancient One
  • * Moderator Emeritus
  • Posts: 5057
  • Location: SF Bae Area
Tell me what's wrong with this code and win a keycap!
« Reply #24 on: Fri, 05 August 2011, 16:15:32 »
Thinking about the MVC clue and daemonraccoon's comment ... is it that the Save() method is too specific to be useful to a controller class? (assuming that the controller deals with many object types, including People).  In other words, Save() should not be part of the Person class, or it should be a member of a base class that is overridden by Person?

Offline DaemonRaccoon

  • Posts: 333
Tell me what's wrong with this code and win a keycap!
« Reply #25 on: Fri, 05 August 2011, 16:16:20 »
Yay! I win!
122-Key Model F 6110345 1985-03-01 | Model M SSK 1391472 1991-01-22 | Rosewill RK-9000 v1 | KBC Poker X | Filco FKBN87M/PWE2

Offline keyboardlover

  • Thread Starter
  • Posts: 4022
  • Hey Paul Walker, Click It or Ticket!
    • http://www.keyboardlover.com
Tell me what's wrong with this code and win a keycap!
« Reply #26 on: Fri, 05 August 2011, 16:16:41 »
Quote from: hashbaz;393756
Thinking about the MVC clue and daemonraccoon's comment ... is it that the Save() method is too specific to be useful to a controller class? (assuming that the controller deals with many object types, including People).  In other words, Save() should not be part of the Person class, or it should be a member of a base class that is overridden by Person?

Yes, this is correct. I'll give you and daemonraccoon both a keycap.

Like seriously, what are you going to do?

if...
Person myPerson = new Person();
myPerson.Save(); ?????

Bad, bad design. Put the Save() in a DataAccess class and fuggedaboutit!

Offline keyboardlover

  • Thread Starter
  • Posts: 4022
  • Hey Paul Walker, Click It or Ticket!
    • http://www.keyboardlover.com
Tell me what's wrong with this code and win a keycap!
« Reply #27 on: Fri, 05 August 2011, 16:19:21 »
Btw, the kid at work who found this rocks. Only 1.5 yrs experience. =)

He's helped me big time on a couple projects.

Offline hashbaz

  • Grand Ancient One
  • * Moderator Emeritus
  • Posts: 5057
  • Location: SF Bae Area
Tell me what's wrong with this code and win a keycap!
« Reply #28 on: Fri, 05 August 2011, 16:38:57 »
Sweet, what is our prize?

Offline keyboardlover

  • Thread Starter
  • Posts: 4022
  • Hey Paul Walker, Click It or Ticket!
    • http://www.keyboardlover.com
Tell me what's wrong with this code and win a keycap!
« Reply #29 on: Fri, 05 August 2011, 16:54:30 »
Great work guys. Hope you found this fun. Congrats to daemonraccoon and hashbaz! PM me your addresses and I'll send you each a keycap of my choosing.

Also, two big THUMBS UP to findecanor who made a lot of great points about software design!

Now that you guys understand the issue, you have to laugh when you look at this code again :D

Ripster doesn't find it funny, but that's because he can't write code :D

Offline DaemonRaccoon

  • Posts: 333
Tell me what's wrong with this code and win a keycap!
« Reply #30 on: Fri, 05 August 2011, 17:08:07 »
@keyboardlover: PM'd with my address.
122-Key Model F 6110345 1985-03-01 | Model M SSK 1391472 1991-01-22 | Rosewill RK-9000 v1 | KBC Poker X | Filco FKBN87M/PWE2

Offline hashbaz

  • Grand Ancient One
  • * Moderator Emeritus
  • Posts: 5057
  • Location: SF Bae Area
Tell me what's wrong with this code and win a keycap!
« Reply #31 on: Fri, 05 August 2011, 17:09:20 »
Thanks KL, this has been fun and edumacational.

Offline keyboardlover

  • Thread Starter
  • Posts: 4022
  • Hey Paul Walker, Click It or Ticket!
    • http://www.keyboardlover.com
Tell me what's wrong with this code and win a keycap!
« Reply #32 on: Fri, 05 August 2011, 17:21:21 »
It's funny cuz it's such a subtle issue. You don't notice it at first. :D

Offline csm725

  • Posts: 276
  • I BLEED RED AND GOLD
    • http://csm725.com
Tell me what's wrong with this code and win a keycap!
« Reply #33 on: Fri, 05 August 2011, 17:32:40 »
Can we have a simpler one of these? My C# skills are degrading.

Offline keyboardlover

  • Thread Starter
  • Posts: 4022
  • Hey Paul Walker, Click It or Ticket!
    • http://www.keyboardlover.com
Tell me what's wrong with this code and win a keycap!
« Reply #34 on: Fri, 05 August 2011, 18:04:56 »
Maybe one of the other code ninjas here can host the next one? :D

Edit: Updated the OP with more info about the winner's answers.
« Last Edit: Fri, 05 August 2011, 19:33:56 by keyboardlover »

woody

  •  Guest
Tell me what's wrong with this code and win a keycap!
« Reply #35 on: Sat, 06 August 2011, 09:14:03 »
Quote from: keyboardlover;393686
(Attachment Link) 23375[/ATTACH]

Explain why this code is terrible and you win a keycap of my choosing!

kps nailed it before me due to different timezone.

Also:
Code: [Select]
// TODO : Add some stuff to save the person.
Jesus saves - all others pay cash.

Offline keyboardlover

  • Thread Starter
  • Posts: 4022
  • Hey Paul Walker, Click It or Ticket!
    • http://www.keyboardlover.com
Tell me what's wrong with this code and win a keycap!
« Reply #36 on: Sat, 06 August 2011, 09:29:12 »
Wrong and wrong.

Language geeks. Wouldn't be able to recognize a design problem if it crawled out and bit them!

Offline Hak Foo

  • Posts: 1272
  • Make America Clicky Again!
Tell me what's wrong with this code and win a keycap!
« Reply #37 on: Sat, 06 August 2011, 12:15:58 »
Shouldn't you just serialize the person and leave how you wish to save it as a choice fopr the code that uses the class?

Of course, it could be a naming convention thing, perhaps related to transaction-style behaviour:

person a = new Person(); //a has default values
a.setName("blah"); //blah is stored in a pending fashion, but not yet available if you do getName
Person:save(a); // only now does the above change take effect.
Overton130, Box Pale Blues.

Offline keyboardlover

  • Thread Starter
  • Posts: 4022
  • Hey Paul Walker, Click It or Ticket!
    • http://www.keyboardlover.com
Tell me what's wrong with this code and win a keycap!
« Reply #38 on: Sat, 06 August 2011, 12:34:55 »
Sure, but regardless, saving person will take place elsewhere in the business logic and likely use some sort of dataaccess class to do so. The point is that person and save have nothing to do with each other and therefore should be decoupled.

Offline Multiple

  • Posts: 40
Tell me what's wrong with this code and win a keycap!
« Reply #39 on: Sat, 06 August 2011, 20:04:14 »
It uses 16bit per character strings, that's likely a huge waste of space, since near half of the available resources could be occupied with nothing. Check if unicode is actually needed at input, add flags...

It uses one object for each person. With object overhead, allocation time and GC time to considering, this Person class can not be used en mass.

So it does not scale.