A method generally relates to code recommendation. In the method, code snippets (220 1, 220 2, ...220 n) may be extracted using an invocation-centered code slicing process and then grouped according to the method usages. For each method usage, a representative code snippet may be selected and stored in the knowledge base (210) in association with metadata (230 1, 230 2, ...230 n). The programming context may be obtained and used to query the knowledge base (210) to retrieve one or more code snippets for recommendation. The recommended code snippets may be ranked to improve the utility and user friendliness, and the metadata (230 1, 230 2,...230 n) may be used to provide variation points and possibly other auxiliary information to improve the operation efficiency and user experience.