Friday, May 6, 2011

identifying code improvements

We've just gone through a pretty major system rewrite and I have been asked to find and identify areas of the code that have been improved. as a way to justify to the customer that the effort we've spent was worthwhile. Identifying the areas isn't really the hard part but I'm struggling with how to best present this information. Any suggestions on this, or if anyone ahs done something similar in the past would be appreciated.

From stackoverflow
  • It depends on the customer: is the customer technically-minded? Are they internal or external? How much detail do they want? Were you asked to rewrite, and if so by whom and for what reason?

  • In effect, this is a reduction of your technical debt. All of the benefits you would normally receive from that sort of effort will also apply here. Some of the effects will be forward-looking. For example:

    • Reduced defect rate as a result of better, clearer APIs.
    • More rapid development time (and lower costs) because interfaces are easier to test and have fewer integration points.
    • Easier to maintain old code, because the layers they depend on will now be cleaner and freer of cruft.

    Some of them will be immediate, however:

    • Faster build because there's less cruft.
    • Identification of bugs that wouldn't have been found until production because unit testing these areas was too hard.
    • Better separation of layers and concerns.

    The degree to which these sorts of benefits apply will, of course, be specific to your project and its code base.

  • Since it is for non technical people I would suggest Bar and Pie charts. Nothing helps non technical people grasp complex subjects like a big colorful chart.

C++ Iterators Considered Harmful?

At the Boost library conference today, Andrei Alexandrescu author of the book Modern C++ Design and the Loki C++ library, spoke about why iterators are bad, and he had a better solution.

I tried to read the presentation slides, but could not get much out of them. I have these questions for the StackOverflow community:

  1. Are iterators bad?
  2. Is his replacement really better?
  3. Will C++ implementators pick up his ideas?

http://www.boostcon.com/site-media/var/sphene/sphwiki/attachment/2009/05/08/iterators-must-go.pdf

