Where to put business logic if I don't have Contexts

Hi, I started a new Phoenix 1.3 application for the first time and I have a question. I’m gonna create a super small web app with memory storage, so as I don’t need a database, I generated the app with --no-ecto parameter.
But now, I’m a bit lost in terms of where to put my business logic. I don’t need to create a schema as what I’m gonna do is to get an input from the user from a web form, make some processing and return the result in an html view. So I think I don’t need to create Schemas nor Contexts. Should I put all the business logic under lib/myApp.ex? or is there something I’m missing?
thanks!

@nelson687: You could use Ecto to validate input data and simply don’t create repos (no database, no migrations etc.). If you have bigger structs of data to send then it’s recommend to declare schema or schema like information to better see how your input/validated data will look.

Adding all code to one file is not a good idea if it’s not 5-min home project (that you will not continue develop in future). At start it could look ok, but there is big chance that you will edit this lots of times, so I recommend to split your logic into modules that their names describes what part of work you are doing (for example data validation, data processing etc.).

Be sure to read tutorials and use tools like credo. We already have lots of tutorials that shows how to style code. I would also recommend to look at code of some libraries/projects and check how well you understand their code. If you don’t understand part/full code then think why you can’t. Maybe there is better idea to organize and/or style code that could help you read specified library/project?

Finally you should know best how well you understand your code (especially after some bigger time like few months when you don’t remember all things so good), so: practice, tutorials, practice, think about it, practice and … did I mention about practice? :077:

2 Likes

Hi Eiji, thanks for your suggestions. And yeah, it’s a home project, a coding challenge that I have to send back to a friend, that’s why I didn’t use ECTO as I wanted to be as light as possible. So I’m still unsure if I should put my business logic under lib/myApp.ex or somewhere else

@nelson687: As said: it depends. Home projects could also be developed in future - there is no need to release it as public package. For example you can send friend sample code and he will be surprised that writing Elixir apps is too easy, so he will want to develop this project himself.

If you have finite things to write and you know that you will not have so much code to write then you can do it in one file. For example I made a small scraping program that parses one page and returns me all wanted links on sub-pages to console - it takes me only 45 lines of code, but I have now two ways: improve this project making it more configurable, so I could scrape more links in similar sites or simply keep it on my hard drive as long as I need it.

In first case I would need to split code on:

  1. module that checks input url (for example domain) and returns implementation module for specified page (or sub-page)
  2. each page I want to support I would like to write code for it in separate file - all that files I would like to group in one directory, so it’s easy to look where I implement scraping data for X site
  3. main code file that uses files described in 1 and 2 point - this code needs to read user input and send result for user

btw. It should be lib/my_app.ex - not lib/myApp.ex. Of course both will work, but we are using snake_case file names as same as variable and function names.

excellent, great explanation. Thanks for your help :slight_smile: