{"action":"created","issue":{"url":"https://api.github.com/repos/codeforscience/learning/issues/1","labels_url":"https://api.github.com/repos/codeforscience/learning/issues/1/labels{/name}","comments_url":"https://api.github.com/repos/codeforscience/learning/issues/1/comments","events_url":"https://api.github.com/repos/codeforscience/learning/issues/1/events","html_url":"https://github.com/codeforscience/learning/issues/1","id":121793618,"number":1,"title":"How to write a science module","user":{"login":"freeman-lab","id":3387500,"avatar_url":"https://avatars.githubusercontent.com/u/3387500?v=3","gravatar_id":"","url":"https://api.github.com/users/freeman-lab","html_url":"https://github.com/freeman-lab","followers_url":"https://api.github.com/users/freeman-lab/followers","following_url":"https://api.github.com/users/freeman-lab/following{/other_user}","gists_url":"https://api.github.com/users/freeman-lab/gists{/gist_id}","starred_url":"https://api.github.com/users/freeman-lab/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/freeman-lab/subscriptions","organizations_url":"https://api.github.com/users/freeman-lab/orgs","repos_url":"https://api.github.com/users/freeman-lab/repos","events_url":"https://api.github.com/users/freeman-lab/events{/privacy}","received_events_url":"https://api.github.com/users/freeman-lab/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-12-11T21:19:37Z","updated_at":"2015-12-12T17:09:16Z","closed_at":null,"body":"The goal here is to collect thoughts on principles for writing modular, composable, and reusable code in science. The idea is heavily influenced by @substack's fantastic [\"How I write modules\"](http://substack.net/how_I_write_modules), and wanting something like it that's focused on science, and maybe one level more general. @blahah recently pointed us to the [this bioinformatics document](https://github.com/pjotrp/bioinformatics) which has a similar spirit.\r\n\r\nTo kick things off, I've listed a few of my own thoughts. This is just my perspective, others should comment on these and/or add their own ideas! My hope is that we can generate a bunch of ideas here and then turn it into a curated document or maybe an interactive tutorial.\r\n\r\n### Defining a problem\r\n\r\nBefore starting anything, it's good to define the problem you want to solve. I think a lot of problems can be categorized along two dimensions: complexity and generality. Complexity reflects the breadth and scope and potential impact of what you're trying to tackle, and generality is the set of domains in which your solution can be applied. \r\n\r\nHere's a little picture of the space\r\n\r\n```\r\n            |                                   \r\n            |    ---         --+  \r\n            |\r\n            |\r\ncomplexity  |\r\n            |\r\n            |    -++         +++        \r\n            |      \r\n      (0,0)  ------------------------\r\n                  generality\r\n```\r\n\r\nIn my experience, a lot of code in science is in the **upper left**: it tries to do something incredibly complex, but for a very specific problem domain. A classic example is setting out to all at once build \"the analysis pipeline for my lab\". This might be tempting because it solves all *your* problems, and keeps it all in one place. But there are many disadvantages with this approach! It will be hard for any other projects or labs to use your solutions (which they should!), and it will be hard for you to maintain or extend your solutions as your science evolves (which it will!).\r\n\r\nAnother common pattern, in the **upper right**, is to take on a project that's big and complex and applies to many domains. These are big \"frameworks\" or \"libraries\", and might include things like \"general-purpose machine learning\" or \"image processing\". Many such libraries exist in science, and some are useful! But there are some important disadvantages to this approach. A monolithic project can be hard to extend or change, especially if some parts change more frequently than others — which is why big projects are often backed by huge teams. Big projects can also be hard to test, because they have many interrelated pieces. And anyone who wants to use or help work on your project needs to deal with the whole thing, even if they just want to use, or improve, a small piece.\r\n\r\nIn the other extreme, the **lower left**, we can design modules that are very simple and have very small domains. This hyper-specificity is sometimes necessary, and if the complexity is low, it'll be easy for you to document and maintain. But without generality, it's unlikely that anyone else will be able to use your module!\r\n\r\nTo me, the best kind of module for science aims at the **lower right**: not too complex and narrow in scope, but otherwise as general as possible. By limiting the complexity, you ensure that it stays manageable and maintainable. But by pushing for generality, and avoiding domain-specific jargon, you ensure that other people can use and compose your modules with theirs, including both inside and outside of science! \r\n\r\nWhen making sure my project isn't getting too complex, I like to ask myself: can I solve this problem in days/weeks or will it take months/years? How hard will it be to modify in the future? When making sure my idea is general, I try to ask myself: can another lab use my solution? Can someone outside of my particular field of science use, and understand, what I've done? \r\n\r\n### Is your problem already solved?\r\n\r\nOnce you've defined your problem, find out if someone's already solved it! This can be hard, especially if you are new to a language. For most languages, there is a website that lets you directly search existing packages: [cran](https://cran.r-project.org/) (for R), [pypi](https://pypi.python.org/pypi) (for Python), [npm](https://www.npmjs.com/) (for node.js), or [pkg.julialang](http://pkg.julialang.org/) (for julia). But curated lists are even more useful. These [`awesome`](https://github.com/sindresorhus/awesome) lists are a good start, but some are still quite general:\r\n\r\n- Python [[1](https://github.com/vinta/awesome-python)]\r\n- Node.js [[1](https://github.com/sindresorhus/awesome-nodejs)] [[2](https://github.com/feross/awesome-mad-science)] [[3](https://github.com/maxogden/maintenance-modules)]\r\n- R [[1](https://github.com/qinwf/awesome-R)]\r\n- Julia [[1](https://github.com/svaksha/Julia.jl)]\r\n- Machine learning [[1](https://github.com/josephmisiti/awesome-machine-learning)]\r\n\r\n***We should assemble a cross-language awesome list of modules for science!***\r\n\r\nIf someone has kind of solved your problem, but not exactly, and made it available as open source, this is a great opportunity to contribute! Maybe with some small changes their solution would work for your problem. Open an issue on their GitHub repository to start a friendly conversation! If the changes are too large and out of scope, that's ok too! You could fork their project and start from there, or start from scratch. Just like in science, it's ok to have a couple different takes on the same problem — we just don't want every single lab reinventing everything from scratch.\r\n\r\n### Think like a user\r\n\r\nOnce you've picked a problem and decided to write some code, don't start implementing your solution quite yet! Instead, write a small example of how someone would **use** your module, to define how people will interface with it.\r\n\r\nPut yourself is the shoes of someone unfamiliar with your approach: what would be an intuitive experience for them? Actually write snippets that **use** the code you want to develop. If you're writing something for data analysis, get some example data and show what you'd do with it. If you're writing a simulation, show how you'd invoke it and what the result would be. If you get stuck, find examples of modules or libraries you like to use, and draw inspiration! When doing machine learning or statistics, for example, I often look at the design and user experience of [scikitlearn](https://github.com/scikit-learn/scikit-learn).\r\n\r\n### What else?\r\n\r\nDo these seem useful? What other topics do people feel are important? What other resources could we provide? Let us know in the comments."},"comment":{"url":"https://api.github.com/repos/codeforscience/learning/issues/comments/164168956","html_url":"https://github.com/codeforscience/learning/issues/1#issuecomment-164168956","issue_url":"https://api.github.com/repos/codeforscience/learning/issues/1","id":164168956,"user":{"login":"Blahah","id":836040,"avatar_url":"https://avatars.githubusercontent.com/u/836040?v=3","gravatar_id":"","url":"https://api.github.com/users/Blahah","html_url":"https://github.com/Blahah","followers_url":"https://api.github.com/users/Blahah/followers","following_url":"https://api.github.com/users/Blahah/following{/other_user}","gists_url":"https://api.github.com/users/Blahah/gists{/gist_id}","starred_url":"https://api.github.com/users/Blahah/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Blahah/subscriptions","organizations_url":"https://api.github.com/users/Blahah/orgs","repos_url":"https://api.github.com/users/Blahah/repos","events_url":"https://api.github.com/users/Blahah/events{/privacy}","received_events_url":"https://api.github.com/users/Blahah/received_events","type":"User","site_admin":false},"created_at":"2015-12-12T17:09:16Z","updated_at":"2015-12-12T17:09:16Z","body":"@olgabot flotilla looks very nice! I've faced the same issue before, and have found that most of the fiddly stuff comes from (1) the infrastructure, (2) redesigning the API, and (3) actively developing multiple interdependent packages at once.\r\n\r\nFor (1), I use a project generator [like yeoman](http://yeoman.io/) to create my personal favourite style of directory structure for the language in question, fill in the required metadata files, create the testing setup etc. Then I just copy over the code/tests from the larger project. In general, automating the tedious tasks becomes more important when you start making many small tools.\r\n\r\nFor (2), I don't really have any special guidance - I just use normal UX design principles and practises - user stories, etc.\r\n\r\nFor (3) I usually symlink in the under-development repos for the dependencies to the place the package manager for the language in question looks for them, or point the manager at a dev branch for the dependencies."}}