Presentation is loading. Please wait.

Presentation is loading. Please wait.

PerlDreamer Consulting, LLC1 Common WebGUI Programming Mistakes.

Similar presentations


Presentation on theme: "PerlDreamer Consulting, LLC1 Common WebGUI Programming Mistakes."— Presentation transcript:

1 perlDreamer Consulting, LLC1 Common WebGUI Programming Mistakes

2 perlDreamer Consulting, LLC2 Common WebGUI Programming Mistakes

3 perlDreamer Consulting, LLC3 Common WebGUI Programming Mistakes and Things that Are Bad

4 perlDreamer Consulting, LLC4 Common WebGUI Programming Mistakes and Things that Are Bad and Stuff I Don't Like

5 perlDreamer Consulting, LLC5 What's up? ● Some of these aren't "mistakes" – They're silly. – Dangerous. – Needless. – Just my opinion ● Short, on-topic discussions of opinion are encouraged. ● Keep an eye on the clock.

6 perlDreamer Consulting, LLC6 Format for the talk ● I'm going to throw up code and coding practices. – For the most part, they're anonymous. ● If you think you know the mistake, please raise your hand. ● Bonus points for not using a reference. ● PLEASE DO NOT BLURT OUT ANSWERS.

7 perlDreamer Consulting, LLC7 What's up? ● If seeing your code up here is going to – Get your goat. – Make you mad. – Make you embarrassed. ● You might want to leave now...

8 perlDreamer Consulting, LLC8

9 9

10 10

11 perlDreamer Consulting, LLC11 ● Until 7.5.11, virtually all class methods in WebGUI required a session variable. Missing session variable

12 perlDreamer Consulting, LLC12 ● WebGUI::Shop::Address takes a WebGUI::Shop::AddressBook. ● WebGUI::Shop::CartItem takes a WebGUI::Shop::Cart. ● WebGUI::Shop::TransactionItem takes a WebGUI::Shop::Transaction. Constructors that don't take a Session object

13 perlDreamer Consulting, LLC13 ● General pattern is WebGUI::Object takes a WebGUI::Container. ● When the Object wants to access a session object, it does it through it's parent, the Container. Constructors that don't take a Session object

14 perlDreamer Consulting, LLC14 ● Several recently made methods check for a session variable or container object passed to constructors. – Constructors in WebGUI::Shop – WebGUI::Asset->newPending ● They either return undef, croak, or throw an exception. Missing session variable

15 perlDreamer Consulting, LLC15

16 perlDreamer Consulting, LLC16 ● i18n really isn't an object, it's a gateway. ● Call it as a subroutine and give it something useful as a first argument... – Like a session variable ● WebGUI::Session does not have a session method. – $self->session->log->warn won't work Abusing object methods

17 perlDreamer Consulting, LLC17

18 perlDreamer Consulting, LLC18 Failing to check a constructor ● Most constructors return undef when they fail, instead of throwing an exception. ● Be sure to check that you got returned an object before using it. – "Can't call method purge on an undefined value" ● New constructors, newPending, will throw an exception.

19 perlDreamer Consulting, LLC19 Check constructor

20 perlDreamer Consulting, LLC20

21 perlDreamer Consulting, LLC21 Long running objects ● What happened to the Asset's version control status? ● $versionTag->commit creates a new copy of the Asset and updates the status in the database. ● But $asset still thinks it is 'pending'. ● Calling $asset->update updates the database with its own status, not the one from the database.

22 perlDreamer Consulting, LLC22 How to avoid this

23 perlDreamer Consulting, LLC23 How to avoid this #2

24 perlDreamer Consulting, LLC24 ● WebGUI::Asset->new($session); ● WebGUI::Group->new($session); ● WebGUI::User->new($session); ● WebGUI::Storage->new($session);

25 perlDreamer Consulting, LLC25 Constructors vs Instanciators ● WebGUI::Asset->new($session); – Find an existing asset ● WebGUI::Group->new($session); – Build a new group from scratch. ● WebGUI::User->new($session, $userId); – Build a new user, only if $userId eq 'new' ● WebGUI::Storage does not have a new method. – use create for new objects – use find for existing objects

26 perlDreamer Consulting, LLC26 Constructors vs Instanciators ● Most recently added code uses – create for accessing existing objects. – new for contructing new objects from scratch. ● The worst offender is Asset.pm with – new, newPending – newByUrl, newByPropertyHashRef – addChild, newByDynamicClass

27 perlDreamer Consulting, LLC27 Goal: Get the name of a user by ID.

28 perlDreamer Consulting, LLC28 Learn the WebGUI API ● There's nothing technically wrong with this code, but it's still a mistake. ● There's no need to open a session object to get a user object. – Create a user object instead.

29 perlDreamer Consulting, LLC29 Goal: Get the name of a user by ID.

30 perlDreamer Consulting, LLC30 The Real Lesson ● The person who wrote this code asked how to get the name of a user from the session variable. ● But that wasn't what they wanted to know. ● When asking questions, give context. ● "This is what I want to do, this is what I have, here's how I'm trying to do it..."

31 perlDreamer Consulting, LLC31

32 perlDreamer Consulting, LLC32 Learn Perl ● That snippet of perl code reimplements perl's exists function, but much more slowly. ● Invest time in: – Keeping abreast of changes in Perl. – New modules on CPAN. – Learning what's new in MySQL –...and JavaScript, and YUI. – What's new in CMSes and other web frameworks.

33 perlDreamer Consulting, LLC33

34 perlDreamer Consulting, LLC34 Beware IRCers bearing gifts ● The person who wrote this code got "help" from someone in IRC. – /me ● They didn't know the "shorthand" of dropping WebGUI:: ● The common mistake: Laziness. – The old dev saved 5 seconds. – The new dev didn't double check.

35 perlDreamer Consulting, LLC35 IRC tips ● Search the forums and bug list before asking questions on IRC. ● Grep through the tests and WebGUI code for examples. ● Never blindly paste code from IRC without double checking it first. ● Don't ask to ask, just ask it. – Then be patient.

36 perlDreamer Consulting, LLC36

37 perlDreamer Consulting, LLC37 Asset vs VersionTag Commit ● Committing an asset will change its status and state. ● But it won't do anything for the version tag that was automatically created for you. ● That tag will just be sitting around – and confusing the users.

38 perlDreamer Consulting, LLC38 Make a CS with RSS

39 perlDreamer Consulting, LLC39 Make a CS with RSS #2

40 perlDreamer Consulting, LLC40 How do you make an CS with RSS? ● RSS in WebGUI is an Asset. ● It requires multiple inheritance, which is handled by the NEXT module. ● It only works when Assets are added through the GUI. ● Programmatic calls to new*, update, addChild, etc. are completely ignored. ● It is total crap.

41 perlDreamer Consulting, LLC41 Make a CS with RSS, really

42 perlDreamer Consulting, LLC42

43 perlDreamer Consulting, LLC43 OO Inheritance ● We have a base class and a child class. ● But the overridden method has different parameters from the parent. – I guess that's one way to make sure it gets overridden.

44 perlDreamer Consulting, LLC44 Template Variables

45 perlDreamer Consulting, LLC45 HTML::Template

46 perlDreamer Consulting, LLC46 Outputs

47 perlDreamer Consulting, LLC47 HTML::Template and undef ● If a variable in a loop is undef... ● And outside the loop a variable with the same name exists ● Then the undef is replaced with the value of the variable from outside the loop. ● '' is okay, this only happens with undef ● Doug wrote a fix for this bug six months ago. – Still waiting for it to be applied.

48 perlDreamer Consulting, LLC48 Buried inside WebGUI::Asset::Wobject::Calendar

49 perlDreamer Consulting, LLC49 viewWeek reorders events? ● What's up with that? ● Why not write www_reorderEvents and then call viewWeek at the end of that? ● Use encapsulation and separation of interests.

50 perlDreamer Consulting, LLC50 Want to “enhance” the Product ● If you modify WebGUI/Asset/Sku/Product.pm, the customer will need to do manual upgrades forever. ● What else can you do?

51 perlDreamer Consulting, LLC51 Copy it all. ● You build WebGUI/Asset/Sku/MyProduct.pm ● Benefits: – Safer upgrades. ● Penalties: – Manual merge for features and bugfixes in any code.

52 perlDreamer Consulting, LLC52 Subclass Product.pm ● Build WebGUI/Asset/Sku/Product/My.pm – Override and add only the code you need. ● Benefits: – Safe upgrades. – Automatic bugfixes in non-overridden code. ● Penalties: – Manual merge for features and bugfixes in any overridden code.

53 perlDreamer Consulting, LLC53

54 perlDreamer Consulting, LLC54 Non-existant JSON Array ● If there are no records, the “records” entry in the hash is never made into an array. ● YUI says this:

55 perlDreamer Consulting, LLC55

56 perlDreamer Consulting, LLC56 Happy YUI ● Pagination labels. ● Nice message about having no data.

57 perlDreamer Consulting, LLC57

58 perlDreamer Consulting, LLC58

59 perlDreamer Consulting, LLC59 Returning unsafe references ● Notice there are two places where references are returned. ● Each reference will allow the user to alter data inside of the object. – Stuff – Stow cache inside the session variable.

60 perlDreamer Consulting, LLC60 Returning unsafe references ● When the bug is tripped, it will be by a user of this code, and not the code itself. ● This makes the code VERY FRUSTRATINGLY HARD TO FIND!!! ● WebGUI is still full of these kinds of bugs. ● This last one is modeled off of the getGroups method in Groups – which has 100% test coverage

61 perlDreamer Consulting, LLC61

62 perlDreamer Consulting, LLC62

63 perlDreamer Consulting, LLC63

64 perlDreamer Consulting, LLC64

65 perlDreamer Consulting, LLC65

66 perlDreamer Consulting, LLC66 The biggest mistake ● Used emacs.

67 perlDreamer Consulting, LLC67 Emacs vs vim vs butterflies

68 perlDreamer Consulting, LLC68 SVN mistakes ● Did not use a commit message. – How will others (or you) figure out what this commit was for? ● Always use a brief, descriptive commit message. – "Time tracker changes" – "clean up test warnings" – "fix a potential future syntax error"

69 perlDreamer Consulting, LLC69 SVN mistakes ● Committed everything, not just the changes to Asset.pm. – Related to a bad commit message. – The ideal SVN workflow has small, atomic commits.

70 perlDreamer Consulting, LLC70 Procedural Mistakes ● Didn't run a syntax check. ● Didn't start (or restart) Apache. – So they didn't check their code in a browser. ● Didn't rerun the test suite. ● Didn't add new tests to check their code. – Not always possible, but more than you might think...


Download ppt "PerlDreamer Consulting, LLC1 Common WebGUI Programming Mistakes."

Similar presentations


Ads by Google