Let \begin{document}$ G $\end{document} be a generator matrix of a linear code \begin{document}$ \mathcal C $\end{document} and \begin{document}$ [G: I_k] $\end{document} be a generator matrix of its extendable linear code \begin{document}$ \mathcal {C}' $\end{document}, we call \begin{document}$ \mathcal C $\end{document} is optimally (almost optimally) extendable if \begin{document}$ d(\mathcal C^\perp) = d({\mathcal C'}^\perp) $\end{document}(\begin{document}$ d(\mathcal C^\perp) $\end{document} is very close to \begin{document}$ d({\mathcal C'}^\perp) $\end{document}, respectively), where \begin{document}$ d(\mathcal C^\perp) $\end{document} is the minimal distance of the dual code of \begin{document}$ \mathcal C $\end{document}. In order to safeguard the susceptible information lay in registers oppose SCA and FIA, it is useful to construct an optimally extendable linear code \begin{document}$ \mathcal C $\end{document}. In this paper, we construct three classes of (almost) optimally extendable linear codes: (1) irreducible cyclic codes; (2) maximum-distance-separable (MDS) codes and near maximum-distance-separable (NMDS) codes.