Sunday, March 6, 2011

Separation of Presentation and Business Tiers with Spring

Hi there,

In my just-completed project, I was working getting distributed transactions working.

We implemented this using JBoss's Arjuna Transaction Manager, and Spring's declarative transaction boundaries.

Our request sequence looked like:

browser -> secured servlet -> 'wafer-thin' SLSB -> spring TX-aware proxy -> request-handler POJO

What this meant is that we had a WAR to serve our secured servlet and an EAR to serve our SLSB.

Our SLSB had a static initialiser block to bootstrap our Spring application context.

I don't like the mix of technologies, but I do like the separation of presentation and business tiers, which could reside on different physical locations.

I would be interested to know what others propose to separate tiers when using Spring?

From stackoverflow
  • Requiring an EJB3 app server just for a SLSB that is a facade doesn't seem like it's worth the effort to me. There is no reason you couldn't just remove that and have your servlet work directly with Spring. You can add the ContextLoaderListener to the WAR to load your ApplicationContext and then WebApplicationContextUtils to get at it. Alternatively you could use SpringMVC, Struts or other web technologies if you need to do more than what the Servlet on its own will allow for.

  • A pretty typical approach is to define a web tier, a service tier and a DAO tier, and attach transactional semantics to the service tier. The service tier might be a bunch of POJOs with @Transactional annotations, for example. The web tier might be Spring Web MVC controllers. In this approach, the web tier is essentially adapting the service tier to HTTP. Good separation and no need for SLSBs here.

    One area of debate though is with respect to the domain objects, like Employee or PurchaseOrder or whatever. These span application tiers and one thing that seems to be happening with annotations is that the domain objects get annotations that are tied to specific tiers. So you might have ORM annotations here but then use the same domain object as a form-backing bean as a way to avoid parallel domain/form object classes. Some people object to that as violating architectural separation of concerns.

    toolkit : thanks Willie, unfortunately I cannot mark you both correct... +1

How do I concisely implement multiple similar unit tests in the Python unittest framework?

I'm implementing unit tests for a family of functions that all share a number of invariants. For example, calling the function with two matrices produce a matrix of known shape.

I would like to write unit tests to test the entire family of functions for this property, without having to write an individual test case for each function (particularly since more functions might be added later).

One way to do this would be to iterate over a list of these functions:

import unittest
import numpy

from somewhere import the_functions
from somewhere.else import TheClass

class Test_the_functions(unittest.TestCase):
  def setUp(self):
    self.matrix1 = numpy.ones((5,10))
    self.matrix2 = numpy.identity(5)

  def testOutputShape(unittest.TestCase):
     """Output of functions be of a certain shape"""
     for function in all_functions:
       output = function(self.matrix1, self.matrix2)
       fail_message = "%s produces output of the wrong shape" % str(function)
       self.assertEqual(self.matrix1.shape, output.shape, fail_message)

if __name__ == "__main__":
  unittest.main()

I got the idea for this from Dive Into Python. There, it's not a list of functions being tested but a list of known input-output pairs. The problem with this approach is that if any element of the list fails the test, the later elements don't get tested.

I looked at subclassing unittest.TestCase and somehow providing the specific function to test as an argument, but as far as I can tell that prevents us from using unittest.main() because there would be no way to pass the argument to the testcase.

I also looked at dynamically attaching "testSomething" functions to the testcase, by using setattr with a lamdba, but the testcase did not recognize them.

How can I rewrite this so it remains trivial to expand the list of tests, while still ensuring every test is run?