From stackoverflow
  • I agree with him that iterators are mostly inferior to ranges, and I don't know if 'something better' will get picked up.

    "The good is the enemy of the best" is strongly at play here, as it usually is. Iterators are useful and firmly entrenched, so it's hard to know if something better like ranges can supplant them in a reasonable amount of time.

  • I think C++ implementors will have their hands full producing full working support for C++0x, without implementing new, non-standard paradigms.

    Unknown : Aren't ranges standard in some other languages though?
    anon : So what? Lots of things are available in other languages that are not, and probably never will be, avaiolable in C++.
    1. Sometimes
    2. Probably
    3. Not likely, at least not for many years
    Binary Worrier : Almost a haiku, well done twentieth century boy, give us more?
  • First, to answer your questions:

    1. No. In fact, I argued elsewhere that iterators are the most important/fundamental concept of computer science ever. I (unlike Andrei) also think that iterators are intuitive.
    2. Yes, definitely but that shouldn't come as a surprise.
    3. Hmm. Looking at Boost.Range and C++0x – haven't they already?

    Andrei's big contribution here is just to say: drop the concept of iterators altogether, see ranges not just as a convenience wrapper but rather as a core construct. Other languages have already done this (much of Andrei's concepts just echo .NET's LINQ or Python's iterators) but they all only offer output ranges. Andrei argues for different types of ranges, much like the conventional iterator categories.

    In that light, it's odd that he starts by mocking the arbitrariness of these iterator categories.

    I also think that his examples are off, especially his file copying: yes, the iterator variant is a huge improvement over the 1975 code. It reduces a loop with complicated break condition down to one statement. What he's really taking issue with here is just the syntax. Well, excuse me: we're talking about C++ here – of course the syntax is ugly. And yes, using ranges here is an improvement – but only syntactically.

    I also think that Andrei's find implementation is off. What he really defines there is the DropUntil operation (naming is hard!) from LINQ. The find operation should really return either one or zero elements (or an iterator!). Shunning iterators here isn't helpful in my opinion since we might want to modify the value directly instead of copying it. Returning a one-element range here only adds overhead without a benefit. Doing it Andrei's way is bad because then the name of the method is just wrong and misleading.

    That said, I essentially agree with Andrei in almost all points. Iterators, while being my pet concept from computer science, are certainly a big syntactical burden and many ranges (especially infinite generators) can (and should) be implemented conveniently without them.

    James Hopkin : +1: Not sure I agree with everything here, but very interesting to read. Almost as provocative as the great Alexandrescu himself :-)
    wilhelmtell : That's why I love programming more than math. There are opinions and we get to argue. Much more interesting this way. Also: the stream iterators solved the infinite range problem, so it IS possible. Of course, with an extra member variable for the default constructor signaling the end iterator does impose an overhead, as compared to a `while( true )` loop, but how significant is this overhead?
    Konrad Rudolph : @wilhelmtell: You’re right, infinite ranges are absolutely expressible via iterators but the syntax is cumbersome since we have to define some arbitrary “end” iterator whose basic semantics is just to be unequal to all other iterators. That’s quite artificial. I don’t believe that the runtime overhead is significant here – possibly non-existent (no idea really). I just find it weird to call an operation with a start and an end iterator when in reality there’s no end.
  • I think we should use ranges next to iterators, i.e. we should choose the evolution way, not revolution way.

  • C++0x is already making the first steps:

    • rvalue references solve some problems with treating containers as ranges
    • ranges have been added to the core library, including range concepts

    Transitioning to ranges without losing any iterator functionality (think of all the combinations of iterator categories, const-ness and rvalue-ness) is hard, especially if you try to factor in infinite and mutable ranges.

    1. Most of us make a simple use of them in what have become well known idioms, like in for loops to iterate through an std::vector. A developer reads it and knows what's going on. In our everyday coding life, iterators are not good or bad, they're just "what gets the job done".
    2. Probably, yes.
    3. I don't think so.
  • Andrei at times can be a bit provocative. Iterators are a reasonable concept, and quite fundamental in the sense that bits are. But just like most bits in C++ are not bools, but part of larger types,most iterators should be dealt with at a high level. Andrei is right that the proper level to do so is the range object. But not all ranges are properly exposed as iterator ranges, as the istream_iterator sentinel shows. That's just a hack to create an artificial end iterator. I don't think his ideas will be picked up by implementations, though. C++1x will be as relevant as C99.

  • The only argument I can see from that presentation is the inability to define ranges, and the c++0x "Range for statement" proposal seems to eliminate that problem to some extent anyway. maybe it shouldn't be an argument about if iterators should / shouldn't be used at all, but more what situations should / shouldn't they be used for?

  • Like any API or function, if misused can create many problems of identification difficult. Iterators have used in many projects, but always maintaining the necessary care required according to their characteristics. Its use should be preceded by a good understanding of their limitations. Iterators can be very useful if user properly.
    This questions are related :
    Is there any way to check if an iterator is valid?
    Should I prefer iterators over const_iterators?

  • I disagree with both Andrei and Konrad and myself :-)

    The most fundamental concept is an interface not an iterator and that is pretty obvious in any work anyone does today (which is all about cross-library, cross-language, cross-compiler, cross-OS, cross-platform, you cross-name it :-)

    Neither iterator or range (apart from source-level use) offer anything more than a clean and simple, non intrusive or intrusive, non shared or shared, non unique or unique: pointer ! Clean pointer to typed data is simply put universal and you can make data mutable or immutable and many other things. All interface is is just another level of indirection to it while still being friendly to machine and compiler of all sorts, plus far safer, relegating iterators and range usage to an implementation detail.

    To that extent IEnumerable and IQueryable do the half 'right thing' TM but they are clearly inferior in their concepts of iteration and much more to what you can do with STL, retain control and so on and on (but otoh, they have better metadata and hence a better, cleaner model). Point being with interfaces you can build any abstraction you want and satisfy, well probably contraversial but essentially a no-brainer: optimal, and runtime or compile-time neutral data representation and code (heck essential to algorithms and compilers and VMs and what not).

    It is even possible to optimise it for 'dynamic'/component systems down to 'runtime' inlining (screw HotSpot VM:-).. To that extent, the advance to 1975 is minimal as evident by a huge interop industry workload (it's everywhere you look, including this site, its use of proprietary and open tech, etc; in computer science idealism, well, this type of interfacing 'work' should not exist should it)..

    Konrad Rudolph : The point is that you need a *well-defined* interface (or a set thereof). So the question is really: *which* interface to use? The one offered by iterators? Ranges? …
    rama-jka toti : Completely agree.. imho, something that won't ever be technology or single-idea specific is the way forward, domain and model driven tools will have the same tough time there. Plus the world is such it enforces one proprietary solution over another for vendor lock in especially with VMs and their own ideas of integration (market share). But it all still favours K&R idea as minimal and complete, much like templates and concepts in C++ push forward, or indeed metadata of execution environments help another aspect of 'reshape'/reuse. Protocols are simply hard to do and follow.
    rama-jka toti : On a side note, what's worst, it makes tweats like Linus right in C++ being a moving target for almost fundamental abstraction change that impacts client code considerably (or to point of ease-of-breakage). Then again, Linux kernel is in such a state it wouldn't be suitable for anything but OS and down-to-metal building (which is sufficient I guess but still inflexible for future). Not that other OS-es are not copying and reinventing *nix, ie. W6.0, W7 especially, but modularity doesn't come from dependency breaking solely.[someone fix the crappy ASP.NET HTML constraints for this site PLEASE]
    rama-jka toti : Reuse goes beyond module/assembly and OO here and it applies to protocols and mechanisms that are effectively algorithms or algorithm friendly (CLR isn't; ie. numerics ). Sequences are a problem for every protocol with complex, non-primitive types, heck even simple arrays can exhibit similar issues. LINQ won't cut it everywhere as evident with continous streaming attempts too, it is as bloated and heavy as any high-level protocol.
    jalf : I don't think you answer the question. Yes, we need an interface. What should it look like? Iterators define an interface. So do ranges. But "interface" does not.
    rama-jka toti : Sure, no answer.. but what you certainly want to see is change of the interface in the model rathen than source. As it currently stands, any change simply breaks code and idioms. Something many libraries are popular for; all I want is to change it as say underlying hardware might prefer something else. Why should it be a specific interface? So somebody can come and say look that abstraction is now 'outdated' yet again? It is typical of C++ and will be typical of C++ reinventing itself which is what the question is about.
    rama-jka toti : Note that I'm not in favour of iterator or range or IEnumerable or IQueryable.. my argument is for neutrality and none of those give me anyone of it cross-language, cross-compiler, or anything similar. Which is just the sign of the times, you pick yours, someone else picks another one, depending on the phase of the moon. I'd prefer to be able to say I don't care whether it is an iterator or a range, but that might be just me..
  • Isn't Andrei trying to do some hidden marketing for the D language (currently he is working with it)...?

    Andrei states that containers are ok, but iterators are ugly, non-intuitive, error-prone and dangerous, hard to implement (well this last one seems to be rather true...) And what do we have in C++... pointers? Aren't they ugly/.../dangerous? But we happily embraced them and live with them.

    Which one is more intuitive to write:

    for(auto i=foo.begin();i!=foo.end();++i)
        bar(*i);
    

    or

    for (auto r=foo.all(); !foo.empty(); foo.popFront())
            bar(r.front());
    

    Iterators concept can be complemented with ranges and other ideas, but I think that they have their place and won't be replaced.

    Unknown : "Isn't Andrei trying to do some hidden marketing for the D language" I was wondering exactly the same thing.
    rama-jka toti : He is,and he is also behind some of the constructs that got far more refined.Boost is continously expanding and rewriting itself the compile-time-STL way but it does break interfaces and it brings complexity that kills with plenty of bugs.I don't care whether it is C++, D or C#, iterator or not. I just want maintainable code without going into that level of detail, just like I don't want to go into MPL in detail (it's a suicide, something machine translation should be doing on your intent an not a brain picking over syntax to grasp its meaning with usual keyboard-reflex :). They will 'go'.
    1. No, they are not bad, they are very clever idea in fact. However, they are not ideal and there is room for improvements in the concept of iterator.

    2. It solves number of real-life problems with iterators. For instance, it's tedious (also error prone) in many cases to query two separate objects, iterators, from a single containers and then pass them as still two separate objects to an algorithm. Why not to pass a single object around? Even std::pair<iterator, iterator> would make for a crude range which is easier to manipulate - one object, not two. Also, it's a good idea to consider a range is an iterator. That's in fact what Andrei suggests. By the way, some of these problems have been already solved by Boost.Range.

    3. I would expect it happened, but it will not be a revolution, rather evolution.

Is it possible to select all floats on a page with JQuery ?

I am trying to select all elements that have CSS computed style float:left or float:right. I see attribute selectors available in the JQuery documentation, however I am interested in CSS properties not attributes.

Possible use cases for such a feature would be to select say all display:none elements on a page.

From stackoverflow
  • One idea would be to apply the floating style via CSS then you could select on the class name and do a show/hide as necessary.

     <style>
        .right { float: right; }
        .left { float: left; }
     </style>
    
     <div class='left'>...</div>
     <div class='right'>...</div>
    
      $('.left,.right').hide();
    
    Jed Schmidt : $('.left.right') would be empty in this case, since you're looking for elements with both classes. Did you mean $('.left, .right')?
    tvanfosson : The interesting bit is that I had it that way originally and then must have misread the documentation. Yes, the comma needs to be there.
  • This should do the trick without class hacking:

    $("*").filter( function() {
        return /^(left|right)$/.test( $(this).css("float") )
    })
    

    By the way, jQuery already has a nice way to find all display: none elements:

    $(":hidden")
    
  • Creating new selectors is kind of fun, so I did that:

    Usage:

    :hasCssAttr(property, value ...)

    Property is the css property you would like use to compare

    value is the value(s) you would like to match against (you can have more than one)

    $(':hasCssAttr(float, left)').css('float', 'right');
    

    The source Luke:

    $.expr[':'].hasCssAttr = function(objNode, intStackIndex, arrProperties, arrNodeStack) {
      var arrArguments = arrProperties[3].split(','); 
      var cssPropVal = $(objNode).css(arrArguments[0]); // need for speed
      for (var i = 1 ; i < arrArguments.length ; i++)
        if (cssPropVal == arrArguments[ i ].replace(/^\s+|\s+$/g,""))
            return true;      
      return false;
    }
    

    Basically, this selects any ol' css property. I suppose you could eliminate the loop if you only wanted one value, kind of unnecessary. Also, I wonder if it might be more interesting to do this in an eval so you could do numerical comparisons. Anyway. There it is.

    Props to Ben for helping me out.

    KyleFarris : +1 for making your own flexible selector. I always prefer flexible solutions when possible.

Looking for a set of tools/objects to start a website in PHP

Looking for a good set of base objects to start a website up in PHP. I am not looking for links to CakePHP as I am not interested in frameworks. I am looking for a set of objects that would come handy to start off with for new projects...

Objects such as:

  • Loggers
  • MySQL wrapper object
  • etc

Basically a "Start with this", I remember seeing a nice collection of objects and code on Digg, but I can't find it again. There were user objects (for logging in), fsck editor built in, etc...

From stackoverflow
  • MySQL wrapper object

    Use PDO.

    I am not interested in frameworks

    Well, you are. You are not looking for a tightly coupled, full stack framework perhaps.

    You might find one or more of the following useful:

    They are all very loosely coupled frameworks, where you can pick and match as you need it.

    Mike Curry : hmm, I've been looking at Zend... seems it is exactly what I want. I've been against frameworks all this time... and they are exactly what I wanted... in this case, ignorance is NOT bliss, lol. Thanks :)
  • Well, there's a bunch of digg related code found here.

    http://github.com/digg/

    I also don't understand why you don't want to use a framework, but want to use all the components that make a framework. That didn't make much sense to me.

    I would look into the Zend Framework. Before you scream at me for recommending it, a lot of the components are modular which means you can take the bits you need.

    http://framework.zend.com/manual/en/

    PS : Although Zend Framework has "framework" in the title, a lot of people see it as a collection of classes. So check it out :)

  • Frameworks by their very definition are a set of objects that come in handy when starting a new project.

    PDO can give you database abstraction without resorting to third-party libraries. Templating can be accomplished with the alternate syntax (foreach():) and short tags (<?= $var ?>). You can do it all yourself if you want, just keep the docs up and get crackin' redefining that wheel.

  • I can certainly recommend Zend Framework for this purpose. As a glue framework you can use as many or as few of the components as you like, with or without the MVC.

Call external dll function (c#) from postgres...

Is it possible? How?

Thanks!

From stackoverflow
  • It looks like there is some overhead to calling C functions from PosgtreSQL, External Functions in Postgres @ Linux Gazette covers writing such a method in C. I would surmise from that that you could, if you REALLY need to, build a hosting module that loads a CLR instance and provides an entry point into your C# method that is mapped with the appropriate Postgres linking logic. This seems extremely costly.

    Another approach that might be better would be to encapsulate the C# method (which I am assuming is already written) in a web service, and use a wrapper (not necessarily C, but one of the lighter-weight extension development bindings for Postgres) that will call out to the web service.

  • You can define an external function using CREATE FUNCTION. This reference implies that the DLL must be a native DLL using the C calling convention and hence cannot handle managed code.

    You could try to use Mono AOT or MS Native Image Generator to compile the managed code to native code, but I cannot tell if this will result in a native DLL using C calling convention. You have to try.

    If this does not work, you could create an unmanged wrapper for your DLL.

    UPDATE

    You have to write a wrapper - C-Language Functions states that the DLL must include a magic block that will not be availiable if you compile managed code to native code.

What steps do you take when writing a really large business processes?

I was given this really long flow chart (8 pages) of how we process our financial transactions. The most critical piece of our application. I'm sure you can imagine lots of squares and diamons with lines drawn all over the place, and then separate detail sheets describing which fields in the database to update, calculate, retrieve, store, etc. etc. etc.

Does anyone have any tips/tricks/suggestions as to the best approach to coding this thing? Do you start with 5 billion if statements just like the flow chart does and then refactor from there?

Any thoughts are appreciated.

From stackoverflow
  • I like to extract some business object out of the mess first. Try to come up with some classes to separate the responsibilities using the typical low coupling, high cohesion guidelines. After that, look for duplication in the flow and think of ways to keep that duplication out of the code.

  • Personally, I look for patterns that I can turn into reusable functions. Often I'll find several different areas that I can create very generic functions that can handle multiple tasks, depending on the parameters I pass in.

    Tetsujin no Oni : -1 for the blatant violation of SRP
  • You have my sympathy.

    That being said, look for anything you can abstract away. Look for similarities. Take more or less coherent sections and write functions out of them.

    Then, when you've done your best, be prepared to spend a lot of time refactoring.

    Then, since I don't believe humans can write accurate 8-page flow charts, be prepared for people blaming you for stuff that doesn't work right. Ideally, you should be able to trace your logic back to the flow chart, and at least divert the blame. Unfortunately, this is not really compatible with refactoring it to something sane.

    Micah : Amen on the blame point!
  • For something that large, you'd better have a good model in place. Look at your business process and identify the actors, the acted-upon, etc. Once you have that model in place, start to flesh it out - work out what a given step means in terms of the actors and the acted upon, determine whether the actor or the acted upon is the best place to handle that part of the interaction, and so on.

    This will translate your big long flow chart into more of a Sequence Diagram which will help you turn the flow chart into code, by identifying the methods you'll need on your objects (and potentially identify more objects that you overlooked in the first place).

    Granted, this assumes an OO perspective, so if you're doing this in COBOL it may not help much.

  • First off, don't do it. Seriously, don't.

    You are going to take ownership of a few hundred processes that not only are not your job, they aren't your specialty either. More so, you won't do it right ( trust me ) and you will be creating a full time job ( or 6! ) fixing and maintaining a system that never works right.

    If you are in an environment that can generate such a large business process, I hope you are in an environment where you can get reasonable funding. What you want to look into is BPM software, meaning Business Process Management. Companies like Captaris, K2 and Ultimus all develop products, as do big boys like IBM and the like. These tools allow you to give the tools to business experts, so the people in the process that actually know what they are doing, then you have domain experts controlling their own domains, generally in a very excel like environment, but sometimes using tools that look alot like Visio ( or.. that are visio! ). You as a developer then, provide the transport between departments, and specialized logic to tie your various business systems together ( or to pull data from various sources.

    Granted, BPM software start in about the 20K range, and the sky is the limit from there. Plus, as you expand you will probably want a document repository like Sharepoint ( which can start from as low as free and up to 100K+$ ) and a forms system like Adobe or Infopath, but all told, you can build a scalable end to end system starting around 30 - 40 grand, depending on the number of users you have.

    That might sound like an obscene amount, but truly it isn't. Factor in you labour costs, all the labour costs of the various people you will have to meet with, the fact your are probably creating a full time job for a few people and creating a system that probably will never really be 100%... the money is well spent.

    Keep domain specific knowledge in the knowledge experts hands, and keep IT specific tasks in IT's hands. This is an area where I see IT department after department screw up and end up with highly valuable employees getting paid high salaries to baby sit report generation. Honestly, I say again, don't do it! This is one of those areas where off the shelf products will be vastly superior than anything you can create.

    (Note, most of my examples betray my most recent background. I am from a Windows shop with a .Net centric development team. You can just as easily find BPM solutions for Unix, Linux, whatever, including a few open source options with Java as the back end instead. )

    EDIT: Oh, and for the record, if you absolutely can't buy off the shelf, if your budget is zero $, you will find most commercial BPMs implement a state based machine, with a gated flow chart-eqsue structure with a database ( or even spreadsheet ) driven back end. If you need to do it yourself, you would still be well served trying a demo from any of the various BPM providers and emulating their approach.

    EDIT2: Links. Captaris

    Ultimus

    Open Source (ProcessMaker)

    There are tons more, but these will get you started. BPM software is one of those things that can go from thousands to millions of dollars, and potentially free. I can only speak towards Captaris, which I choose about 3 years ago and worked as advertised.

  • If the process requires any human interaction (i.e. decision making) and is long running or there are lots of similar business processes, then a workflow (BPM) product like K2 or ProcessMaker might be appropriate. If your business process is largely orchestrating data flows then a product like BizTalk might be appropriate.

    Note that there is a significant learning curve for workflow programming (generally due to poor debugging support). So, if you aren't already familiar with workflow programming and quick turnaround is required, you probably don't want to go down this path.

    If your task is to implement a relatively fast process (meaning it runs from beginning to end without pause and does not require human interaction), then I would tackle it in the following manner:

    1. identify the process steps
    2. Code a runnable shell of the process without implementing any of process step details, along with (failing) unit tests for each process step and for the overall process.
    3. identify the subsystems the process interacts with
    4. identify the business objects each process step interacts with
    5. identify the data flows to and from each subsystem
    6. identify the contracts for interacting with the business objects
    7. if necessary, code the business objects, with unit tests for functionality required in the final product
    8. implement the process steps one by one until all the unit tests pass.
    9. debug the overall process (add unit tests if necessary)
    10. integration testing (add unit tests if necessary)
    11. deployment
  • I totally agree with David T - no human can create an 8 page flow-chart and have it be a solid-bullet proof set of steps. I would change to a state diagram (http://en.wikipedia.org/wiki/State_diagram) basically describing the life cycle of the financial transaction - from conception to final state. This would probably help with the overall approach you take to the design and implementation of the process and greatly reduce the nested if statements (each nesting exponentially increases the complexity and brittleness of the code). If each step in the life cycle is clearly decoupled you can then easily modify as needed (yes - business needs change - ha ha) without impacting the other steps. Keep in mind testing needs and logging to ensure each step is clearly recorded for traceability and debugging.

Fixing BeanNotOfRequiredTypeException on Spring proxy cast on a non-singleton bean?

I'm having an issue with pulling a Spring bean from an application context.

When I try;

InnerThread instance = (InnerThread) SpringContextFactory.getApplicationContext().getBean("innerThread", InnerThread.class);

I get;

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'innerThread' must be of type [com.generic.InnerThread], but was actually of type [$Proxy26]

Without the specified class in the getBean() call I get a ClassCastException (which you can see in detail below).

The InnerThread bean is being initialized as a non-singleton, because I need multiple instances. The InnerThread class also extends Thread. The interesting thing is that this error shows up within OuterThread, which is set up in the exact same way the InnerThread is.

I've tried to include all relevant code listings/stack traces below. Could someone with more Spring experience tell me what is going on here?


Code/Configuration Listing

OuterThread.java snippet:

public class OuterThread extends Thread {
    private Queue<InnerThread> createInnerThreads() {
     Queue<InnerThread> threads = new ArrayBlockingQueue();

     ApplicationContext ctx = SpringContextFactory.getApplicationContext();
     int i = 0;
     for (SearchRule search : searches) {
      logger.debug("Number of times looped " + i++);
      //Seprated lines to get a better sense of what is going on
      Object proxy = ctx.getBean("innerThread", InnerThread.class);
      logger.debug(ReflectionToStringBuilder.toString(proxy));
      logger.debug("proxy.getClass(): " + proxy.getClass());
      logger.debug("proxy.getClass().getClassLoader(): " + proxy.getClass().getClassLoader());
      logger.debug("proxy.getClass().getDeclaringClass(): " + proxy.getClass().getDeclaringClass());
      logger.debug("InnerThread.class.getClassLoader(): " + InnerThread.class.getClassLoader());

      //---Exception here---
      InnerThread cst = (InnerThread) proxy;

      threads.add(cst);
     }
     return threads;
    }

    public static void main(String[] args) throws Exception {
     try {
      OuterThread instance = (OuterThread) SpringContextFactory.getApplicationContext().getBean("outerThread", OuterThread.class);
      instance.run();
     } catch (Exception ex) {
      logger.error("Fatal exception.", ex);
      throw ex;
     }
    }
}

SpringContextFactory.java:

public class SpringContextFactory {
    static final Logger logger = LoggerFactory.getLogger(SpringContextFactory.class);
    private static ApplicationContext ctx;
    private static final String DEFAULT_PATH = "META-INF/app-context.xml";

    public static ApplicationContext getApplicationContext() {
        return getApplicationContext(DEFAULT_PATH);
    }

    public static synchronized ApplicationContext getApplicationContext(String path) {
        if (ctx == null) return createApplicationContext(path);
        else return ctx;
    }

    private static ApplicationContext createApplicationContext(String path) {
        if (logger.isDebugEnabled()) logger.debug("Loading Spring Context...");
        ctx = new ClassPathXmlApplicationContext(path);
        if (logger.isDebugEnabled()) logger.debug("Spring Context Loaded");
        return ctx;
    }
}

app-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <!-- persistence context from separate jar -->
    <import resource="persistence-context.xml"/>

    <bean id="outerThread" class="com.generic.OuterThread" scope="prototype"/>
    <bean id="innerThread" class="com.generic.InnerThread" scope="prototype"/>

</beans>

Stack Trace

2009-05-08 14:34:37,341 [main] DEBUG com.generic.OuterThread.init(OuterThread.java:59) - Initializing OuterThread object, com.generic.OuterThread@1c8fb4b[em=org.hibernate.ejb.EntityManagerImpl@e2892b,currentTime=java.util.GregorianCalendar[time=1241634874841,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/New_York",offset=-18000000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/New_York,offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2009,MONTH=4,WEEK_OF_YEAR=19,WEEK_OF_MONTH=2,DAY_OF_MONTH=6,DAY_OF_YEAR=126,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=2,HOUR_OF_DAY=14,MINUTE=34,SECOND=34,MILLISECOND=841,ZONE_OFFSET=-18000000,DST_OFFSET=3600000],maxConcurrentThreads=5,reconId=3,reportUsername=TEST,useOffset=false,username=removed,uuid=bf61494d-ff96-431c-a41f-1e292d0c9fbe,name={T,h,r,e,a,d,-,1},priority=5,threadQ=<null>,eetop=0,single_step=false,daemon=false,stillborn=false,target=<null>,group=java.lang.ThreadGroup[name=main,maxpri=10],contextClassLoader=sun.misc.Launcher$AppClassLoader@11b86e7,inheritedAccessControlContext=java.security.AccessControlContext@1524d43,threadLocals=<null>,inheritableThreadLocals=java.lang.ThreadLocal$ThreadLocalMap@2cbc86,stackSize=0,nativeParkEventPointer=0,tid=9,threadStatus=0,parkBlocker=<null>,blocker=<null>,blockerLock=java.lang.Object@a68fd8,stopBeforeStart=false,throwableFromStop=<null>,uncaughtExceptionHandler=<null>]
2009-05-08 14:34:37,341 [main] DEBUG org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.doJoinTransaction(ExtendedEntityManagerCreator.java:385) - No local transaction to join
2009-05-08 14:34:37,529 [main] DEBUG com.generic.OuterThread.createInnerThreads(OuterThread.java:139) - Number of times looped 0
2009-05-08 14:34:37,529 [main] DEBUG org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:458) - Creating instance of bean 'searchThread' with merged definition [Root bean: class [com.generic.InnerThread]; scope=prototype; abstract=false; lazyInit=false; autowireCandidate=true; autowireMode=0; dependencyCheck=0; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [META-INF/app-context.xml]]
2009-05-08 14:34:37,545 [main] DEBUG com.generic.InnerThread.<init>(InnerThread.java:50) - Constructing InnerThread object, com.generic.InnerThread@1080876[em=<null>,coolScheme=<null>,coolUrl=<null>,date=<null>,error=<null>,millisecondsTaken=0,thresholdMet=false,reconId=0,result=-2,searchId=0,username=<null>,uuid=<null>,name={T,h,r,e,a,d,-,2},priority=5,threadQ=<null>,eetop=0,single_step=false,daemon=false,stillborn=false,target=<null>,group=java.lang.ThreadGroup[name=main,maxpri=10],contextClassLoader=sun.misc.Launcher$AppClassLoader@11b86e7,inheritedAccessControlContext=java.security.AccessControlContext@1524d43,threadLocals=<null>,inheritableThreadLocals=java.lang.ThreadLocal$ThreadLocalMap@3aef16,stackSize=0,nativeParkEventPointer=0,tid=10,threadStatus=0,parkBlocker=<null>,blocker=<null>,blockerLock=java.lang.Object@126c6ea,stopBeforeStart=false,throwableFromStop=<null>,uncaughtExceptionHandler=<null>]
2009-05-08 14:34:37,545 [main] DEBUG org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:203) - Returning cached instance of singleton bean 'entityManagerFactory'
2009-05-08 14:34:37,545 [main] DEBUG org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:203) - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
2009-05-08 14:34:37,560 [main] DEBUG org.springframework.transaction.interceptor.AbstractFallbackTransactionAttributeSource.getTransactionAttribute(AbstractFallbackTransactionAttributeSource.java:108) - Adding transactional method [report] with attribute [PROPAGATION_REQUIRED,ISOLATION_DEFAULT]
2009-05-08 14:34:37,560 [main] DEBUG org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.buildAdvisors(AbstractAutoProxyCreator.java:494) - Creating implicit proxy for bean 'searchThread' with 0 common interceptors and 1 specific interceptors
2009-05-08 14:34:37,560 [main] DEBUG org.springframework.aop.framework.JdkDynamicAopProxy.getProxy(JdkDynamicAopProxy.java:113) - Creating JDK dynamic proxy: target source is SingletonTargetSource for target object [com.generic.InnerThread@1080876]
2009-05-08 14:34:37,591 [main] DEBUG com.generic.OuterThread.createInnerThreads(OuterThread.java:141) - $Proxy26@1594a88[h=org.springframework.aop.framework.JdkDynamicAopProxy@1f0cf51]
2009-05-08 14:34:37,591 [main] DEBUG com.generic.OuterThread.createInnerThreads(OuterThread.java:142) - proxy.getClass(): class $Proxy26
2009-05-08 14:34:37,591 [main] DEBUG com.generic.OuterThread.createInnerThreads(OuterThread.java:143) - proxy.getClass().getClassLoader(): sun.misc.Launcher$AppClassLoader@11b86e7
2009-05-08 14:34:37,591 [main] DEBUG com.generic.OuterThread.createInnerThreads(OuterThread.java:144) - proxy.getClass().getDeclaringClass(): null
2009-05-08 14:34:37,591 [main] DEBUG com.generic.OuterThread.createInnerThreads(OuterThread.java:145) - InnerThread.class.getClassLoader(): sun.misc.Launcher$AppClassLoader@11b86e7
2009-05-08 14:34:37,591 [main] ERROR com.generic.OuterThread.run(OuterThread.java:101) - Exception in OuterThread, ending reconciliation.
java.lang.ClassCastException: $Proxy26 cannot be cast to com.generic.InnerThread
    at com.generic.OuterThread.createInnerThreads(OuterThread.java:148)
    at com.generic.OuterThread.run(OuterThread.java:65)
    at com.generic.OuterThread.main(OuterThread.java:170)


Similar questions that do not answer my question

From stackoverflow
  • This is just a guess, but try making an interface InnerThreadInterface, then let InnerThread class extend it.

    After that you should be able to do:

    InnerThreadInterface inner = ctx.getBean("innerThread", InnerThread.class);

    James McMahon : I gave this a try, unfortunately no dice on this one.
  • Once again, after spending hours trying to debug this I find the answer right after posting on StackOverflow.

    A key point that I left out from my question is that InnerThread has a transactional method (sorry thought this was irrelevant). This is the important difference between OuterThread and InnerThread.

    From the Spring documentation:

    Note

    Multiple sections are collapsed into a single unified auto-proxy creator at runtime, which applies the strongest proxy settings that any of the sections (typically from different XML bean definition files) specified. This also applies to the and elements.

    To be clear: using 'proxy-target-class="true"' on , or elements will force the use of CGLIB proxies for all three of them.

    Adding the above to my configuration (based in persistance-context.xml, which you can see loaded above) line seems to fix the problem. However, I think this may be a quick fix workaround as opposed to real solution.

    I think I've got a few deeper issues here, number one being that I find Spring as confusing as expletive deleted. Second I should probably be using Spring's TaskExecutor to kick off my threads. Third my threads should implement Runnable instead of extending Thread (See SO question below).

    See Also

  • I had this issue even though I referenced CGLIB and used the proxy-target-class="true" setting. I determined ehcache:annotation tag was to blame... Removing the following configuration solved this for me. Fortunately, I was able to use hibernate level 2 caching instead of having to use ehcache declarative caching.

    <ehcache:annotations>
     <ehcache:caching id="myCacheModel" cacheName="myCache"/>
     <ehcache:flushing id="myFlushModel" cacheNames="myCache" when="after"/>
    </ehcache:annotations>
    
  • Another way to handle this problem would be to implement an interface and request the interface from spring, the proxy would fully implement the interface, and the cast shouldn't have a problem.

DB2 sp_rename equivalent

Is there a DB2 v9.1 equivalent for sp_rename?

From stackoverflow
  • It depends on the object.

    For tables and indexes, you can use the RENAME sql statement.

    If you want to rename a column, you can use ALTER TABLE ... RENAME COLUMN.

    For other objects, you probably have to drop and recreate it.

    Hope that helps.

    Rashmi Pandit : can i rename an sp or do i have to do a drop - create??
    Michael Sharek : You have to do drop/create...alter procedure doesn't allow rename. http://publib.boulder.ibm.com/infocenter/db2luw/v9/topic/com.ibm.db2.udb.admin.doc/doc/r0008168.htm
    Rashmi Pandit : Thanks Michael. I guess sp_rename in SQLServer would internally do the same. Drop and recreate.

Any ideas why my GetCompanies() function isn't working?

All it does is output 'CHECK' which I put in to make sure it was actually hitting the function... (Proceed() is called first then GetCompanies() after).

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using Mexico.Data;

public partial class admin_tools_Optimus : System.Web.UI.Page
{
    protected int step = 0;
    protected string[] companies = new string[260];
    protected string[,] courses = new string[260,50];
    protected int total = 0;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Proceed(object sender, EventArgs e)
    {
        DataSet getCompanies = SqlHelper.ExecuteDataset(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString, CommandType.StoredProcedure, "Companies_All_Get");

        int counter = 0;

        foreach (DataRow dr in getCompanies.Tables[0].Rows)
        {
            lstData.Items.Add(dr["companyid"].ToString() + ": " + dr["companyname"].ToString());
            companies[counter] = dr["companyid"].ToString();
            counter++;
        }
        lblCurrentData.Text = counter.ToString() + " companies ready, click next to get all company courses.";
        total = counter;
        btnNext.Visible = false;
        btnNext1.Visible = true;
    }


    protected void GetCompanies(object sender, EventArgs e)
    {
        Response.Write("CHECK");
        for (int i = 0; i < total; i++)
        {
            DataSet getBundles = SqlHelper.ExecuteDataset(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString, CommandType.StoredProcedure, "CompanyCourses_ByCompanyID_Get_Sav", new SqlParameter("@companyid", companies[i]));

            int counter = 0;

            foreach (DataRow dr in getBundles.Tables[0].Rows)
            {
                courses[i, counter] = dr["bundleid"].ToString();
                counter++;
            }

            string allID = "";

            allID += courses[i, 0];

            for (int ii = 0; ii < counter; ii++)
            {
                allID += "," + courses[i, ii];
            }
            Response.Write(allID + " <br/>");
        }
    }

}
From stackoverflow
  • Use the debugger. What is total set to before that loop?

  • It looks like you're calling the two methods during different postbacks. If that is the case, then your companies array will be empty because you are not persisting it (in session, viewstate, etc.).

    curtisk : bingo! +1, total is zero in the for loop every time
    shogun : thanks a million!
  • The page is instantiated anew on every postback. The value that you set in the Proceed call only applies to the instance of the object that handles that call. Thus, you post back again and GetCompanies is invoked it gets the initial value of total again -- 0 -- and doesn't think there is any work to do. Try persisting the total number in the Session and retrieve it from there when GetCompanies is called. Make sure to have GetCompanies remove it from the session after it is used so that it doesn't reused without being reset.

  • I might be missing something here, but you call GetCompanies before you set the "total" variable. So the for loop inside GetCompanies won't loop over anything since "total" is still set to 0.

  • Your loop condition within GetCompanies() is met at the start and never executes any loops.

  • there are some things that you should improve in your codding-way-to-do-things, but 1st, there is no need to have Response.Write clauses, a debug with breaking points will tell you exactly what the problem is.

    first, stop protecting your global variables, make them private if you don't want to expose them outside your class.

    The protected keyword is a member access modifier. A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member.

    Do you need to inherit this class? I don't thing so.

Release build not available WPF

I am writing a fairly simple WPF desktop application and under build\configuration manager, release is not an option. The only available options are Debug, New, and Edit.

Anyone have an idea on why this is?

Related: the control library in the same solution does have release as a build option. It is only missing in the wpf app.

From stackoverflow
  • I figured it out.

    To fix i copied the propertygroup tag for release build from the xml in the other project to the project that was missing it.

  • The propertygroup tag thing does fix the problem.

    1) Open your project file in notepad.

    2) Paste this after the "debug" propertygroup tag:

    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
        <DebugType>pdbonly</DebugType>
        <Optimize>true</Optimize>
        <OutputPath>bin\Release\</OutputPath>
        <DefineConstants>TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
    </PropertyGroup>
    

How do you programmatically turn off the Windows XP Print Spooler service in C#

I need to write a small console app (patch) that turns off the print spooler service, does a few things, then starts the print spooler service. I'd like to write this in C#. Can someone point me in the right direction? Thanks in advance!

From stackoverflow
  • net start spooler
    net stop spooler
    

    http://www.tech-recipes.com/rx/864/windows-service-managing-through-command-line/

  • I suspect you use the ServiceController class to control (i.e. to stop and start) the service whose name is spooler.

    MikeW : Patch complete. Thanks. Your input sent me to the right place!
  • You can probably do that using the ServiceController class :

    ServiceController controller = new ServiceController("Spooler");
    controller.Stop();
    ...
    controller.Start();
    
    MikeW : Thanks. This was exactly what I needed to complete the task.

Which data model strategy to use for a relatively simple app?

For a relatively simple app that manipulates objects that store a date/time and some other values? Should I use property lists? archiving? or sqlite?

From stackoverflow
  • Plists are the simplest way to do save data. Use the built in saving methods in the collection classes (NSarray, NSDict, etc.)

  • You might want to take a look at NSUserDefaults.

How can I get a value from a CheckBoxColumn using C# 3.0?

I can get the current selected row in this way:

 private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e){

//Cells[0] cause CheckBoxColumn is in that index (first column)
DataGridViewCheckBoxCell temp = (DataGridViewCheckBoxCell)dgv.Rows[e.RowIndex].Cells[0];
}

