Naming Conventions

Naming Conventions

The basic naming conventions apply to most if not all languages( I am mainly basing this off C++ as its what I have been working with recently, but from what I remember these also generally apply to Java, C, and C#), although the standard(if there is one) may vary depending on the language. The reason to follow naming conventions is to make your code more readable.

The basic rules for this are:

  • Use meaningful names
    • Try to be short, but still easily readable
      • a good rule of thumb is for it to be at least two words, but no more than 20-30 characters long.
        • also if you use abreviations make sure its a common abreviation
      • ex: call something studentNumber or studentNum not num
  • Use different styles for different kinds of identifiers
    • ex: use camal case for variables, but use pascals case for function names, and snakecase for class names
  • For global constant varibles use all caps with underscors between words
    • global as in declared outside of any function, which means it applies to the whole module
  • BE CONSISTANT
    • choose a convention and stick with it throughout the code, don’t randomly switch what you use for function names and variable names or something

There are 4 or so naming conventions:

  • camelCase:
    • the first letter of everyword but the first word is capitalized
  • PascalCase:
    • the first letter of every word is capitalized including the first word
  • snake_case:
    • no capital letters
    • put a underscore between differwnt words
  • sHungarianNotation:
    • in this notation you start the identifier with an abbreviation that tells you what the datatype or the purpose of the variable is
      • in this case sHungarianNotation would be a string as ‘s’ is a common abreviation of string
        • another example would be arrStudentNames, which would be an array
    • after the abreviation should be a descriptor of the variable
    • this also follows CamalCase, in how its capitalization

One other thing to note is that it is generally better for code to be over commented than under commented, so feel free to comment the purpose of the variable, especially if it’s something very specific.

Some typical cases where people don’t follow naming conventions is when it’s a temporary variable like the index for a loop, in which case people generally use something along the lines of i, or if i is already being used some other random letter like j or k. People may also commonly just name temporary variables temp.

Keep in mind that naming conventions are not enforced by the compiler, they simply are good practice in terms of making sure other programmers can actually read your code when necessary.

objects

objects

A object is essentially the instance of a class. So alternatively a class is like the blueprint of a object.A object will have attributes and behaviors, like one would in real life.

A attribute would be like the color, length width and weight of iteam. This is typically represented through the parameters used when making a object( which are passes into the constructor and turned into fields/ global variables).

Behavior is the things the object can do. For example a tea kettle can boil water so a tea kettle object would have a method it could call to boil the water.

Creating a object:

ClassName objectName = new ClassName(<parameter>);

So when creating a object you need to declare what you will call it create the object with the new operator and call the constructer which initializes the object, similar to how you make a variable.

Calling a method:

Variablename = ObjectName.methodName(<parameter>);

Of course you can also declare a variable at the same time as calling the method and of the method is void you don’t set a variable to it.

Calling a field:

Most of the time you should have a get or set method but if you don’t . . .

objectName.varableName

Either in a system.print, parameter or setting a variable.

Interface

Interface

A interface is like a class where non of the methods have a body(called a abstract method) so a common analogy that is used for interfaces is that it’s like the blueprint of a class. Which basically means that tells what a class has to do but not how.So the main purpose of a interface is either to use polymorphism more effectively or to have interaction between to classes be based on the interfaces abstract methods which one of the classes implements since if the class implements the interface then it must define the methods given otherwise it becomes a abstract class and Java doesn’t support objects of abstract classes. (So by implementing the interface, the class is essentially promising that it will define the methods in the interface in that class or a subclass of that class)

A interface is made as shown below :

public interface interfaceName

{

returntype methodName(type parametername);

}

You can also make variable constants, but this isn’t recommended because the constants of different interfaces if given the same name and implemented by the same class could cause the program not to know which to use and cause a error.

In JDK8 and JDK9 there have been some updates to interface for more info on that you can look at the geek for geek article on interfaces( in case you didn’t realize it I’m not a professional so I do actually try to double check that my info is right before posting a article.)

Or you can wait till I add that information to here.

Some related topics are:

inheritance:

classes(not done yet):

Object oriented programming

Object oriented programming

 Also known as OOP, is a method of programming that is event-driven so instead of just going from the beginning of the code to end like  in procedural programming it goes in order in the main method until it reaches a call to another method or the creation of an object through a constructor. Java is a good example of OOP programming.

   In other words oop programming is like  a virtual world of objects and each object has its own memory and a set of methods(things it can do like send messages , process messages, change its memory or create new objects).  Each object in this world is a part of a class , which determines what the object does, what methods it has and how it’s memory is structured.

 The other important thing about oop programming other than event-driven programming is inheritance, which is where a class extends another class. So, say you made a general balloon object and then later you made a oval balloon and a square balloon, it would be a waste to duplicate the general balloons methods, instead you have oval balloon and a square balloon extend the balloon class, making them a subclass of the general balloon class meaning that they have access to all of general balloons methods. The general balloon class may be called a superclass if another class extends them.