From stackoverflow
  • You could use a metaclass to dynamically insert the tests. This works fine for me:

    import unittest
    
    class UnderTest(object):
    
        def f1(self, i):
            return i + 1
    
        def f2(self, i):
            return i + 2
    
    class TestMeta(type):
    
        def __new__(cls, name, bases, attrs):
            funcs = [t for t in dir(UnderTest) if t[0] == 'f']
    
            def doTest(t):
                def f(slf):
                    ut=UnderTest()
                    getattr(ut, t)(3)
                return f
    
            for f in funcs:
                attrs['test_gen_' + f] = doTest(f)
            return type.__new__(cls, name, bases, attrs)
    
    class T(unittest.TestCase):
    
        __metaclass__ = TestMeta
    
        def testOne(self):
            self.assertTrue(True)
    
    if __name__ == '__main__':
        unittest.main()
    
    saffsd : Thanks, this works. Only one slight quirk, nose is unable to see the testcases added by the metaclass. Any suggestions?
    Dustin : I'm not familiar with nose. This adds the methods to the class, so I'm not sure what nose could be doing to miss them. I'd be interesting to find out what its magic is, though.
    Matt Joiner : cutting it bit fine with the use of `f` in the __new__ there, it's a bit obscure
  • Metaclasses is one option. Another option is to use a TestSuite:

    import unittest
    import numpy
    import funcs
    
    # get references to functions
    # only the functions and if their names start with "matrixOp"
    functions_to_test = [v for k,v in funcs.__dict__ if v.func_name.startswith('matrixOp')]
    
    # suplly an optional setup function
    def setUp(self):
        self.matrix1 = numpy.ones((5,10))
        self.matrix2 = numpy.identity(5)
    
    # create tests from functions directly and store those TestCases in a TestSuite
    test_suite = unittest.TestSuite([unittest.FunctionTestCase(f, setUp=setUp) for f in functions_to_test])
    
    
    if __name__ == "__main__":
    unittest.main()
    

    Haven't tested. But it should work fine.

    saffsd : unittest.main() doesn't automatically pick this up, and neither does nose. Also, FunctionTestCase calls setUp without arguments, and the functions_to_test need to be wrapped in something that asserts the test.
  • Here's my favorite approach to the "family of related tests". I like explicit subclasses of a TestCase that expresses the common features.

    class MyTestF1( unittest.TestCase ):
        theFunction= staticmethod( f1 )
        def setUp(self):
            self.matrix1 = numpy.ones((5,10))
            self.matrix2 = numpy.identity(5)
        def testOutputShape( self ):
            """Output of functions be of a certain shape"""
            output = self.theFunction(self.matrix1, self.matrix2)
            fail_message = "%s produces output of the wrong shape" % (self.theFunction.__name__,)
            self.assertEqual(self.matrix1.shape, output.shape, fail_message)
    
    class TestF2( MyTestF1 ):
        """Includes ALL of TestF1 tests, plus a new test."""
        theFunction= staticmethod( f2 )
        def testUniqueFeature( self ):
             # blah blah blah
             pass
    
    class TestF3( MyTestF1 ):
        """Includes ALL of TestF1 tests with no additional code."""
        theFunction= staticmethod( f3 )
    

    Add a function, add a subclass of MyTestF1. Each subclass of MyTestF1 includes all of the tests in MyTestF1 with no duplicated code of any kind.

    Unique features are handled in an obvious way. New methods are added to the subclass.

    It's completely compatible with unittest.main()

    muhuk : I like this object-oriented solution. "Explicit is better than implicit"
    saffsd : I don't like this because it introduces a whole heap of duplicated code. Since each of the functions is meant to observe the same invariant being tested, I'd like a way to express exactly this without having to lump them into a single testcase. Perhaps I should? Thanks for the suggestion though.
    S.Lott : Refactor common code up into a superclass. That's what superclasses are for. Your "common test" is precisely why we have superclasses and subclasses.
    saffsd : The issue is that it doesn't refactor. I'm working with classification algorithms, and I'm modelling each as a function with two input matrices and one output matrix. Most of these functions are not even python, they're thin wrappers. Perhaps I should assert inside rather than test outside?
    S.Lott : @saffsd: "it doesn't refactor"? What is "it? I'm talking about refactoring the tests into a single common superclass so each function has a subclass that assures that common features of the function have common methods in a test.
  • The problem with this approach is that if any element of the list fails the test, the later elements don't get tested.

    If you look at it from the point of view that, if a test fails, that is critical and your entire package is invalid, then it doesn't matter that other elements won't get tested, because 'hey, you have an error to fix'.

    Once that test passes, the other tests will then run.

    Admittedly there is information to be gained from knowledge of which other tests are failing, and that can help with debugging, but apart from that, assume any test failure is an entire application failure.

    saffsd : I recognize that, but another counter-argument is that if you are running tests in a batch, say overnight, you want to know where all of the failures are, not just the first one.
  • If you're already using nose (and some of your comments suggest you are), why don't you just use Test Generators, which are the most straightforward way to implement parametric tests I've come across:

    For example:

    from binary_search import search1 as search
    
    def test_binary_search():
        data = (
            (-1, 3, []),
         (-1, 3, [1]),
         (0,  1, [1]),
         (0,  1, [1, 3, 5]),
         (1,  3, [1, 3, 5]),
         (2,  5, [1, 3, 5]),
         (-1, 0, [1, 3, 5]),
         (-1, 2, [1, 3, 5]),
         (-1, 4, [1, 3, 5]),
         (-1, 6, [1, 3, 5]),
         (0,  1, [1, 3, 5, 7]),
         (1,  3, [1, 3, 5, 7]),
         (2,  5, [1, 3, 5, 7]),
         (3,  7, [1, 3, 5, 7]),
         (-1, 0, [1, 3, 5, 7]),
         (-1, 2, [1, 3, 5, 7]),
         (-1, 4, [1, 3, 5, 7]),
         (-1, 6, [1, 3, 5, 7]),
         (-1, 8, [1, 3, 5, 7]),
        )
    
        for result, n, ns in data:
         yield check_binary_search, result, n, ns
    
    def check_binary_search(expected, n, ns):
        actual = search(n, ns)
        assert expected == actual
    

    Produces:

    $ nosetests -d
    ...................
    ----------------------------------------------------------------------
    Ran 19 tests in 0.009s
    
    OK
    
  • The above metaclass code has trouble with nose because nose's wantMethod in its selector.py is looking at a given test method's __name__, not the attribute dict key.

    To use a metaclass defined test method with nose, the method name and dictionary key must be the same, and prefixed to be detected by nose (ie with 'test_').

    # test class that uses a metaclass
    class TCType(type):
        def __new__(cls, name, bases, dct):
            def generate_test_method():
                def test_method(self):
                    pass
                return test_method
    
            dct['test_method'] = generate_test_method()
            return type.__new__(cls, name, bases, dct)
    
    class TestMetaclassed(object):
        __metaclass__ = TCType
    
        def test_one(self):
            pass
        def test_two(self):
            pass
    
  • You don't have to use Meta Classes here. A simple loop fits just fine. Take a look at the example below:

    import unittest
    class TestCase1(unittest.TestCase):
        def check_something(self, param1):
            self.assertTrue(param1)
    
    def _add_test(name, param1):
        def test_method(self):
            self.check_something(param1)
        setattr(TestCase1, 'test_'+name, test_method)
        test_method.__name__ = 'test_'+name
    
    for i in range(0, 3):
        _add_test(str(i), False)
    

    Once the for is executed the TestCase1 has 3 test methods that are supported by both the nose and the unittest.

    Matt Joiner : yeah i find metaclasses for purposes of "instrumenting" single-use classes never flies well, this is a much better approach.

When would you use the mediator design pattern

As the title states when would you recommend the use of the mediator design pattern and where do you see it used incorrectly?

From stackoverflow
  • I have used it to deal with swing apps.

    When I'm building a GUI I don't like each control know each other because that would require subclassing.

    Instead I have a Main object whose contains the listener and the widgets and let it mediate between the different controls, buttons, textfields etc.

  • Use a mediator when the complexity of object communication begins to hinder object reusability. This type of complexity often appears in view instances, though it could really be anywhere.

    Misuse of a mediator can result in crippling the interfaces of the mediator's colleague classes.

    It seems a little funny to talk about misusing a pattern. If your implementation follows the pattern, then you've used the pattern. Otherwise, you haven't. In other words, if your mediator is doing something else, then it probably isn't a mediator. Patterns are defined by what they do, what they in fact are. The names of things are simply labels.

    The real question to ask yourself is whether your implementation of a pattern fulfills the pattern's promises for your design. The mediator pattern aims to encapsulate complex inter-object communication when it is becoming unmanageable. If it hasn't accomplished this, or hasn't done it very well, you could say that a mediator is being misused. At some point, it becomes a value judgement.

    OscarRyz : 1+ for the answer
    hhafez : could you give more details on the misuse? Thanks
    keparo : (added a note above)
  • The mediator is also basically what an event-pump is. A very common pattern in GUI's and Games.

    Also I've used mediator before to communicate among very dispar systems, and legacy frameworks.

