CategoryProgramming

Phoenix: It was nice while it lasted

We decided to pull the plug on using Phoenix.

The people we know who are using it are mostly well funded and have the time to learn to use it and find their way around things that are a struggle.

We are not.

I wanted to do something that would have taken me five minutes in Rails and Phoenix just wouldn’t do it. Or, at least, the way to do it wasn’t documented.

I also got burned, and wasted a lot of time, because the Phoenix commands have been renamed to be phx instead of phoenix.

I ended up creating a Phoenix 1.2 app by mistake because the old command wasn’t deleted.

It’s annoying. I like the Elixir language a lot. But it’s back to Ruby on Rails because I don’t have time or the dime.

I think in about a year I might come back to it because it will be a bit more mature.

Notes on my first Ember/Phoenix app

I hit a bit of a problem. I’m writing a quotation system for my company.

I have an entity called EstimateType – it is the driver for the rest of the setup. Basically it’s a name and it’s used to group the pieces of the estimate together, so you have, say, small business, or sole trader, and they each may have the same parts of the quote but the details will be different (for example sole traders are generally taught one to one and we charge a flat fee not per delegate).

I built a prototype front end in Ember and used the mirage library.

Using Mirage I just sent the EstimateTypeId to the app and it worked.

The back end’s in Phoenix and I’m using a library called ja_serializer that’s supposed to support Ember out of the box. Having done some experimenting with hand building stuff that can talk to Ember I think this is a good idea and will save some time.

The code generated by this library puts the parent up away in a different part of the JSON from the main data, in a place called relationships. This would be fine (I suppose) but the ID doesn’t end up getting saved either by the generated controllers, or by the generated change sets (I had to add it in).

I’m really not convinced this is right.

10 Apr

The generator doesn’t do parent child properly. It essentially generates the same set of models, tests and controllers that you would get if there were no parent. This is a bit useless and is what got me confused.

I added in some more methods to the tests that create the parent entity and then put it into the queries and structs used by the wrapper methods in the Estimates module (which is the main one for this part of the app).

I’m still a bit meh about having to put things into a module for different parts of the app, which I think came in with 1.3. It’s nice, but often those decisions at the beginning of a development or design run will seem not quite right, and then you get into the problem of asking yourself if it’s worth moving things around. I’d far rather have the problem of deciding if it was worth pushing things into domains and/or namespaces because my app had become overcomplex. It feels like adding an extra layer of indirection for its own sake, and I’ve had enough of that from the days I used to write lots of Java.

Now I have a set of tests that run, and controllers that do parent child correctly.

I did get somewhat hampered where my CI system was failing and not deploying when the tests were running locally. Have since worked out that this was because

MIX_ENV=test mix ecto.reset

Has running the seeds built into the aliases. I’ve since added these aliases to my mix.exs file:

defp aliases do
 [
 "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
 "ecto.setup-noseed": ["ecto.create", "ecto.migrate"],
 "ecto.reset-test": ["ecto.drop", "ecto.setup-noseed"],
 "test": ["ecto.create --quiet", "ecto.migrate", "test"],
 ]
end

And now I do ecto.reset-test if I want to trash the test db. I still haven’t worked out how to tell mix to always run this with the test environment, but not worrying about that now.

I’ve also added

 {:mix_test_watch, "~> 0.5", only: :test},
 {:ex_unit_notifier, "~> 0.1", only: :test},

To my deps, so that I can run the equivalent of guard. Test watch autosets the environment to test but I added only: test because I didn’t want the dep in my production setup. It does mean I need to put MIX_ENV=test onto the command line or it won’t compile and run, but it’s no great hardship.

Later the same day

I must have used the wrong generator commands because this now seems to at least attempt to create the parent records

mix ja_serializer.gen.phx_api Estimates Estimate estimates company_name:string aspiration:string prepared_for:string email:string body:map estimate_type_id:references:estimate_types

The estimates tests now contain

estimate_type = Repo.insert!(%RosieEstimatesServer.Estimates.EstimateType{})

In the controller tests. The tests are still all busted, but at least there’s a starter for 10 there now where there wasn’t before. I still had to set up an alias for Repo, though.

And another thing

The autogenerated change sets don’t have the parent id in them – maybe they’re supposed to be used differently – but in the absence of any decent examples it’s a bit hard to get to the bottom of.

In all cases I’ve had to add estimate_type_id to the cast and validate_required clauses in the model files.

In addition

|> foreign_key_constraint(:estimate_type_id)

Wasn’t put in automatically, which seems a bit weird.

Meh

In order to get errors to be returned in a format Ember likes I needed to change views/changeset_view.ex so that it returned the errors in a compatible list

def render("error.json", %{changeset: changeset}) do
  %{errors: errors_to_json_format(translate_errors(changeset))}
 end

defp errors_to_json_format(errors) do
  errors |> Enum.map(fn {k, v} -> %{detail: List.first(v), source: %{ pointer: "data/attributes/#{k}"}} end)
end

As in the old format isn’t supported any more. This code needs a bit more refactoring but right now it works. Thanks to this guy for the tip.

Also Ember pluralises the entity name so the controller methods needed to be changed

- def create(conn, %{"data" => _data = %{"type" => "estimate", "attributes" => estimate_params}}) do
+ def create(conn, %{"data" => _data = %{"type" => "estimates", "attributes" => estimate_params}}) d

As in, pluralise the type.

Happy days.

And …

import { underscore } from '@ember/string';
...
keyForAttribute(attr) {
 return underscore(attr);
 }

