Tuesday, April 5, 2011

Is there a way to get LaTeX to place figures in the same page as a reference to that figure?

I am using LaTeX and the figure environment.

I am well familiar with the parameters to that environment: [htbp], and I am also generally familiar with the strategies that LaTeX takes, by default, to decide where to put each figure. For example, by placing figures at the top or bottom of the page.

What I am wondering is whether there is a package, macro, or some commands that I can give so that if I have a single-column document and I mostly have a single in-text reference to a figure, that the figure would be more likely to be placed in the same page of the reference?

For example, imagine that I have a long paragraph which in the middle has a \ref{FIG:X}. When rendered, some of the paragraph appears before the page break, and some appears after the page break. I can also place the figure command somewhere before and after the whole paragraph. Is there a way to get it to actually be placed in the same page as the actual reference?

From stackoverflow
  • I solve this problem by always using the [h] option on floats (such as figures) so that they (mostly) go where I place them. Then when I look at the final draft, I adjust the location of the float by moving it in the LaTeX source. Usually that means moving it around the paragraph where it is referenced. Sometimes I need to add a page break at an appropriate spot.

    I've found that the default placement of floats is reasonable in LaTeX, but manual adjustments are almost always needed to get things like this just right. (And sometimes it isn't possible for everything to be perfect when there are lots of floats and footnotes.)

    The manual for the memoir class has some good information about how LaTeX places floats and some advice for manipulating the algorithm.

    Uri : The problem with [h] is that it often ignored, and that it does not take context (such as reference) location into account. I often end up in a situation where placing it either or after the paragraph alwyas ends up putting it not in the same line as the reference...
    Jon Ericson : True. I tend to fiddle quite a bit with the final draft (and often earlier) to get tables and figures where I want them. So I'm not a huge fan of the automatic placement algorithm and [h] is the best way I know to avoid it moving things around. I don't tend to have large documents, however.
    Brent.Longborough : Heartily endorse your mention of 'memoir'
  • I don't want to sound too negative, but there are occasions when what you want is almost impossible without a lot of "artificial" tuning of page breaks.

    If the callout falls naturally near the bottom of a page, and the figure falls on the following page, moving the figure back one page will probably displace the callout forward.

    I would recommend (as far as possible, and depending on the exact size of the figures):

    • Place the figures with [t] (or [h] if you must)
    • Place the figures as near as possible to the "right" place (differs for [t] and [h])
    • Include the figures from separate files with \input, which will make them much easier to move around when you're doing the final tuning

    In my experience, this is a big eater-up of non-available time (:-)


    In reply to Jon's comment, I think this is an inherently difficult problem, because the LaTeX guys are no slouches. You may like to read Frank Mittelbach's paper.

    Uri : I agree. Hence I am wondering if there is a way to do this automatically. If I wanted to manually tinker with how my paper is formatted and get horrible results, I'd use Word :)
    Jon Ericson : Good to know we aren't alone. On the other hand, I can't decide if this is an inherently difficult problem or if the float algorithm is poorly designed. My guess is the later. :-(
    Brent.Longborough : @Uri: Yes. I'm not really proposing tinkering, though; just a final pass when the document is ready to publish to tune up the relative positions of figures and callouts. You can do this with LaTeX and expect good results, while Word will break you.
    Jon Ericson : @Brent.Longborough: I withdraw my disparaging remark. ;-)
    Brent.Longborough : @Jon: ~"disparaging" - don't be too hard on yourself. In a world where 99% of stuff is rubbish, it was a reasonable, though perhaps not fully informed, guess.
  • You can always add the "!" into your float-options. This way, latex tries really hard to place the figure where you want it (I mostly use [h!tb]), stretching the normal rules of type-setting.

    I have found another solution:
    Use the float-package. This way you can place the figures where you want them to be.

  • I have some useful comments. Because I had similar problem with location of figures. I used package "wrapfig" that allows to make figures wrapped by text. Something like ... \usepackage{wrapfig} \usepackage{graphicx} ... \begin{wrapfigure}{r}{53pt} \includegraphics[width=53pt]{cone.pdf} \end{wrapfigure}

    In options {r} means to put figure from right side. {l} can be use for left side.

  • One way I found that helps with this is to use \include{file_with_tex_figure_commands}

    (not input)

[.NET] Using Thread.VolatileWrite() with array parameters

I want to use Thread.VolatileWrite() (or an equivalent function) to change the value of a T[] field, so that the updated value is immediately visible to all other threads. However, the method does not provide a generic version, and I'm not able to use the Object overload since it takes a ref parameter.

Is there an alternative? Would Interlocked.Exchange<T> do the job? Is there a better way to achieve what I want to do?