Oracle checking existence before deletion in a trigger

I have analyzed a hibernate generated oracle database and discovered that a delete of a row from a single table will spawn the firing of 1200+ triggers in order to delete the related rows in child tables. The triggers are all auto-generated the same - an automatic delete of a child row without checking for existence first. As it is impossible to predict which child tables will actually have related rows, I think a viable solution to preventing the firing of the cascaded delete down a deeply branched completely empty limb, would be to check for the existence of a related row before attempting to delete. In other dbms', I could simply state " if exists....." before deleting. Is there a comparable way to do this in oracle?

From stackoverflow
  • You can query the special dba_objects table: 

    DECLARE 
    X    NUMBER;
    BEGIN
        SELECT COUNT(*) INTO X FROM DBA_OBJECTS WHERE OBJECT_TYPE = 'TRIGGER' AND OBJECT_NAME = 'YOUR_TRIGGER_NAME_HERE';
        IF X = 0 THEN
            --Trigger doesn't exist, OK to delete...
        END IF;
    END;
    
  • If possible, modify and setup your DB tables appropriately. - Involve a DBA if you have one at your disposal.

    You need to use Foreign Key constraints and cascade deletes. This eliminates the need for triggers, etc...

  • "delete of a row from a single table will spawn the firing of 1200+ triggers" Are these statement or row level triggers ? If the latter, they'll only fire if a row is deleted. Say you have a BEFORE DELETE trigger on customers to delete the customers orders, and a BEFORE DELETE trigger on orders to delete order items. If the customer has no orders, and the orders table trigger is a row level trigger, then it will not fire the delete from order items.

    "check for the existence of a related row before attempting to delete" Probably no benefit. In fact it would do more work having a SELECT followed by a DELETE.

    Of course the Hibernate logic is broken. The deleting session will only see (and try to delete) committed transactions. If FRED has inserted an order for the customer, but it is not committed, JOHN's delete (through the trigger) won't see it or try to delete it. It will however still 'succeed' and try to delete the parent customer. If you have actually got your foreign key constraints enabled in the database, Oracle will kick in. It will wait until FRED commits, then reject the delete as it has a child. If the foreign key constraints aren't in place, you have an order for a non-existent customer. This is why you should have this sort of business logic enforced in the database.

  • **select * from Tab where Tname = "TABLENAME"

    <

    If this query returns any row then table exist else Not**

threadlocal variables in a servlet

Are the threadlocals variables global to all the requests made to the servlet that owns the variables?

I am using resin for the server.

Thanks for awnser.

I think I can make my self more clear.

The specific Case:

I want to:

  • initialize a static variable when the request starts the execution.
  • be able to query the value of the variable in the further executions of methods called from the servlet in a thread safety way until the request ends the execution
From stackoverflow
  • I think they are global to all requests made with that specific thread only. Other threads get other copies of the thread-local data. This is the key point of thread-local storage: http://en.wikipedia.org/wiki/Thread-local_storage#Java.

    Unless you check the appropriate option in the servlets config, the servlet container will use your servlet with multiple threads to handle requests in parallel. So effectively you would have separate data for each thread that's up serving clients.

    If your WebApplication isn't distributed (runs on multiple Java Virtual Machines), you can use the ServletContext object to store shared data across requests and threads (be sure to do proper locking then).

    Julie : Indeed. On a previous project, I stored a user's ticket in their session, and created a filter to transfer the ticket from the session to a thread local to make sure that a user's authentication state was always available to the thread processing the request.
  • Threadlocal variables are always defined to be accessed globally, since the point is to transparently pass information around a system that can be accessed anywhere. The value of the variable is bound to the thread on which it is set, so even though the variable is global, it can have different values depending on the thread from which it is accessed.

    A simple example would be to assign a user identity string to a thread in a thread local variable when the request is received in the servlet. Anywhere along the processing chain of that request (assuming it is on the same thread in the same VM), the identity can be retrieved by accessing this global variable. It would also be important to remove this value when the request is processed, since the thread will be put back in a thread pool.

  • Like Adiel says, the proper way to do this is probably to use the request context (i.e. HttpServletRequest), not to create a ThreadLocal. While it's certainly possible to use a ThreadLocal here, you have to be careful to clean up your thread if you do that, since otherwise the next request that gets the thread will see the value associated with the previous request. (When the first request is done with the thread, the thread will go back into the pool and so the next request will see it.) No reason to have to manage that kind of thing when the request context exists for precisely this purpose.

    sehugg : You could also use a servlet filter to manage the ThreadLocal, at least the creation/cleanup would be in one place.
  • Short answer: Yes.
    A bit longer one: This is how Spring does its magic. See RequestContextHolder (via DocJar).

    Caution is needed though - you have to know when to invalidate the ThreadLocal, how to defer to other threads and how (not) to get tangled with a non-threadlocal context.

    Or you could just use Spring...

How do you specify a where clause on data returned from a .Include in an Entity Framework query?

Given the following database table hierarchy:

Region
------
RegionId
RegionName

Country
-------
CountryId
RegionId
CountryName

Destination
-----------
DestinationId
CountryId
DestinationName

Venue
-----
VenueId
DestinationId
VenueName

I have the following Entity Framework query:

var result = from region in context.Region.Include("Country.Destination.Venue") 
select region

which will return all rows from all tables (outer join)

Is it possible to introduce a where clause so that only rows where the venue is not null are included (or use an inner join) ?

Thanks

