Recently I came across a statement which meant roughly the following.
If we want to apply composition of functions, then the co-domain of the inner function should be equal to the domain of the outer function. Mathematically, if $f$ and $g$ are functions and $f : A \rightarrow B$ and $g: C\rightarrow D$, so that we can perform $fog$ we must have $D=A$ and the pictorially the situation is something like this:
But I do not think it is necessary to have $A=D$ in the above diagram. I mean even if $D \subseteq A$ then I guess our composition shall work just fine. But in that case one might argue that the co-domain of the inner function $g$ degenerates to $A$. Yes it is fine, but if strictly $g$ is defined as $g: C\rightarrow D$ and $D\subseteq A$ then we can define a new function $h$ which is same as $g$ except that the co-domain is degenerated to $A$. But $h$ is a new function after all different from $g$ as far as the domain and co-domain is concerned.
For example if we have $g: \mathbb{N} \rightarrow \mathbb{N}$ and $g(x)=x$ and $f:\mathbb{R} \rightarrow \mathbb{R}$ and $f(x)=\sin (x)$ then without any problem we can do $h(x)=f(g(x))=\sin (x)$ where $h: \mathbb{N} \rightarrow \mathbb{R}$
Below is an excerpt from the text Thomas' Calculus:
Which I guess has intuitively more generalized the situation (and more practical though) that range of $g$ should be subset of domain of $f$.


You are right in the sense that codomains tend to be overly specific. We specify both a domain and codomain anyway to give context to our discussion of a function. If you want to narrow that context, by all means restrict the domain or codomain.
You might be interested in the composition of relations. A relation $r:A \to B$ is a subset $r \subseteq A \times B$. Like functions, relations don't have to implicate every member of their codomain (there may be $b \in B$ that don't appear as the second coordinate of a member of $r$). But unlike functions, relations don't need to implicate every member of their domain either (there may be $a \in A$ that don't appear as the first coordinate of a member of $r$). So both the domain and codomain of relations may be overly specific.
We can compose relations $r : A \to B$ and $s: B \to C$. The composite $s \circ r : A \to C$, which I like to read as "$s$ after $r$", is defined by $$ s \circ r = \{(a,c) \in A \times C \;:\; \exists b\in B \text{ such that } (a,b) \in r \text{ and } (b,c) \in s \}. $$ Note that this definition of composition doesn't care so much about every member of $A,B,C$, only those elements implicated by one of the relations $r,s$. Also note that the composite $s \circ r$ may implicate less members of $A$ then did $r$. That is, there may be $a \in A$ that appear as the first coordinate of a unique pair $(a,x) \in r$, but with no $c \in C$ for which $(x,c) \in s$, so that $a$ was lost in composing $r$ with $s$.