Sometimes aubergine and courgette tastes fine when cooked as is, but other times they are so bitter that they're almost inedible. I've tried dry salting and leaving the slices in salt water beforehand for various lengths of time, but this is a mission and can often make them too salty. Also the outcome seems pretty random given that sometimes this isn't necessary. Is it to do with freshness?
Yes, it is to do with freshness - the fresher and younger the aubergine, the less bitter it will be. If you have an old aubergine, you could try peeling it, as the bitter compounds are concentrated just under the skin.
In Turkish cuisine the tradition is to peal the eggplant lengthwise in zebra stripes an inch wide and to slice the eggplant into thick wedges which are then soaked in very salty cold water for at least a quarter hour. Just before cutting smaller and cooking you squeeze out the salt water.
This should help against them turning dark and bitterness.
I slice them into 1-2cm thick circles, sprinkle granular (coarse) salt on both sides and let sit for at least half an hour, preferably in the sun. Afterwards rinse with water to remove the salt and extracted liquid and proceed with the recipe.
When i am cooking courgettes, i usually taste a thin slice from each one - raw. If it is bitter i just discard it. One bitter courgette can ruin a dish. Regarding aubergines... no raw tasting, of course. Then i go for the salt trick.