From stackoverflow
  • Try this. It should return the results you are looking for: only regions that have a corresponding venue.

        var result = from region in context.Region.Include(Country.Destination.Venue)
                     let v = (from ctry in region.Country
                             join dest in context.Destination
                             on ctry.CountryId
                             equals dest.CountryId
                             into destGroup
                       from dests in destGroup
                       join ven in context.Venue
                       on dests.DestinationId
                       equals ven.DestinationId
                       into venGroup
                             select ctry).Any()
                     where v == true
                     select region;
    
    Alan Heywood : Thanks, I tried this however it brings back all regions in the case that there is an associated Country and Destination but no associated Venue.
    YeahStu : Ok. You might be right. I tested this on a sample app here but I only had three tables to play with, not four. I hope this shows an example of what you might try though.

How to Check for Image in Cache Using Silverlight

I am creating a basic image browsing application using Silverlight. Depending on the user's connection speed, some images may take time to appear once they click on the thumb nail. I would like to show a progress bar while it is being downloaded. I have this part done.

However, if the image is already in the cache (viewing a previous image), I'd rather not have the progress bar flash up and then disappear. Is there a way to see if a particular file is in the cache before I display my progress bar?

From stackoverflow
  • After thinking about it for a while, I did come up with one solution, though it wasn't what I was originally intending.

    I am using the WebClient class to get my image file. I attach to the DownloadProgressChanged event. If the image has already been downloaded, then the ProgressPercentage is immediately 100.

    So instead of making the ProgressBar visibile when I call OpenReadAsync and making it invisible when the Completed event is fired, I set the visibility in the DownloadProgressChanged event handler.

how hard is it to stop programming after you get off work?

I just started a new project, and have found myself having to force myself into not researching/coding after I leave work, in order to spend time with my wife and kid. Is this common among the SO community?

From stackoverflow
  • I would say my short answer is, "It depends." If I have some problem or issue that I'm trying to resolve and not getting anywhere then I may find myself wondering about alternative approaches to solve the problem. On the other hand, there are times when this doesn't happen because I have to remember to tend to other things like eating and sleeping, which are rather basic needs.

    NTDLS : Sleeping? You can sleep with these problems rattling arround in your head?
    JB King : Yes, oddly enough there are times where I lose conscience that is where my body overrules my mind.
  • Don't worry, you'll come to understand the phrase "Get off that damn computer and come to bed" as a term of endearment... at least, I did :oP

    paxdiablo : :oP - is that someone with squinty eyes, a big bulbous nose and their tongue hanging out of their mouth?
    BenAlabaster : That sounds remarkably like a description of me, hehe ;)
  • I don't usually code at home, but I sometimes lose track of time and don't leave when I should. usually, that ends up with my wife calling to ask when I'll be home. :-)

    One of the reasons that I don't code at home is my work machine has me spoiled -- 3 monitors, 2 cores, lots of memory. Single monitor at home makes it enough of a pain that I'm usually not tempted. Same goes for the laptop -- unless I'm working on a personal project in RoR.

    daub815 : So I guess it would be bad when you get a similar setup at home?
    Unkwntech : Is it even worse that I prefer to code on my system at home because its faster/has more monitors/ and more software then my system at the office?
  • Maybe quite a few of us here are addicted to programming. It's probably one of the main reasons we choose this profession. Personally I try to schedule my time so I don't neglect the important things in life. I spend enough time glued to my computer as it is.

  • I usually find myself going home and working on some other programming area such as another language. I usually prefer to go home and do C/C++ or desktop style application development if I've been doing web development all day, and vice versa. Also I will use the time to read a book about theory or design rather then coding again and staring at a screen more.

    paxdiablo : Married? Have kids?
  • Working on project you are passionate about is great. It makes work seem like play and you are devoting more than required since you enjoy it. It is important to keep things balanced though, as to not burn out.

    Forcing yourself to disconnect from work and concentrate on other things in your life would be best in the long run. I am currently working on such a project (as I'm sure many other SO members are), and have gone through several burnouts before I figured out how to balance it properly.

  • Not hard at all!

    No one on their death bed wishes they spent more time away from their family.

    Get away from the PC, do something else, something physical.

  • It's not hard at all, my pay compensates me for the time I have to spend away from the family. If I were rich, I wouldn't be working - I'd still spend some time doing my own "work" stuff but it would be fairly minimal, just enough to give a sense of usefulness.

    This isn't directed at you of course but, if you find yourself spending more time working than you need to, why did you bother starting a family? You could have just donated your money to some poor kids in Africa if you wanted a feel-good feeling (or keep it for yourself if you don't really care).

    Your family is meant to be enjoyed, work is just an enabler for that. I have a 4yo boy and a 2yo girl and I actively try to get as much time off work as possible to spend time with them (and my wife). By the time they're teenagers, they probably won't want to know me so I'm getting in as much quality time as I can.

    Robert Gould : +1 for family values :)
    NTDLS : +1 for family values but the answer makes it sound as though you do not enjoy what you do: "...Your family is meant to be enjoyed, work is just an enabler for that..." - I sir, enjoy both!
  • In totally reductionist terms, human beings are wired to do what feels good. If your individual wiring makes you especially prone to feeling good when you're problem solving, some would say that you're probably more likely to have trouble stepping back. At the end of the day, it all comes down to dopamine and what you're doing/thinking when you get a blast of it.

    When I've had trouble stepping away from the keyboard, I've always tried to distract myself with something else positive, rather than dwelling on the the fact that I'm incapable of stepping away from the keyboard. Influx of good feelings reinforces the behavior.

    My apologies to neuroscientists everywhere.

    NTDLS : Damn, where is the super-upvote button?
  • Don't know, never tried.

  • luckily not for my case, i know it's fun to just go for one more bug, one more paper one more line of codes, but there needs to be boundary else your personal life is going to hit the crapper pretty soon.

    usually i try to outline the stuff i got to do else i might get fried by my boss activities first and i try to plan out the type of activities and how much time it might take so i don't overdo it.

    because i think it's healthy that you have predefined boundary and don't overburn so when you come back tomorrow morning you're still up and perky for more challenges. as i've find the hard way when you're not at 100% you spend more time and effort trying to do the simplest thing and even making dumb mistakes that you spend more time to fix..

    GTD anyone? hehe just my humble 2cents.

  • Keep the passion going, don't burn out !

    its all about moderation. I try to limit the number of hours per night after the office to my own projects but don't neglect your family or you will regret it in the future.

  • Am I the only one that codes after work. I do have to say that I choose what I work on. I hardly ever "code" work related stuff at home. I will however play with the chip or technology so I can get familiar with it. This way I will be ready to be productive when I get back to work. I do fun stuff at home though. Like right now I am using an 8051 to create a PWM output. This gets me familiar with the chip but really don't need a PWM for work. When I'm not coding I like programming related podcast and books.

    Of course there are times when I do however break away and spend time with my lovely wife or WoW (World of Warcraft)!

  • It's very hard for me not to program at home. I don't get to program enough at work, and on days when it's not pleasant to go play outside, programming is the most fun I can have with my clothes on!

  • Yes, I know what you're saying. Especially, back when I was a newbie it was pretty hard to pull away. Now that I've been at it a few years I've become a bit more balanced. Tomorrow is another day and things can wait. At least for me having a more (or less) balanced life actually makes coding more fun (and productive) now than ever.

    That said, balance is always a struggle. From time to time I'll spent half an hour fighting with myself to break free of the keyboard and go home. But when I get home and my kids (6 and 3) come running up screaming "daddy's home" I'm always glad I left the work behind.

    Best of luck to you!

  • Remember that you just started a project - it's easier to be interested in new problems while they're still new. That should settle down after the fun problems have been solved.

  • Especially at the start of a project, I find it very hard to leave my work at the office. I have learned that most of the time, work will wait until tomorrow. Unless you have pressing deadlines then I guess you will have to work at home as well as at the office. And some time I just research things at home so that I am that much ahead for the next day.

    Try to have a balance of home life and work life. In my opinion it is OK to work hard for a few day's or a week. But any more than that and you can get burned out, And the wife doesn't like it either.

  • I have a hard time letting go. It's a matter of being in the zone. When you're in the zone, you are infinitely more productive so you want to keep going and get as much done as you can. In an ideal world I would work for a few days non stop and then take a few days off.

  • Depends on the project. I have a side project I'm working on that I just can't seem to get motivated enough to work on. I should have had it near done by now.

    If it's something I'm excited about, I'll sometimes forget to leave work until about an hour or two after I should have.