From stackoverflow
  • You can maybe use the volatile modifier. But check the documentation to see that the limitation don't apply to your case. And there is probably a performance penalty for using it.

    Update: About using Interlocked. Notice that the documentation doesn't state that the changed value is visible to all threads. So it is probably not the same. But if you are in a situation that this is important. You must investigate further all the concurrency disaster possibilities.

    Hosam Aly : Thank you. I am already using the volatile modifier, and I was hoping I could avoid its penalty by calling VolatileWrite explicitly. (Please see my comment on Marc's answer.) Thanks.
  • VolatileWrite is only useful if you also use VolatileRead; the volatile modifier is easier to use, but I wonder if it wouldn't be simpler to use synchronization - perhaps a ReaderWriterLockSlim (since it sounds like you have lots of readers).

    Interlocked.Exchange<T> simply performs the get and set as an atomic operation; it doesn't (AFAIK) make any volatility claims (indeed, the compiler even tells you that a volatile field won't be treated as volatile when used as a ref argument).


    Edit to clarify "it doesn't (AFAIK) make any volatility claims" - the behaviour of Interlocked is predictable, and you should see updates immediately on other thread as long as they also use Interlocked; my point is that if one thread uses Interlocked, and another relies on volatile (to talk to the same field), I don't know of any guarantees. Stick to one or the other for all access.

    Hosam Aly : So you mean that, unless I use `VolatileRead` too, other threads may still have a cached value of the field?
    Marc Gravell : Exactly. Hence the `volatile` modifier to make this simpler.
    Hosam Aly : I was trying to use it in favor of using a volatile field, since that field is accessed very frequently, but only written to very, very few times (e.g. accessed 3n times, while written log(n) times only). So I thought I could introduce VolatileWrite only in write cases...
    Jon Skeet : @Hosam: The problem is that unless the reading threads know to make sure they don't get a cached version, they could just not both looking at the newly published version. In fact, I believe that .NET (i.e. the MS CLI implementation) makes all writes volatile anyway - it's reads that need extra care.
    Hosam Aly : Thank you Mark and Jon for your informative replies. :)

Advice for Getting By in the Delphi IDE without VI

As a long time VIM user, it pains me to have to use the Delphi IDE. At least with Visual Studio there's ViEmu which works well.

So, do you have any advice for getting by in the Delphi IDE when you're so used to VI?

Any settings that you'd recommend to make things a little more familiar?

Any specific keyboard shortcuts that can save time and mouse clicks?