In the serializer config – because elixir inbound wants underscores and I lost patience with JSON API sez X pedantry 🙂

Tumbleweed Interview Candidates

In my present role helping a team become more agile I was asked to help with some interviews. We must have talked to about ten people. The profile is relatively unusual: Object-oriented PHP with MVC and some Oracle PL/SQL. Unusual but a lot of people claim to have at least some of it.

I’ve helped conduct at least two interviews where you ask a straight question related to a claim on a CV e.g. claims about knowing Object-oriented design patterns, I get what I’ve come to call the tumbleweed response. As in what happens when someone makes an unfunny joke and there is silence. The idiom comes from the cowboy movie where the wind blows across the silent plains and makes the tumbleweed roll by; there is nothing there! (Reeves and Mortimer fans will know exactly what I mean).

So, if you are going to be interviewed by me, remember the following:

If you claim to have designed databases with hundreds of tables you should be able to explain what foreign keys and lookup tables are. Third normal form? It’s a dying art. Look it up or don’t make the claim.

If you claim to know PL/SQL then you know the difference between implicit & explicit cursors, probably what ref cursors are and what in, out and nocopy mean and why you would use them. Bonus question – what are PL/SQL tables (hint: nothing to do with database tables, it’s a language construct, so don’t start talking about database tables – it means you don’t have a clue).

SQL: inner and outer joins, foreign keys etc. Why as well as what.

If you know J2EE or Java beyond having attended a one-day course tell me what might go wrong with singletons (have a read up about serialisation)? How and why do you implement an equals method (just look it up)? Bonus question – if you have read about trying to create enumerated types in Bloch’s Effective Java are there any problems with it? Double bonus – tell me about synchronisation and the actual order statements can be executed when optimised that breaks it. It’s a feature. I’m a bastard question: why have’t you read Effective Java? Do you know what POJO is, and why does it have so much meaty goodness? (See Bootnote – some of this stuff has changed).

If you claim to be well-versed in object-oriented techniques you sure as christmas is coming need to be able to tell me the difference between has-a and is-a relationships, and why they are needed. Plus the usual stuff about abstract classes, interfaces and so on. Maybe, as a bonus question, why dynamic languages don’t need interface or abstract – or do they? I like people with opinions.

Ruby – what is duck typing? Can you explain what method_missing does? What does yield do? What’s the difference between a string and a symbol? How do you pass a block to a function – why would you? What’s a mixin and why do they taste so nice and chocolatey? Bonus: Why is the splat operator so handy?

Rails –  how does an @ variable set in the controller appear in the view? What tools to you use to test model/view/controller code separately and together? Tell me why fat model, thin controller is a good guideline. (This question also works for PHP/J2EE and whatever framework you want).

(I will think up more RoR questions – readers feel free to chip in and I will add them).

Agile: What does YAGNI mean? What does PIE mean? What is TDD? Then, of course, why? Bonus: Demeter/Tight and loose coupling/…

Patterns: Describe MVC (why as well as what). Do you know what the conductor pattern is? If you claim to know patterns such as Factory or Singleton, then expect to be asked “what does a factory give you” (concrete class that implements a known interface, like a database connector or cross-platform representation of a GUI object) or “why would you use a singleto” (global data store, or even a factory!) Bonus question – what does “concrete class” mean? Expect why questions – anyone can implement someone else’s pattern – why was it a good idea?

General Programming: How do you track production system bugs down (this is open ended – no right answer – but have some idea, please!) Why is refactoring old code generally a good idea? Or is it a bad idea? What’s refactoring anyway?

I can’t be bothered asking questions about XML but there are plenty – I leave that as an exercise for you, dear reader.

Some of these questions overlap, obviously.

Bottom line: If you claim to know something then expect to be asked about it – I will bone up on the web if I don’t know to do you the courtesy of being able to shine – I want you to succeed, honest.

Bottom line: Don’t make claims you can’t back up. Don’t waste my time.

Envoi: I don’t know is a fine answer, don’t be afraid of it. You get more respect for it. Just don’t sit there watching the tumbleweed after claiming to be a world expert on something – it makes us all so embarrassed.

Bootnote: 2015

I believe that enums are now part of Java and the daft problems where the compiler would sometimes re-order code outside of synchronized blocks have been resolved. Not so sure about the singletons not being singletons when they get moved from VM to VM problem though.

Coding Horror: Presentation: Be Vain

http://www.codinghorror.com/blog/archives/001007.html?r=21636#endcomments

I spent most of my spare time from age 8 to 16 learning to play the guitar and these instant gratification things just annoy me.

That said, I agree with the point that presentation is far more important that people think, and the example is a reasonable one. Please stop focusing on the specifics here – Jeff’s trying to make a very reasonable point.

Techies start from a data model and then wrap it with some half-assed forms from a generator – I know we do, I’ve done it myself lots. I learned that this does’t work because the logical view of the data isn’t the user’s view. Not listening to their pain and trying to help them get to where they want is arrogance. For example, on a system our users use (and I did’t write) they hate the way it takes forever to open the person details screen and all they ever want is the email address or phone number – a techie generated the user form out from the data model and joined it to the logically correct place. Imagine how much extra effort it would have been to put the phone number and email address on the summary? Hardly any, but it does’t fit the cod-head approach. I would lay money the coder has never talked to a user or watched one use the system.

The sexy unwritten thing is’t at all new. I worked on many projects in the pre-web days where the client had “bought the powerpoint”. Then they kicked us lots because it was all a lie.

© 2018 Francis Fish

Theme by Anders NorénUp ↑