Do you create your own code generators?

The Pragmatic Programmer advocates the use of code generators. Do you create code generators on your projects? If yes, what do you use them for?

From stackoverflow
  • Code generators if used widely without correct argumentation make code less understandable and decrease maintainability (the same with dynamic SQL by the way). Personally I'm using it with some of ORM tools, because their usage here mostly obvious and sometimes for things like searcher-parser algorithms and grammatic analyzers which are not designed to be maintained "by hands" lately. Cheers.

  • We use code generators for generating data entity classes, database objects (like triggers, stored procs), service proxies etc. Anywhere you see lot of repititive code following a pattern and lot of manual work involved, code generators can help. But, you should not use it too much to the extend that maintainability is a pain. Some issues also arise if you want to regenerate them.

    Tools like Visual Studio, Codesmith have their own templates for most of the common tasks and make this process easier. But, it is easy to roll out on your own.

  • In hardware design, it's fairly common practice to do this at several levels of the 'stack'. For instance, I wrote a code generator to emit Verilog for various widths, topologies, and structures of DMA engines and crossbar switches, because the constructs needed to express this parameterization weren't yet mature in the synthesis and simulation tool flows.

    It's also routine to emit logical models all the way down to layout data for very regular things that can be expressed and generated algorithmically, like SRAM, cache, and register file structures.

    I also spent a fair bit of time writing, essentially, a code generator that would take an XML description of all the registers on a System-on-Chip, and emit HTML (yes, yes, I know about XSLT, I just found emitting it programatically to be more time-effective), Verilog, SystemVerilog, C, Assembly etc. "views" of that data for different teams (front-end and back-end ASIC design, firmware, documentation, etc.) to use (and keep them consistent by virtue of this single XML "codebase"). Does that count?

    People also like to write code generators for e.g. taking terse descriptions of very common things, like finite state machines, and mechanically outputting more verbose imperative language code to implement them efficiently (e.g. transition tables and traversal code).

    Robert Harvey : You're not the first person who has said that it is easier to write code than it is to use XSLT.
  • there might be a lot of code generators out there , however I always create my own to make the code more understandable and suit the frameworks and guidelines we are using

  • Yes I've had to maintain a few. CORBA or some other object communication style of interface is probably the general thing that I think of first. You have object definitions that are provided to you by the interface you are going to talk over but you still have to build those objects up in code. Building and running a code generator is a fairly routine way of doing that. This can become a fairly lengthy compile just to support some legacy communication channel, and since there is a large tendency to put wrappers around CORBA to make it simpler, well things just get worse.

    In general if you have a large amount of structures, or just rapidly changing structures that you need to use, but you can't handle the performance hit of building objects through metadata, then your into writing a code generator.

  • We use a generator for all new code to help ensure that coding standards are followed.

    We recently replaced our in-house C++ generator with CodeSmith. We still have to create the templates for the tool, but it seems ideal to not have to maintain the tool ourselves.

  • My most recent need for a generator was a project that read data from hardware and ultimately posted it to a 'dashboard' UI. In-between were models, properties, presenters, events, interfaces, flags, etc. for several data points. I worked up the framework for a couple data points until I was satisfied that I could live with the design. Then, with the help of some carefully placed comments, I put the "generation" in a visual studio macro, tweaked and cleaned the macro, added the datapoints to a function in the macro to call the generation - and saved several tedious hours (days?) in the end.

    Don't underestimate the power of macros :)


    I am also now trying to get my head around CodeRush customization capabilities to help me with some more local generation requirements. There is powerful stuff in there if you need on-the-fly decision making when generating a code block.

    Chris : Yes, emacs and on-the-fly lisp code is great for pushing out repetive stuff.
    Mike Dunlavey : Agree. When I've got serious repetitive code-rewriting to do, nothing beats trusty old Epsilon macros.
  • in my opinion a good programming language would not need code generators because introspection and runtime code generation would be part of language e.g. in python metaclasses and new module etc.

  • I have my own code generator that I run against SQL tables. It generates the SQL procedures to access the data, the data access layer and the business logic. It has done wonders in standardising my code and naming conventions. Because it expects certain fields in the database tables (such as an id column and updated datetime column) it has also helped standardise my data design.

  • I can't think of any projects where we needed to create our own code generators from scratch but there are several where we used preexisting generators. (I have used both Antlr and the Eclipse Modeling Framework for building parsers and models in java for enterprise software.) The beauty of using a code generator that someone else has written is that the authors tend to be experts in that area and have solved problems that I didn't even know existed yet. This saves me time and frustration.

    So even though I might be able to write code that solves the problem at hand, I can generate the code a lot faster and there is a good chance that it will be less buggy than anything I write.

  • code generators usually generate more unmanageable code in long term usage.

    however, if it is absolutely imperative to use a code generator (eclipse VE for swing development is what I use at times) then make sure you know what code is being generated. Believe me, you wouldn't want code in your application that you are not familiar with.

  • It is often useful to create a code generator that generates code from a specification - usually one that has regular tabular rules. It reduces the chance of introducing an error via a typo or omission.

  • If you're not going to write the code, are you going to be comfortable with someone else's generated code?

    Is it cheaper in both time and $$$ in the long run to write your own code or code generator?

    I wrote a code generator that would build 100's of classes (java) that would output XML data from database in a DTD or schema compliant manner. The code generation was generally a one time thing and the code would then be smartened up with various business rules etc. The output was for a rather pedantic bank.

  • In "Pragmatic Programmer" Hunt and Thomas distinguish between Passive and Active code generators.

    Passive generators are run-once, after which you edit the result.

    Active generators are run as often as desired, and you should never edit the result because it will be replaced.

    IMO, the latter are much more valuable because they approach the DRY (don't-repeat-yourself) principle.

    If the input information to your program can be split into two parts, the part that changes seldom (A) (like metadata or a DSL), and the part that is different each time the program is run (B)(the live input), you can write a generator program that takes only A as input, and writes out an ad-hoc program that only takes B as input. (Another name for this is partial evaluation.)

    The generator program is simpler because it only has to wade through input A, not A and B. Also, it does not have to be fast because it is not run often, and it doesn't have to care about memory leaks.

    The ad-hoc program is faster because it's not having to wade through input that is almost always the same (A). It is simpler because it only has to make decisions about input B, not A and B.

    It's a good idea for the generated ad-hoc program to be quite readable, so you can more easily find any errors in it. Once you get the errors removed from the generator, they are gone forever.

    In one project I worked on, a team designed a complex database application with a design spec two inches thick and a lengthy implementation schedule, fraught with concerns about performance. By writing a code generator, two people did the job in three months, and the source code listings (in C) were about a half-inch thick, and the generated code was so fast as to not be an issue. The ad-hoc program was regenerated weekly, at trivial cost.

    So active code generation, when you can use it, is a win-win. And, I think it's no accident that this is exactly what compilers do.

  • Code generators are work-around for programming language limitations. I personally prefer reflection instead of code generators but I agree that code generators are more flexible and resulting code obviously faster during runtime. I hope, future versions of C# will include some kind of DSL environment.

  • How many are you looking for? I've created two major ones and numerous minor ones. The first of the major ones allowed me to generate programs 1500 line programs (give or take) that had a strong family resemblance but were attuned to the different tables in a database - and to do that fast, and reliably.

    The downside of a code generator is that if there's a bug in the code generated (because the template contains a bug), then there's a lot of fixing to do.

    However, for languages or systems where there is a lot of near-repetitious coding to be done, a good (enough) code generator is a boon (and more of a boon than a 'doggle').

  • The only code generators that I use are webservice parsers. I personally stay away from code generators because of the maintenance problems for new employees or a separate team after hand off.

  • I write my own code generators, mainly in T-SQL, which are called during the build process.

    Based on meta-model data, they generate triggers, logging, C# const declarations, INSERT/UPDATE statements, data model information to check whether the app is running on the expected database schema.

    I still need to write a forms generator for increased productivity, more specs and less coding ;)

  • I've created a few code generators. I had a passive code generator for SQL Stored procedures which used templates. This generated generated 90% of our stored procedures.

    Since we made the switch to Entity Framework I've created an active codegenerator using T4 (Text Template Transformation Toolkit) inside visual studio. I've used it to create basic repository partial classes for our entities. Works very nicely and saves a bunch of coding. I also use T4 for decorating the entity classes with certain Attributes.

  • Yes , I developed my own code generator for AAA protocol Diameter (RFC 3588). It could generate structures and Api's for diameter messages reading from an XML file that described diameter application's grammar.

    That greatly reduced the time to develop complete diameter interface (such as SH/CX/RO etc.).

  • I use code generation features provided by EMF - Eclipse Modeling Framework.

  • Code generators are really useful in many cases, especially when mapping from one format to another. I've done code generators for IDL to C++, database tables to OO types, and marshalling code just to name a few.

    I think the point the authors are trying to make is that if you're a developer you should be able to make the computer work for you. Generating code is just one obvious task to automate.

    I once worked with a guy who insisted that he would do our IDL to C++ mapping manually. In the beginning of the project he was able to keep up, because the rest of us were trying to figure out what to do, but eventually he became a bottleneck. I did a code generator in Perl and then we could pretty much do his "work" in a few minutes.

