Skip to content

Grammar of Programming Languages

Programming is not only about rules of code language (e.g. JAVA, PHP, C++, JS, etc) but also rules of your human language, which you are using to write variable and function names (e.g. English).

Code written without following grammar rules of your human language, only lead to confusion.

Here are some examples, how to follow rules of your human language in your programs (for English language):

1. Class names should be always a singular word and a noun e.g. Person, User, Car

2. Variable and constant names should always start a noun (e.g. name, email, password) or an adjective-noun combination (e.g. isPerson, isActivePerson, fetchedValues, userHasValues)

Exceptions: Class variable and constant names sometimes can have only adjectives e.g. etc.

3. Function names always start with a verb and end with a non-verb (noun, adjective etc) e.g. fetchData, sendEmail, createUser etc

Exceptions: functions can start with underscore as well. magic functions e.g. __construct, __clone, __get etc

4. Class methods can start with verb and adjective as well e.g. getField, setField, isActive, hasValue

Do you also follow these rules or any other kind of grammatical rules while coding? What do you think about these rules if they make sense to you?

Published inBlog