So, Now I want to get all of the rows that have been checked by the user:

 foreach (var row_ in DataGridView1.Rows.OfType<DataGridViewRow>().
                                        Select(o => o.Cells.OfType<DataGridViewCheckBoxCell>().
                                         Where(r => r.Value.Equals(true))).FirstOrDefault()){

}

I am getting null reference from the debugger.

What am I doing wrong?

From stackoverflow
  • I suspect you're going about it wrong, and what you actually meant to write is this:

    foreach (var row_ in
        DataGridView1.Rows.OfType<DataGridViewRow>().
        Where(o => o.Cells.OfType<DataGridViewCheckBoxCell>().
        Any(r => r.Value.Equals(true))))
    {
    
    }
    

    But I'm not certain.

    Angel Escobedo : Stills showing null reference.
  • This is the answer, Hopes helps (Thanks mquander for the .Any idea):

            foreach (var _row in dgvpendientepago.Rows.OfType<DataGridViewRow>().
                Where(o => o.Cells.OfType<DataGridViewCheckBoxCell>().
                    Any(r => r.EditedFormattedValue.Equals(true))))
            {
              // do stuff like the following : 
              lst4Pay.Add(new Cobranzaciaseguro
              {
                numeroatencion = Convert.ToInt16(_row.Cells[3].Value),
                estado = 'P'
              });
            }
    