Do people still live by the 80 column rule?

I have recently been converted, at least on the principal that I can read and edit my code as I left it from any terminal in the world. But I still ask myself, when will I ever need to do that? I am finding in c++, especially modern c++, my lines are longer, with more namespace qualified references, etc.

So what do people think, 80 columns or is that standard long dead?


This question keeps coming up:

From stackoverflow
  • I've personally switched to a 120-column "rule" - for no reason other than the fact that all the monitors I find myself sitting in front of can support that many (if not more) characters. Everyone's widescreen these days, right? ;)

    Robert Gould : +1 I also upgraded to the 120 rule. Besides it generally makes code easier to read
  • I personally think it is dead. 80 columns doesn't make sense in the world of high resolution monitors. Just think of how many developers have dual (or more) monitor setups at their desks.

    This is not to say that there is no limit to column length. I just think the column length limit has become more subject to the particular scenario, allowing each developer to decide what "logically" should go on a line.

  • Depends on the language.

    For example, in something like Python, where lines tend to be shorter anyway, the convention is 80 cols.

    On the other hand, for Java or C#, 120 is more common, simply because the 80 character limit becomes a stumbling block more often.

    In my opinion, wider monitors do not make it "okay" to have very long lines. It's kind've wasting the screen real estate to have a really wide window is mostly white space on the righthand side, just to allow for the occasional long line. I prefer to be able to have more than one thing side by side.

  • I still need 80 columns. In fact, I cut off text in my .vimrc at 70; I set that years ago due to requirements in a newsgroup I posted in. Sorry to the rest of the world for not changing, but I still want a bunch of 80 column xterms on my screen!

    Even though a lot of the coding I do today is in Eclipse, it still looks awful if you don't wrap it somewhere around 80 columns. I suppose I might be able to tolerate a 120-column rule as some people have said, but there are still some situations for which vim is still the right tool, and I don't want to have to blow my xterms up all the time to accommodate the code I wrote in another tool.

    Draemon : Java looks awful if you restrict it to 80 columns. It's subjective I guess.
    skiphoppy : @Draemon: That's true. It doesn't help that people like to use 25-char+ identifiers.
    skiphoppy : I guess Java looks awful no matter where I set the break...
  • Nearly every organization I have worked for has stuck to the "80 column rule" in their coding standards. The reason given is usually for printing standards, code is usually printed out for code reviews and wraps on paper are terrible.

    So I'd say it is definitely not dead in the working world, where code reviews are king.

    Robert Gould : interesting insight. At my work we print in landscape actually
    SoapBox : I did work at one place where they printed in 2 column landscape, which is large than 80. I dont particularly like 80, but when its forced on you you get used to it :-)
    SoapBox : Funny, though, it was wider than 80 but they still had 80 in their coding standard "for printing". Figure that one out :-)
    Vadim Ferderer : Code printing is actually a bad habit.
  • I zealously wrap my code at 80 columns, and encourage my colleagues to do the same.

    1. When I need to refactor/decipher messy or complicated code, I sometimes prefer to sit on a couch with a hardcopy and a pen in my hand. Code wrapped at 80 columns perfectly fits a standard (raw text) printout you can get out of any printer.
    2. Having a lot of characters on a single line isn't necessarily a good thing. Newspapers and magazines use narrow columns of text in their layouts for a reason. The human eye/brain is the most comfortable when reading lines of text that are of moderate length. 80 characters is close to that moderate number.
    3. If you have a gigantic monitor with 2000 pixels across, why waste all that space with horizontally expanding code while you could display multiple windows that are horizontally tiled? Multitasking trumps a panoramic view of my code, however beautifully crafted it may be ;)
    4. I sometimes copy/paste existing code to my blog (or stackoverflow.com). Websites typically don't accommodate long lines of code well.
    5. Code written with Spartan principles (that I live by) won't typically have a lot of lines that are longer than 80 characters since there will be good modularization, minimal nesting (that leads to a lot of indentation) etc.