From stackoverflow
  • There are a lot of keyboard shortcuts in Delphi. Some of them came from the turbo pascal time (and they came from the ancient wordstar period).

    Just beware that there are several modes that you can use and each has its own settings:

    • default
    • IDE classic
    • BRIEF emulation
    • Epsilon emulation
    • Visual studio (tm) emulation
    • Visual basic (tm) emulation
    • New IDE emacs
    • New IDE classic

    Unfortunately It looks like these are hard coded so it is not possible to change it.

    The help has a list although it is not complete.

    Rob Kennedy : But is any of them even remotely like Vi?
    mghie : The point is that VIM has modes where keystrokes don't even make it into the edited text, they are just commands. Like "15dd" to delete fifteen lines (not sure about the proper incantation, though). Somehow I doubt the editor DLL of Delphi can be convinced to do that.
  • Here's my favourites.

    • CTRL-SHIFT-I to indent a block
    • CTRL-SHIFT-U to indent a block
    • CTRL-/ to comment or uncomment a block
    • CTRL-E for "search-as you type"

    GExperts for a bunch of useful things, including

    • SHIFT-ALT-R to reverse a statement: a:=b; => b:=a;

    And of course, Code Templates

  • I have come to love the refactoring-shortcuts in Delphi 2007:

    CTRL+SHIFT+V - Declare variable
    CTRL+SHIFT+E - Rename identifier

    I also often use keyboardrecording

    CTRL+SHIFT+R - Start/Stop record CTRL+SHIFT+P - Play

    My favorite GExperts-shortcut was

    CTRL+ALT+Z - Align lines

    I have always wanted a more powerful 'Align lines' function, and fore some reason I have stoped using GExperts al thogether :-/

    I guess the bookmark-shortcuts are kind of obvious

    CTRL+SHIFT+1 - Set bookmark 1
    CTRL+1 - Goto bookmark 1

    Before I switched to Delphi 2007, I used Castalia (never upgraded, don't know why...) It extends bookmarks with a bookmark-stack, where you drop bookmarks and collect them with ESC. I liked that one.

  • See also question Hidden features of Delphi. Many useful shortcuts were mentioned there.

  • AFAIK there is no single one of the various edit modes in the Delphi IDE that implements the distinction between command mode and edit mode that VI(M) has. In that light I think it would be quite difficult to write a vi edit mode for the IDE, but I'm no expert.

    Having said that I need to go a little off-topic - why do you "have to use the Delphi IDE" for your editing? It does of course depend on the amount of form editing you have to do - editing Delphi forms (DFM files) in a text editor is possible, creating them in a text editor is not a good idea if you want to keep your sanity - but if you edit source code whole day you absolutely don't have to use the Delphi IDE. You can create a Makefile for your project, or use the MSBuild command line facility that the newer Delphi versions have, and do all your editing in VIM. You can even edit source code in VIM and switch to Delphi for compilation, as the IDE will detect that open files have changed outside the IDE and ask you whether to reload them, and files that not opened will be recompiled without any question when you build the project. This may even be the best compromise when you do work with forms.

    Trying to have most of the source code in normal source files outside the forms of your project can even help you to prevent the kind of tight coupling between UI code and "business rules" that one sees so often in Delphi projects.

  • Learning the keyboard shortcuts in Delphi will save you a huge amount of time. Especially getting to know the refactoring tools, code completion, code templates, and so on... There is a nice list of keyboard shortcuts available delphi.wikia.com. I also did a video awhile back on 100 IDE Tips and Hints, which should get you started.

double dimensional array value from php

Dear All

I have a PHP file going to write a two-dimensional array in javascript:

<?php

      print "<script language='javascript'>";
      print " extra[0][0]=new Array(1,'Bob',12);"; 
      print " extra[0][1]=new Array(2,'Alice',18);"; 
       ..
      //  need to assign the extra[1][0],extra[1][1] so on.
      print "</script>";    

 ?>

Mu.js:

  var extra=new Array();
  ...

Any suggestion to assign the two-dimensional array from PHP to a javascript variable?

From stackoverflow
  • json_encode is your friend: json_encode in the PHP manual

    
    <script type="text/javascript">
      var jsArray = <?= json_encode($my_array) ?>;
    </script>
    
    
    venkatachalam : I have encoded in PHP, I unable to decode in javascript ,could u please suggest an example?
    wvanbergen : Just build the array in PHP as you want it to look in Javascript, and then pass that array to json_encode.
  • <script type="text/javascript">
     var jsArray = <?php json_encode($my_array); ?>;
    </script>
    

Number of lines in a file in Java

I use huge data files, sometimes I only need to know the number of lines in these files, usually I open them up and read them line by line until I reach the end of the file

I was wondering if there is a smarter way to do that

From stackoverflow
  • On Unix-based systems, use the wc command on the command-line.

    IainMH : wc -l for the line count..
    Paul : @IainmH, your second suggestion just counts the number of entries in the current directory. Not what was intended? (or asked for by the OP)
    PhiLho : @IainMH: that's what wc does anyway (reading the file, counting line-ending).
    IainMH : @PhiLho You'd have to use the -l switch to count the lines. (Don't you? - it's been a while)
    IainMH : @Paul - you are of course 100% right. My only defence is that I posted that before my coffee. I'm as sharp as a button now. :D
    Jason S : You can get wc.exe for Win32 systems: see http://unxutils.sourceforge.net/
  • Only way to know how many lines there are in file is to count them. You can of course create a metric from your data giving you an average length of one line and then get the file size and divide that with avg. length but that won't be accurate.

    Esko : Interesting downvote, no matter what command line tool you're using they all DO THE SAME THING anyway, only internally. There's no magic way to figure out the number of lines, they have to be counted by hand. Sure it can be saved as metadata but that's a whole another story...
    Richie_W : +1 to make you feel better.
  • This is the fastest version I have found so far, about 6 times faster than readLines. On a 150MB log file this takes 0.35 seconds, versus 2.40 seconds when using readLines(). Just for fun, linux' wc -l command takes 0.15 seconds.

    public int count(String filename) throws IOException {
        InputStream is = new BufferedInputStream(new FileInputStream(filename));
        byte[] c = new byte[1024];
        int count = 0;
        int readChars = 0;
        while ((readChars = is.read(c)) != -1) {
            for (int i = 0; i < readChars; ++i) {
                if (c[i] == '\n')
                    ++count;
            }
        }
        return count;
    }
    
    martinus : you were right david, I thought the JVM would be good enough for this... I have updated the code, this one is faster.
    wds : BufferedInputStream should be doing the buffering for you, so I don't see how using an intermediate byte[] array will make it any faster. You're unlikely to do much better than using readLine() repeatedly anyway (since that will be optimized towards by the API).
    martinus : Ive benchmarked it with and without the buffered inputstream, and it is afaster when using it.
    Mark : Its neat, than you so much
    bendin : You're going to close that InputStream when you're done with it, aren't you?
    Peter Lawrey : If buffering helped it would because BufferedInputStream buffers 8K by default. Increase your byte[] to this size or larger and you can drop the BufferedInputStream. e.g. try 1024*1024 bytes.
  • If you don't have any index structures, you'll not get around the reading of the complete file. But you can optimize it by avoiding to read it line by line and use a regex to match all line terminators.

    willcodejavaforfood : Sounds like a neat idea. Anyone tried it and has a regexp for it?
    PhiLho : I doubt it is such a good idea: it will need to read the whole file at once (martinus avoids this) and regexes are overkill (and slower) for such usage (simple search of fixed char(s)).
    David Schmitt : @will: what about /\n/ ? @PhiLo: good point.
  • The answer with the method count() above gave me line miscounts if a file didn't have a newline at the end of the file - it failed to count the last line in the file.

    This method works better for me:

    public int countLines(String filename) throws IOException {
        LineNumberReader reader  = new LineNumberReader(new FileReader(filename));
    int cnt = 0;
    String lineRead = "";
    while ((lineRead = reader.readLine()) != null) {}
    
    cnt = reader.getLineNumber(); 
    reader.close();
    return cnt;
    }
    

Oracle: Is there a simple way to say "if null keep the current value" in merge/update statements?

I have a rather weak understanding of any of oracle's more advanced functionality but this should I think be possible.

Say I have a table with the following schema:

MyTable
  Id INTEGER,
  Col1 VARCHAR2(100),
  Col2 VARCHAR2(100)

I would like to write an sproc with the following

PROCEDURE InsertOrUpdateMyTable(p_id in integer, p_col1 in varcahr2, p_col2 in varchar2)

Which, in the case of an update will, if the value in p_col1, p_col2 is null will not overwrite Col1, Col2 respectively

So If I have a record:

id=123, Col1='ABC', Col2='DEF'

exec InsertOrUpdateMyTable(123, 'XYZ', '098'); --results in id=123, Col1='XYZ', Col2='098'
exec InsertOrUpdateMyTable(123, NULL, '098');  --results in id=123, Col1='ABC', Col2='098'
exec InsertOrUpdateMyTable(123, NULL, NULL);   --results in id=123, Col1='ABC', Col2='DEF'

Is there any simple way of doing this without having multiple SQL statements?

I am thinking there might be a way to do this with the Merge statement though I am only mildly familiar with it.


EDIT: Cade Roux bellow suggests using COALESCE which works great! Here are some examples of using the coalesce kewyord. And here is the solution for my problem:

MERGE INTO MyTable mt
    USING (SELECT 1 FROM   DUAL) a
    ON (mt.ID = p_id)
    WHEN MATCHED THEN
        UPDATE
           SET mt.Col1 = coalesce(p_col1, mt.Col1), mt.Col2 = coalesce(p_col2, mt.Col2)
    WHEN NOT MATCHED THEN
        INSERT (ID, Col1, Col2)
        VALUES (p_id, p_col1, p_col2);
From stackoverflow
  • Using MERGE and COALESCE? Try this link for an example

    with

    SET a.Col1 = COALESCE(incoming.Col1, a.Col1)
        ,a.Col2 = COALESCE(incoming.Col2, a.Col2)
    
    George Mauer : How do I do this?! Please tell me.
    George Mauer : Ah, thanks, let me give this a shot
    George Mauer : Wondefully elegant
    Nick Pierpoint : Shouldn't this be the other way around? COALESCE will return the first non-null value so in this case once a value is set to something it will never be set to anything else. I think the nvl statement is more precise in this context.
    Cade Roux : Yes, I will correct my code to reverse the parameters. NVL() is the same as COALESCE() for two values (at least in SQL Server ISNULL() is the same as COALESCE()).
  • Change the call or the update statement to use

    nvl(newValue, oldValue)
    

    for the new field value.

    George Mauer : But then I have to get the oldValue out in another SQL statement, unless you know something I don't

Does this Entity Repository Service example fit into Domain-Driven Design?

I would like to know if you find the following pattern meaningful in domain driven design.

The domain layer consists of model and repository. The application layer consists of services that handles queries from the user interface, or from controllers in the Model-View-Controller pattern.

Details of the structure:

// Assembly Model:
public class Phrase
{
    public int PhraseId { get; private set; }
    public string PhraseText { get; private set; }

    public Phrase(string phraseText) { this.PhraseText = phraseText; }

    public void SetId(int phraseId) { this.PhraseId = phraseId; }
}

// Assembly Repository (references assembly Model):
public interface IPhraseRepository
{
    Phrase SavePhrase(Phrase phrase);
    Phrase GetPhrase(int phraseId);
}

// Assembly Services (references assemblies Model and Repository):
public class PhraseService
{
    private IPhraseRepository _phraseRepository;
    public PhraseService(IPhraseRepository phraseRepository)
    {
        _phraseRepository = phraseRepository;
    }
    public Phrase SavePhrase(string phraseText)
    {
        Phrase phrase = _phraseRepository.SavePhrase(new Phrase(phraseText));
        // doing other things like sending mail, logging, etc.
        // ...
        return Phrase;
    }
}

Particularly, would it make sense to move the method into the Phrase entity class? In that case, how would that be called?

EDIT:

The example above has been modified after the answer from moffdub and the comment from Adeel Ansari. The changes are highlighted.

I would like to ask about the added IPhraseRepository.GetPhrase(phraseId) and how you would include that?

From stackoverflow
  • The repository should take in a Phrase, not a string. I'm also not sure why the SavePhrase method returns a Phrase. I tend to make such methods void methods.

    Also, be wary of making every property in your domain model have public getters and setters. That can lead you to an anemic domain model.

    Adeel Ansari : Think you don't have an id before persisting the entity. You must need the entity back, in that case. Think of auto-number type in the database, or sequence.
    Ole Lynge : @moffdub and @Adeel Ansari : Thanks for both your replies. I learned something, I think, and will update with a suggested improvement.
    Ole Lynge : How do I serialize with private setters? See my question about that here: http://stackoverflow.com/questions/455884/conflict-between-avoiding-anemic-model-and-allowing-serialization.
  • Just some thoughts:

    SetId(int phraseId) should not be public

    Phrase could implement IPhrase (or IPhraseAggregate) which would not expose SetId(..)

    SavePhrase(Phrase phrase) could (should?) return void if the reference to the phrase entity stays "valid" after saving:

    public void SavePhrase(string phraseText)
    {
        Phrase phrase = new Phrase(phraseText); // NOTE: keep a reference to phrase
        this._phraseRepository.SavePhrase(phrase); // NOTE: returns void
    
        return phrase; // NOTE: assume the repository sets the phrase.PhraseId
    }
    

SQL nvl equivalent - without if/case statements & isnull & coalesce

Are there any nvl() equivalent functions in SQL?

Or something close enough to be used in the same way in certain scenarios?


UPDATE:
no if statements
no case statements
no isnull
no coalesce

select nvl (purge_date,"SODIUFOSDIUFSDOIFUDSF") from id_rec where id=36581;


(expression)

SODIUFOSDIUFSDOIFUDSF

1 row(s) retrieved.

select isnull (purge_date,"SODIUFOSDIUFSDOIFUDSF") from id_rec where id=36581;

  674: Routine (isnull) can not be resolved.
Error in line 1
Near character position 8

select coalesce (purge_date,"SODIUFOSDIUFSDOIFUDSF") from id_rec where id=36581;

  674: Routine (coalesce) can not be resolved.
Error in line 1
Near character position 8

select decode(purge_date, NULL, "01/01/2009", purge_date) from id_rec where id=74115;

  800: Corresponding types must be compatible in CASE expression.
Error in line 1
Near character position 57
From stackoverflow
  • ISNULL (for a single replace)

    or

    COALESCE (Returns the first nonnull expression among its arguments.)

    CheeseConQueso : isnull & coalesce are both undefined routines in whatever version im using....
    BradC : What version are you using?
  • You seem to be using Informix.

    AFAIK, there is DECODE there:

    DECODE(field, NULL, 'it is null, man', field) should give you same result as NVL(field, 'it is null, man')

    Please post exact name and version of the RDBMS you are using.

    CheeseConQueso : Yea, old informix.. dont know what version... i got this error from that syntax select decode(purge_date, NULL, "01/01/2009", purge_date) from id_rec where id=74115; 800: Corresponding types must be compatible in CASE expression. Error in line 1 Near character position 57
    CheeseConQueso : ....but it works in other situations thanks
  • SQL Server: IsNull or COALESCE http://msdn.microsoft.com/en-us/library/ms184325.aspx

    Sybase: isnull function http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.ase_15.0.blocks/html/blocks/blocks162.htm

    Postgres: I couldn't find one though haven't fully checked. Suggests to select where IS NULL and build from here http://archives.postgresql.org/pgsql-sql/1998-06/msg00142.php

    DB2 - COALESCE http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.udb.doc/admin/r0000780.htm

  • The problem with your DECODE statement that is generating the 800 error is simple. '01/01/2009' is being treated as a string, and it's actually the 4th argument that generates the error.

    Appreciate that the input and output of a DECODE statement can be different data-types, so the engine requires you to be more explicit in this case. (Do you want purge_date cast as a string or the string '01/01/2009', or the string argument parsed as a date or the original date? There's no way for the engine to know.

    Try this:

    SELECT DECODE(purge_date, NULL, '01/01/2009'::DATE, purge_date)
    

    You could also write that 3rd argument as:

        DATE('01/01/2009')
        MDY(1,1,2009)
    

    depending on version and personal preference.

How do I iterate over/dereference an array of subroutine refs in Perl?

I’m trying to figure out how to iterate over an array of subroutine refs.

What’s wrong with this syntax?

use strict;
use warnings;

sub yell { print "Ahh!\n"; }
sub kick { print "Boot!\n"; }
sub scream { print "Eeek!\n"; }

my @routines = (\&yell, \&kick, \&scream);
foreach my $routine_ref (@routines) {
  my &routine = &{$routine_ref};
  &routine;
}

Thanks in advance!

From stackoverflow
  • Try this:

    use strict;
    use warnings;
    
    sub yell { print "Ahh!\n"; }
    sub kick { print "Boot!\n"; }
    sub scream { print "Eeek!\n"; }
    
    my @routines = (\&yell, \&kick, \&scream);
    foreach my $routine_ref (@routines) {
      &$routine_ref ();
    }
    
    Axeman : &$routine_ref calls it. The parens do nothing.
  • foreach my $routine_ref (@routines) {
            $routine_ref->();
    }
    
  • In your foreach loop, the following is a syntax error:

    my &routine;
    

    Your variable $routine_ref already has a reference to the subroutine, so all you need to do at that point is call it:

    for my $routine_ref (@routines) {
        &{$routine_ref};
    }
    

    As always with Perl, "There's More Than One Way to Do It." For example, if any of those subroutines took parameters, you could pass them inside parenthesis like this:

    for my $routine_ref (@routines) {
      $routine_ref->();
    }
    

    Also note that I've used for instead of foreach, which is a best pratice put forth by Damian Conway in Perl Best Practices.

    Kent Fredric : I've always preferred the latter example, its less-ambiguous in behaviour to me. ( +1 )
    brian d foy : You probably don't want to dereference it with just the &. Without the parens, that uses the current value of @_ as the implicit argument list, and that's usually never what you mean to do.
    j_random_hacker : Good point Brian.

Proper unit testing: testing a method with no [accessible] state or return value

Hi all,

I'm somewhat new to unit testing. One thing (for now) about proper testing confuses me.

For instance, how do you test a main method if it has no state and only console output? Like this, where myServer methods & state are private?

 public static void main(String[] args)
 {
     Server myServer = new Server()
     if(myServer.start())
          System.out.println("started");
     else
          System.out.println("failed"); 
 }

I don't want to have to change my code and expose my Server methods & state to make them public.

Note, I'm not asking how to test myServer.start(), I'm asking how to test main() itself.

Please let me know.

Thanks guys, jbu

From stackoverflow
  • You (generally) don't unit test main() methods. In your case, you unit test the Server class (and probably others) ie testing it's public interface.

    jbu : ok, but my Server Class only has the public constructor, and all of its methods are private since the Server takes care of itself and its methods aren't called by any outside classes. So then, would I only need to test the constructor? But if I do, there's no state to test, it's all private.
    cletus : If your Server is just a black box with a public constructor then it's nothing more than a main() method in another form. Surely your Server class ses other classes that do have public interfaces? All your code isn't in one class is it? Rethink your black box Server.
    S.Lott : "But if I do, there's no state to test, it's all private" is basically a poor, untestable design. Design for testability is hard. Everything private is a bad choice.
    Andreas Huber : @S. Lott: "... Everything private is a bad choice.". I disagree: Most of the code I write is private and yet I'm usually able to get very good code coverage by only writing tests against the public interface.
    cletus : @Andreas: the fact that you have a public interface means you're agreeing with S.Lott (and me for that matter). He's saying "everything private is bad" not "anything private is bad".
    Andreas Huber : @cletus: If I interpret the comments by S.Lott *only* in the very narrow context of this question, then yes, I would agree. However, IMO his last two sentences have a very universal tone to them, which seems to go beyond this question.
  • If you would like to have this case - and many others - cleared up, may I suggest the Pragmatic Unit Testing series?

  • Generally speaking, main() is not a "unit", and would be difficult to devise a unit test for. By convention, main() should return an int to indicate error state. Perhaps this is all you need?

  • Since your method has information flow only in one direction - to the collaborator objects (in this example: the Server object), and doesn't otherwise return values or throw exceptions, you could supply a mock collaborator object (a mock Server in your example) that would have configured expectations about the method calls and their parameters received from your main() method.

    You can use one of several mock object frameworks for that, e.g. Easymock.

    During unit testing, the mock object would throw a proper exception when calls made on it by the main() method wouldn't match the configured expectations.

    Of course, in order to supply a mock object, you'd have to modify the method a bit with regards to object instantiation - make the "Server" an interface, not a concrete class, and use a framework (like Spring) instead of manually instantiating your Server object, e.g. instead of:

    Server myServer = new Server();
    

    you'd have this:

    Server myServer = applicationContext.getBean("server");
    

    You'd create separate Spring application context configurations for production application and for unit tests, and the difference would be that application context for production would be configured using an XML file, while for unit tests would be programmatically created and populated with mock objects from your test classes' setUp() methods.

  • wrap the code in a non-main method and test that ;-)

    seriously, what everyone else said

    but if you must...make a unit test run the console app in a DOS shell (see the Process object) with STDOUT redirected to a file, then read the contents of the output file to see if it says "started" or "failed", whichever you expect

  • Main methods are supposed to be very thin and just get the ball rolling. Hence generally not tested. e.g. a .net Winforms app Main method may contain

    static void Main()
    {
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      Application.Run(new MainWindow());
    }
    

    If your main method is doing a lot of stuff, consider extracting a method (to a new class if need be). e.g. as shown below... (don't have a JavaIDE handy.. the foll .net code should be easy to translate)

    static void Main()
    {
      new MainApp().Run();
    }
    

    Now if Run can be tested.. Main too is covered. Lets look at MainApp. Refactored the 2 tasks of starting the server and logging the result as 2 methods.

    public class MainApp
    {
      private Server m_Server;
      public MainApp():this(new Server())
      {}
      public MainApp(Server s)
      {  m_Server = s;      }
    
      public void Run()
      {  Log(LaunchServer());  }
    
      private string LaunchServer()
      {  return (m_Server.start() ? "started" : "failed");        }
    
      protected virtual void Log(string sMessage)
      {  System.Console.WriteLine(sMessage);        }
    }
    

    Now lets look at the test code... I'll use the "subclass and override" trick to cache the result as shown below. (You could also use Mocks.. as per taste)

    public class FakeApp : MainApp
    {
      private string sLastLoggedMessage;
    
      public FakeApp(Server s) : base(s) { }
    
      public string getLastLoggedMessage()
      {  return sLastLoggedMessage;        }
    
      protected override void Log(string sMessage)
      {  sLastLoggedMessage = sMessage;        }
    }
    

    the test is now trivial

    [TestFixture]
    public class TestMainApp
    {
      [Test]
      public void TestRun()
      {
        Server s = new Server();
        FakeApp f = new FakeApp(s);
    
        f.Run();
    
        Assert.AreEqual("started", f.getLastLoggedMessage());
      }
    }
    

    if you can't force Server.start() to succeed or fail as per your desire.. You can create a FakeServer.. subclass and override start() to return fixed values.

    If a method does something, it can be tested. If it does not, it should cease to exist. Refactor it away. HTH

asp.net web hosting for new business

Duplicate: http://stackoverflow.com/questions/3305/asp-net-hosting-options


I am about to launch a new business and want a fast and reliable webhost. For development we were on webhost4life which I do like but the performance doesn't seem all that great. I've heard good things about crystal tech (maybe stackoverflow is biased there :) and discountasp.net. Are there any other good recommendations?

Also, am I right in assuming I'll need at least semi-dedicated hosting to run a business website?

(the website is not a new idea. it will just lists about 50 new online deals a day. so it will show some text and a picture of the deal, that's it. it needs to be asp.net with sql server database)

From stackoverflow
  • Duplicate of ASP.NET Hosting Options.

  • If you qualify check out Microsoft's Bizspark program www.microsoft.com/bizspark. They offer great deals on software, and they have some hosting providers lined up to help startups. These providers pay a lot of money to join the program so they should be motivitated to help you succed.

How relevant is academia to professional programmers?

If you work as a professional programmer, how much does the world of academia directly affect the work you do (I'm ignoring indirect effects such as programming language features that have worked their way into your languages)? Do you ever pull up and read journal papers? Do you listen to talks by academics or communicate with them? Have you had inspirations based on academic projects?

From stackoverflow
  • Totally! The work of academia helps you decide if you want to use one type of algorithm versus another. One type of data structure versus another.

    Just look at Donald Knuth's TAOCP to see a brilliant example of the fruits of academic research!

    HTH

    Hmmm. Hash or linked list? Decisions, decisions. (-:

    Edit: Digesting further your question, look at the awesome ACE (and associated TAO) projects that were borne out of academia.

    Ongoing learning is one of the benefits of our job. Just remember that you're either "green and growing" or "ripe and rotting"! (-:

    aka. As they say in rugby "Use it! Or lose it!" A saying that applies to your brain as well! (-:

    cheers,

    Rob

  • It happens quite a lot actually. Being in academia, I see quite a lot of interest from commercial and governmental organizations. If you are doing anything professionally that is cutting edge or state of the art and you aren't reading journals then you won't know that what you are doing hasn't been done or disproven. Obviously, if you're just building a typical boiler plate CRUD application you will have no use for this sort of endeavor, but contrary to popular belief there are people building software that is not CRUD.

  • It did earlier in my career when I was in a research environment (kind of obvious that it would have an impact there). I am almost tempted to say that it has very little effect now but, on reflection, I think that it does. This is not because I am likely to use any techniques from the cutting edge of computer science but rather because, in order to really satisfy the needs of my users (health promotion specialists) I do have to have a more than passing familiarity with developments in that field.

    So...academia can be important but perhaps not in the ways that you anticipate. I would also say that the more ambitious you are about having an impact as a developer, the more likely it is that you'll find that academia is relevant.

    Rob Wells : @Mark,I'd give you +2 if I could for the second paragraph of your answer! cheers. (-:
  • In recent years there are many academic tools and techniques that make their way into industrial products that you use every day.

    For example, many Eclipse users use Mylyn, which started out as a PhD thesis.

    Many things that you see in Visual Studio are based on academic work of students and researchers collaborating with Microsoft Research and its programming group.

    If you use aspect oriented programming, you use a lot of academic work and papers.

    And if you use IBM Jazz, many components were built in academic collaboration.

    For the average programmer, however, there is no real need to investigate academic sources or publications because he will only use a tool that is a "stable" product, which many early research prototypes are not.

    My own research, for example, probably found things that would help you as a programmer (e.g., how to write and use documentation better), and maybe with some luck you would end up using those techniques one day :)

    Uri : I'd love to know why this was downvoted. I am of the opinion that for most programmers (not necessarily SO people) academic materials are not sufficiently relevant or approachable.
    Rob Wells : @Uri, I did it because I thought the statement "For the average programmer, however, there is no .. need to investigate academic sources" did a disservice to those non-average guys who care about the job they do when they research the best way to do the job. (Quotes elided due to 300 char limit) )-:
    Uri : I would argue that (unfortunately) the average programmer does not spend his time doing research or contributing to stack overflow.
    Uri : Academic work is presented and evaluated in ways that don't answer the question "will it work for me". It may sound snobbish, but reading, understanding, and benefiting from such article is not very practical for most developers. Certain publications, like CACM, are more approachable.
    Rob Wells : "For the average programmer..." In that quote you sum up the problem! (-: Most programmers, who are average, go through the motions clicking on their gui buttons to build things as they program by coincidence, i.e. getting things working without understanding why. No SO contributers there!
    Rob Wells : @Uri, definitely agree with your second point though. Most programmers, incl. me I must admit, find almost all academic papers dry and impenetrable. But when the fruits of academic research are presented in a more accesible form then I am more than happy to use them, e.g. Don Knuth's TAOCP
    Uri : I'm not sure I would classify TAOCP as an academic book even though it was written by an academic. Just like I wouldn't classify textbooks as "academic" in the sense that they are very different from papers. Knuth is one of those few people who have the ability to write really well in both mediums.
  • Depends on what you mean by "professional" programmers. If that means building CRUD apps, then no. Oftentimes, academia stuff like data structures and algorithms are already baked into frameworks. But if you are working on deeper topics, say an OS or a language, then probably.

    By the way, this question is probably more wiki material than a normal SO question.

RSS Feed Stocks

Hi, I didn't realize until today that you can't get an RSS feed for a stock quote. Can someone suggest to me ways I can pull data from a service for a particular stock quote? It should include information about the stock etc. Thanks. I will be working in .NET.

From stackoverflow

What is the rationale behind the behavior of constructor pointer in JavaScript?

Considering the simplistic scenario:

function Base() {}
function Child() {}
Child.prototype = new Base;

I wonder why would an instance of Child have constructor property set to Base and not to Child?

From stackoverflow
  • It's all tied to how inheritance works in JavaScript. Check this link for a detailed explanation (basically, constructor is just another property of the prototype).

    Edit: Also, if you want 'real' prototypical inheritance, you have to use some sort of clone function, eg

    function clone(obj) {
        if(typeof obj !== 'undefined') {
            arguments.callee.prototype = Object(obj);
            return new arguments.callee;
        }
    }
    

    Then, you can do things like this

    function Base() {}
    function Sub() {}
    Sub.prototype = clone(Base.prototype);
    
    var obj = new Sub;
    

    and you'll still get true two times on

    document.writeln(obj instanceof Sub);
    document.writeln(obj instanceof Base);
    

    The difference to your solution is that Base() won't be called and Sub.prototype will only inherit the properties of Base.prototype - and not the ones set in the constructor.

  • function Base() {}
    //Base.prototype.constructor === Base
    function Child() {}
    //Child.prototype.constructor === Child;
    var b = new Base;
    //b.constructor = Base;
    Child.prototype = b;
    //Child.prototype.constructor === Base
    

    basically, any property of the prototype becomes a property of the instance, including "constructor"

    when you reassign the whole prototype property to a new object, you're replacing the old constructor property with the new one.

    if you don't want this to happen, you have to assign properties to the prototype object one by one, instead of assigning the whole prototype property to a new object at once. Or replace the old constructor property afterward.

    Much better idea: don't rely on the constructor property for anything important.

    Christoph : `instanceof` is perfectly safe to use if you know what you're doing: `obj instanceof Func` is the same as `Func.prototype.isPrototypeOf(obj)`; all it does is check the [[Prototype]] chain for a specific object - perfectly reasonable in a language with prototypical inheritance
    Sergey Ilinsky : I would argue on your recommendation not to rely on usage of instanceof. It works perfectly and does exactly what it is supposed to do.
    bobince : +1 on "don't rely on the constructor property", it is not standard/globally available and doesn't work the way it looks like. More: http://stackoverflow.com/questions/402538/convention-for-prototype-inheritance-in-javascript
    Breton : what the, I had the "instanceof" remark in my answer for about 2 minutes, realized on my own that I was going overboard, and edited it out. Now I come back the next day and find comments saying that I'd gone too far? How weird, is there something wrong with stackoverflow? edits not taking effect?

What does this Python message mean?

ho-fe3fdd00-12:~ Sam$ easy_install BeautifulSoup
Traceback (most recent call last):
  File "/usr/bin/easy_install", line 8, in <module>
    load_entry_point('setuptools==0.6c7', 'console_scripts', 'easy_install')()
  File "/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/setuptools/command/easy_install.py", line 1670, in main
    with_ei_usage(lambda:
  File "/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/setuptools/command/easy_install.py", line 1659, in with_ei_usage
    return f()
  File "/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/setuptools/command/easy_install.py", line 1674, in <lambda>
    distclass=DistributionWithoutHelpCommands, **kw
  File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/distutils/core.py", line 125, in setup
    dist.parse_config_files()
  File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/distutils/dist.py", line 373, in parse_config_files
    parser.read(filename)
  File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/ConfigParser.py", line 267, in read
    self._read(fp, filename)
  File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/ConfigParser.py", line 462, in _read
    raise MissingSectionHeaderError(fpname, lineno, line)
ConfigParser.MissingSectionHeaderError: File contains no section headers.
file: /Users/Sam/.pydistutils.cfg, line: 1
'install_lib = ~/Library/Python/$py_version_short/site-packages\n'

The same here as a picture: http://dl.getdropbox.com/u/175564/py.png

I am trying to install beautifulsoup.

The first two lines in ~/.pydistutils.cfg:

install_lib = ~/Library/Python/$py_version_short/site-packages
install_scripts = ~/bin
From stackoverflow
  • The configuration file .pydstutils.cfg has a syntax error.

    Masi : How can I fix this syntax error?
  • Try to add the line at the top of ~/.pydistutils.cfg:

    [easy_install]
    
    Masi : It gives me this error: error: error in /Users/Sam/.pydistutils.cfg: command 'easy_install' has no such option 'install_lib'
    J.F. Sebastian : @Masi: `[default]` might help. Or just rename `.pydistutils.cfg` as @Charles Duffy suggested.
    Masi : Where can I find Charles Duffy's suggestion?
    Masi : What do you mean with this: `[default]` might help?
    J.F. Sebastian : Charles Duffy's suggestion is in comments to your question (click 'comments' under the question).
    J.F. Sebastian : Replace '[easy_install]' by '[default]' (don't include the quote sign "'" in the actual file) at the first line in '~/.pydistutils.cfg'.
    jmanning2k : Just [install] would work...
  • BeautifulSoup is a pure Python module which you can install by grabbing the BeautifulSoup.py file (eg. from inside the standard .tar.gz distribution) and putting it somewhere on your PythonPath - eg. inside /Users/Sam/Library/Python/2.5/site-packages, if the paths mentioned in the error message are accurate.

    No need for fussy and error-prone installers which just overcomplicate the issue.

Big difference in execution time of stored proc between Managment Studio and TableAdapter.

How could a stored procdure run in 10 seconds via Management Studio, but take 15 minutes via a TableAdapter for the same inputs? It is repeatable, meaning I have run it at least three times in each environment, and Management Studio is consistently about 100 times faster.

I'm using .net 2.0 and SQL Server 2000

In SQL Server Management, I'm executing it like this:

EXEC    [dbo].[uspMovesReportByRouteStep]
 @RouteStep = 12000,
 @RangeBegin = N'12/28/08',
 @RangeEnd = N'1/18/9'

In the TableAdapter, I'm using a StoredProcedure CommandType and dbo.uspMovesReportByRouteStep for the CommandText. I'm calling the table adapter from an ASP.NET page, although it times out in 30 seconds if I attempt to "Preview Data" locally too.

It's not practical to provide the stored procedure because it's over 100 lines long with dependencies on a number of other UDFs and views on the same and other databases.

All other stored procedures appear to run in about the same time using either method. How is this possible?

From stackoverflow
  • This is due to 'parameter sniffing' and a cached query plan that is not appropriate for the particular values of the parameters you are calling it with. How does that happen? Well, the first time you call a SP with one set of values, a query plan will be generated, parameterised and cached. If the SP is called again with another set of parameter values that would have resulted in a different query plan, but it uses the cached query plan, then performance can suffer.

    It is often because statistics are out of date. You can determine if that's the case by comparing the Estimated execution plan against the Actual execution plan; if different then statistics are most likely out of date.

    I would first try and get the Database's indexes rebuilt, or at least it's statistics updated (ask your DBA). One way to rebuild the indexes (should work on all versions on SQL Server):

    exec sp_msforeachtable "dbcc dbreindex ('?')"
    

    If it's still a problem, try temporarily adding the statement WITH RECOMPILE to the stored procedure definition. If the problem goes away, then take a look at using OPTIMIZE FOR, described in this blog post.

    recursive : Hilariously, we don't really have a DBA here. Somehow the database keeps on trucking on without any intervention, and has for years. But I have privileges to do pretty much anything on the server, so maybe I'll start poking at this stuff. Thanks for the pointers.
    recursive : WITH RECOMPILE in the sproc definition makes it run faster than ever. I haven't rebuilt the indexes yet. But the WITH RECOMPILE definitely solved the problem.
    Mitch Wheat : Please Note: WITH RECOMPILE will recompile your query each time the query is run (and the compilation is relatively expensive, which is why SQL Server tries to cache them). I only advocate using this temporarily, and would use OPTIMIZE FOR instead.
    recursive : I'll check that out next week. But I'd say it's significantly less expensive than doing nothing, as illustrated by the multiple order of magnitude execution time reduction.