Skip to content

move example code from docs into source code - #2143

Merged
karussell merged 10 commits into
masterfrom
move_doc_to_code
Oct 14, 2020
Merged

karussell merged 10 commits into
masterfrom
move_doc_to_code

Conversation

@karussell

@karussell karussell commented Oct 1, 2020 •

Copy link
Copy Markdown
Member

This should fix #774 via creating a separate 'example' module with sources only for documentation purposes and at the same time it can be used as a start to create a Java project using GraphHopper through the Java library.

There were many incomplete examples (that probably converted into this state over the time and because they are not executable) and this is the main indication that we really need to do this when we want usable example Java code :)

TODOs:

  • I was unsure if we should make it safe for refactoring via assertEquals or easy for beginners to copy and paste via System.out.println. But now I'm sure I need both and will e.g. comment all assertEquals with "// not necessary in real code".
  • include isochrone

@karussell karussell added this to the 3.0 milestone Oct 1, 2020
@michaz

michaz commented Oct 2, 2020 •

Copy link
Copy Markdown
Member

I was unsure if we should make it safe for refactoring via assertEquals or easy for beginners to copy and paste via System.out.println. But now I'm sure I need both and will e.g. comment all assertEquals with "// not necessary in real code".

I would suggest not mixing levels of instruction here, or meta-commenting on what you're doing -- keep it simple, choose one of two:

  1. Write the examples as JUnit tests. Then assume that user knows what a test is and what assert means, and how to write regular code from the example given by the test. Let it speak for itself, don't comment on it. Comment only on what the API is doing.
    Otherwise, you would in principle also have to explain how to move the code from test to main, how to write a main method, etc. -- doesn't make sense. Don't teach Java while documenting a Java API. Different job.
  2. Write the examples as "main" code. Then don't put them in src/test but in src/main, and use Java's assert, not JUnit's, and then just run them from a one-line JUnit test. Even then, no need to apologize, it's fine to have assertions, especially beginners (but really everyone) should use them more. So you could even comment // verify that what we're doing makes sense rather than // not necessary in real code.

@karussell

Copy link
Copy Markdown
Member Author

Write the examples as "main" code. Then don't put them in src/test but in src/main,...
then just run them from a one-line JUnit test.

Thanks, this is an interesting idea. Will think about which one makes more sense and also look into other open source projects how they do it. btw: do you know any with good examples?

@karussell

karussell commented Oct 2, 2020 •

Copy link
Copy Markdown
Member Author

Will move the code into main and use your second approach. Open source projects like dropwizard or optaplanner that use this approach seem to have normal unit testing in place for the example app instead of asserts in the code. Will see if I can make it working this way, otherwise I will use assert like you proposed.

@karussell
karussell merged commit b980a65 into master Oct 14, 2020
@karussell
karussell deleted the move_doc_to_code branch October 14, 2020 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update documentation

2 participants