Looking for an XSD representing an 'Order' for a shopping cart

I am trying to create an XML schema representing an 'order' for a shopping cart

I want this to completely abstract away my shopping cart's implementation -- and eventually support partners sending us orders using this schema. (yes i'm planning on integrating this schema into our existing cart)

It will have original order items, repeat shipping items and domain specific things. I'm quite capable of building this, but i was wondering if there are many things out there like this that I could at least base mine upon.

I know there are standards out there for certain schema elements like this, but I've lost track of which are the best/standard and how you might extend them etc.

obviously if i want a partner to send me an 'order' i'd like to use a standard if one exists.

From stackoverflow
  • Do you want the XML, or the XSD? For the XSD, you can generate one using Microsoft's XSD generator based off of an XML document.

    If you want a generic XML document that could represent an 'order', here's one.

    <?xml version="1.0"?>
        <Order>
     <Date>2003/07/04</Date>
     <CustomerId>123</CustomerId>
     <CustomerName>Acme Alpha</CustomerName>
    
       <Item>
     <ItemId> 987</ItemId>
     <ItemName>Coupler</ItemName>
     <Quantity>5</Quantity>
     </Item>
    
    <Item>
     <ItemId>654</ItemId>
     <ItemName>Connector</ItemName>
     <Quantity unit="12">3</Quantity>
     </Item>
    
    <Item>  
     <ItemId>579</ItemId>
     <ItemName>Clasp</ItemName>
     <Quantity>1</Quantity>
     </Item>
    
    </Order>
    

    From here.

    Simon_Weaver : thanks. xsd or xml is fine. much easier to read XML on here anyway. i was more wondering about more complicated scenarios - including more complex thins like shipping, discounts, addresses, partner specific things. especially interested in standards based thins
    George Stocker : Great. What have you tried so far? What are your business requirements? Unfortunately, your question comes off as 'plz send teh codez', and it's very tough for the SO crowd to do that, especially when you haven't shown any of your work, as it were.
  • If you are looking for ideas about how to structure the shopping cart:
    http://stackoverflow.com/questions/360959/database-table-structure-for-shopping-cart

  • UBL (Universal Business Language) defines schemas for business documents (purchase orders, invoices, etc.). It is an OASIS standard, see:

    http://www.oasis-open.org/committees/tc_home.php?wg_abbrev=ubl

    Simon_Weaver : this is exactly the kind of thing i'm looking for. thanks

Accessing the Document class in AS3

How can instantiated classes access the Document class?

Even after I name the Document class using the Properties bar in Flash, attempting to access it from other classes usually fails, saying "attempting to access an undefined property...

One solution is always casting the Document class to itself! eg.

Main(Main).globalMethod();

But sometimes even this stellar gotcha fails, and then there's usually no way out, apart from the obvious!

class Other{