Django -- ForeignKey() uses different column than the row ID?

Hi,

I have an application called Location. Location has Country, State, City, Big_City_Nearby, Longitude, latitude.

I have an application for an item for sell. Item has Title, Link, Description, Price & Location which is a ForeignKey().

Now, if someone wants to see all items for sell in the US, they click on a link (let say http://example.com/United-States/) and the URL will pass in the "United States" as country with state, city and big_city_nearby empty.

#in view.py
class by_location(request, country, state, city, big_city_nearby)
   location = Location.objects.filter(country=country, state=state, city=city, big_city_nearby=big_city_nearby)
   items = Items.objects.filter(location__in=[location.id for loc in location])
   return render_to_response('item/by_location.html', {"items":itmes, "country":countyr, "cit":city, "nearby":big_city_nearby})

There is no problem if they pass in (http://example.com/United_State/California/Oakland) as I can go ahead and list all the items in Oakland without a need to display the location per items found.

The problem is when the select http://example.com/United_States/ or http://example.com/United_States/California/. As I have to now display the location of each items as the items can be from different cities.

So when the template gets the list of items, it only gets the ForeignKey(Location) in each item. I can put in place another loop, where each item is examined and the Location object is fetched and put in a tuple with the item object. But that would be very SQL inefficient. As I would have to hit the database for each item found to get the actual location object.

Is there a way where I can say the ForeignKey is holding the city instead of the Id to the location row in the database.

To make the question simple: Is there a better way of leaving the location out as an application, without a need to provide country, state, city ...etc. in each item row.

Thx,

VN44CA

From stackoverflow
  • Item.objects.filter(...).select_related("location"). This will JOIN against the locations table, so it'll just be one query.

    Even better, Item.objects.filter(location__country=country, location__state=state).select_related("location") and you can omit the Location query entirely.

    Honestly, I'm having trouble figuring out if that's all you need, but that handles the O(N) queries issue.

    VN44CA : You allow people to sell stuff on your site. Person A only sells item A locally in US/CA/Oakland, Person B only sells item B in US/CA/* and person C sells item C in US/*. Now if you don't want item A, B & C contain any information about country, state, city, long & lat as you might have thousands of items and that would take up much space. What you have here works to get the items, but how do you pass the location of each item to the template? How would I get/pass the location objects for each item to the template so I can print the related city where the item is found?
    VN44CA : I missed the JOIN somewhere ...
    AdamKG : Use `.select_related("location")` on your `Item` query, then access `{{ item.location }}` in the template. That's really all there is to it...
    VN44CA : Thank you very much for clearing thing out! It is exactly what I need. Now, how efficient is a join of this sort? Also, If the location was using itself as foreignKey, would a double join be possible? Like: items = Item.objects.filter(location__country=country, location__state=state,location__location__city=city).select_related('Location').select_related('Location')
    VN44CA : What I want is: return all items whose locations points to a common/center location. Location is using itself as foreignKey. So if you select the /us/ca/oakland, simple join works, but if you select /us/ca/oakland/area/ then all cities around oakland (e.g. Alameda, Berkeley .. etc.) that have oakland as foreignKey (big_city_nearby) would be selected. don't know if we have double join or not as I just learned about join today. Thx very much.

Given a pointer to a C++ object, what is the preferred way to call a static member function?

Say I have:

class A {
public:
    static void DoStuff();

    // ... more methods here ...
};

And later on I have a function that wants to call DoStuff:

B::SomeFunction(A* a_ptr) {

Is it better to say:

    a_ptr->DoStuff();
}

Or is the following better even though I have an instance pointer:

    A::DoStuff()
}

This is purely a matter of style, but I'd like to get some informed opinions before I make a decision.

From stackoverflow
  • I think I'd prefer "A::DoStuff()", as it's more clear that a static method is being called.

    jeffamaphone : Though I really like Adam's example, I'll give the check for this subjective question to the crowd favorite.
  • I personally prefer the A::DoStuff() convention because it's immediately clear to anyone reading the code that it's a call to a static member function.

  • It's better to call the static method by its name, not through an object, since it doesn't actually use that object at all. In Java, the same problem exists. A not-too-uncommon problem in Java is the following:

    Thread t = getSomeOtherThread();
    t.sleep(1000);
    

    This compiles fine but is almost always an error -- Thread.sleep() is a static method that causes the current thread to sleep, not the thread being acted on as the code seems to imply.

    jeffamaphone : Good point. I suppose similar errors could arise in C++ frameworks.
    Richard Corden : If the API does have this, then I think this is a major "bug" in the API. Maybe it's because Java doesn't have the concept of free functions that it had to do this - but that isn't necessary in C++. A function should be a member function if it has special access to the object (eg. private constructor).
  • Although I agree that A::DoStuff() is clearer, and it's what I'd write myself, I can see an argument for going via the pointer, which is "suppose the class name changes". If class A becomes class B, then we only need to update the class name in one place (the pointer declaration) instead of two.

    Just a thought...

    Dan Breslau : Also, the static method might someday change to an instance method; I've seen that happen on occasion.
    jeffamaphone : Either change will cause compile time errors. Changing names is a simple matter of search and replace (usually--works fine with names more descriptive than "A" and "B").
  • Generally I do the A::DoStuff(); way instead of a->DoStuff(); because maybe someday the function I'm in won't have that instance pointer anymore due to refactoring. But it's a total style thing that you shouldn't loose any sleep over.

  • Jon Skeet opened my eyes to why you should not call a static method through an instance pointer. His example is in Java, but the concept applies to C++, too:

    Thread t = new Thread(...);
    t.start();
    t.sleep(1000); // Which thread does it look like this will affect?
    

    As I commented when I first read his answer: "Until I read [Jon's post], I considered being able to call static methods through an instance reference a feature. Now I know better."

    In short, call static methods using the class name, not an instance. In my opinion, it's more than a style issue - it can result in misleading, buggy code.

    Richard Corden : This looks like a really contrived example. If you have a sleep member in a Thread class then if that member does *not* affect just that thread then you should shoot the person who wrote the API.
    Richard Corden : Another answer mentions this too - so this must be an issue in Java. However, a flaw in the design of a Java API (because of the lack of free functions) is not necessarily a good argument for something in C++.
    Michael Burr : While the example given was actually Java, it is more-or-less valid C++ as well, given a Thread class with a start() instance method and a static sleep() method. Of course, you'd also either have to get rid of the `new` or make `t` a pointer and change the `t.start()` to `t->start()`, etc. - but those changes are besides the point about calling a static method through an instance.
    Michael Burr : Also, you may want to get out your gun - a Thread class with a static sleep() method seems to be quite common. Even boost::thread has it, and while the boost developers are not perfect, I'd be reluctant to call them worthy of shooting.
  • I've seen many questions in Java where if people called a static method using the syntax of calling an instance method through an object, and the object is actually a subclass of the variable type, people wonder why it doesn't call a static method of the same name in the subclass. The fact that they are calling it through an object makes them think that it is an instance method which can be overridden and it somehow does runtime dynamic lookup using the type of the object.

    But of course the object is never used in the call at all -- only the type of the variable is used at compile-time to decide what class's static method it is. So if you put an object there, it is completely misleading because it makes people think that it is used, when it is not. That's why I favor only calling static methods through the class name -- it is exactly equivalent to calling it through a variable of the class type; but it tells you exactly what is going on, with no useless misleading extra information.