   var parentClass:Main;
   public function Other(parent:Main){
       parentClass = parent;            // pointer to the Main class in a local var!

       Main(parentClass).globalMethod();
   }
}
From stackoverflow
  • The document class is not inherently a globally accessible object. If you want to call methods that are in the document class, you'll always have to pass a reference from Main to any other classes/objects that want to call its methods. A more object oriented approach would be to dispatch events from your other classes (Other) for the Main class to listen to and call an appropriate method in itself.

    If you are unconcerned about keeping a good OOP structure and want to access the document class from a display object that has been added to the display list you could try something like: stage.getChildAt( 0 );

  • You can use a singleton for your document class (Main, in your example), which allows you to access the instance from anywhere.

    public class Main extends Sprite {
        private static var _instance:Main;
        public static function get instance():Main { return _instance; }
    
        public function Main() {
            _instance = this;
           // etc...
        }
    
        // etc...
    }
    

    Then you access the Main instance like this:

    public class Other {
        public function Other() {
            Main.instance.usefulInstanceMethod();
        }
    }
    

    The document class is a pretty good candidate for the singleton pattern, because generally there should only be instance available.

    Jenko : Is the "instance()" property really necessary in the Main class? I mean can't you just access the "_instance" pointer variable?
    Matt W : Thats not a Singleton, that is more of a Monostate.
    aaaidan : Jeremy: The instance property function just ensures that only the Main class could possibly change the private _instance var. Notice that there is no setter. Matt: Thank you for enlightening me.
  • Just a side note, but the shortest answer to this question is: the same way any class access any other class. That is, with either a direct reference or a static exposure. The document class is no different from any other class in this regard.

silverlight: reflection effect

I've seen examples in silverlight where the achieve a effect using 1 of 2 ways:

  1. Two image objects, both displaying the same image, but the bottom one rotating it 180 degrees and applying a gradient image opacity
  2. A MediaElement object and a VideoBrush element.

I have a series of path objects, rectanges, etc which I would like to apply a reflection affect to. Is there a more elegant solution other than copying my objects manually and rotating them? Unfortunately the VideoBrush object only works on MediaElement objects, but is there something else I can use?

From stackoverflow

How to block the UI thread from another thread or force a form to run within the UI thread

A requirement for my application is if it looses database connectivity then it must pop up a big modal "No Connection. Try again later" dialog blocking all user interaction until such time that connectivity is regained.

I achieve this by at the start of the application starting an instance of a DeviceMonitor class. This class creates a System.Threading.Timer and every Tick (usually 1 second) along with a few other things it tries to draw data from the database. If the draw fails and the cause is determined to be due to a lack of connectivity, the exception is handled by popping up the aforementioned dialog. Likewise, if the data fetch succeeds and the dialog is currently up hen it is forced closed.

The problem is that although this all works fine, the ConnectionLost dialog does not block the user from interacting with the UI. This makes sense, since the Timer.Elapsed event is raised within its own thread and noConnectionDialog.ShowDialog() is called from within the callback it blocks the thread it is on but not the UI Thread.

To my understanding I need to either force the noConnectionDialog.ShowDialog() to run within the UI thread or to block the UI thread until noConnectionDialog.Hide() is called but I don't know how to do either.

Perhaps there is some other remedy or I am missing something here. Any advice is appreciated.

EDIT: Further information - this is a stylized dialog, not just a messagebox. It is being created when my application starts by Castle Windsor and injected into a DialogFactory class which gets passed around. The dialog is therefore accessed by

var d = _dialogFactory.GetNoConnectionDialog();
d.ShowDialog();

I have experimented with putting this code outside of the timer elapsed callback - when a button on UI interface is clicked for example - and it blocks the UI just fine from there so this is not a matter of where the form is created.

From stackoverflow
  • If you have access to a UI element, you can push to the UI thread by using things like:

    someControl.Invoke((Action)delegate {
        MessageBox.Show(someControl, "Foo");
        // could also show a form here, etc
    });
    

    (the someControl in MessageBox.Show helps parent the message-box)

    If you don't have access to a UI control, you can also use sync-context:

    SynchronizationContext.Current.Post(delegate {
        MessageBox.Show("Foo");
    }, null);
    

    But it is easier to keep hold of a control ;-p

    George Mauer : This is not a messagebox however. Or rather its a stylized message box so it has to be a full form. I do have a decorator wrapping the dialog that calls Invoke() if necessary so I thought it would be running on the UI thread, but apparently it is not.
    Marc Gravell : The form will want to run on the thread that creates it. So create the form within the Control.Invoke
    George Mauer : The form is created by Castle Windsor and is being DIed into a dialog factory class so I don't think this is a problem. When I have it called from a place other than the device monitor it blocks the UI thread just fine
    George Mauer : please see my edit
    George Mauer : System.Threading.SynchronizationContext.Current == null !!!! What the heck could that mean?
    Marc Gravell : A null sync-context means you don't have a UI-pumping thread, i.e. a primary winform.
    RichardOD : +1 for SynchronizationContext. This still remains a little used feature.
  • I'm pretty sure what Marc suggested should work. This is how I would write it to use your dialog instead of MessageBox:

    someControl.Invoke((Action)delegate {
        var d = _dialogFactory.GetNoConnectionDialog();
        d.ShowDialog();
    }, null);
    

    If that really isn't working I've had success in the past using a Timer control (System.Windows.Forms.Timer) on my form and a queue of Actions with an Tick function that looks like this:

    void timer_Tick(object sender, System.EventArgs e)
    {
        lock(queue)
        {
            while(queue.Count > 0)
            {
                Action a = queue.Dequeue();
                a();
            }
        }
    }
    

    And when your DeviceMonitor class needs to show the UI it would do this:

    lock(queue)
    {
        queue.Enqueue((Action)delegate 
        {
            var d = _dialogFactory.GetNoConnectionDialog();
            d.ShowDialog();
        });
    }
    

    That being said, I really want to reiterate that I think Marc's method should work correctly and I would only use my Timer + queue method if you're absolutely certain that Control.Invoke won't work for you.

  • This is called marshaling and is a very simple concept once you read some good material on it (Google is your friend).

    If your background thread has a delegate that calls into an object that is owned by the UI thread, then that method (on the called end of the delegate) simply has to marshal itself back onto the thread that owns its object (which will be the UI thread) and then block. It's very simple code (IsInvokeRequired), you just have to understand how to lay things out. (This is basically restating what Marc said, but from a higher level.)