DEV Community

Cover image for Coding Best Practices Part 1 (Naming Conventions & Class Designing Principles)

Coding Best Practices Part 1 (Naming Conventions & Class Designing Principles)

Mohit Rajput on April 30, 2017

In my last article IMPORTANCE OF WRITING CLEAN CODE, you realized the importance of creating a beautiful piece of code. You learned some techniques...
Collapse
 
klausdonnert profile image
Klaus Donnert • Edited

I know this is a pipe dream, but it would be nice if there were only one universal style. I work with PHP, JS, and CSS. Frequent Switching between camelCase, snake_case and kebab-case has cost me many annoying bugs because I will invariably mis-name a variable because I forgot (just for a second) the context of the name.

But then there's Google's place_id. Nice. I'm not the only one that does it. :)

Collapse
 
mohitrajput987 profile image
Mohit Rajput

I can understand that pain. Switching between different type of languages make it hard to follow naming convention rules.
To catch the pace, don't switch frequently in different languages. Give at least one or two entire days to one language.

Collapse
 
kardonice profile image
shitpost_​​​​​​​bot

This is wildly over-expansive to the point of being useless.

If you're writing a project in Java or Qt, these recommendations are great. However, if you're using Python or Boost for a project, using camelcase for variables and functions will make your code look alien from the libraries you are working with. This is especially true with Python, where the official, recommended style guide contradicts (and overrides) the style suggestions here. There isn't one good integrated style guide for code. It depends on the project, the libraries used, and the language.

Collapse
 
mohitrajput987 profile image
Mohit Rajput

At the end, I mentioned that this is more of Java, C++ and PHP languages.
Languages like Python, ROR have different styles and use underscore as well. But class design principles are applicable everywhere in OOP.

Collapse
 
kardonice profile image
shitpost_​​​​​​​bot

The class design principles are definitely useful. But I think it's worth highlighting that rather than having a single, unifying style guide, that you should follow the conventions of the language or framework you are using, which will make your code readable, hopefully idiomatic, and easy-to-maintain.

Collapse
 
bitdaddy profile image
Mountain Lover

In your IShape code example, it isn't clear for me what is desirable to meet Interface Segregation Principle (ISP). Should IShape only have getArea () method only not to force Circle to have getWidth() method?

Collapse
 
mohitrajput987 profile image
Mohit Rajput

Yes. If IShape has the only getArea() method, it can be used in any shape i.e. Rectangle, Circle, Triangle to the area of that shape. The circle doesn't have width or height so Circle class itself can have a method called getRadius(). Similarly Rectangle can have getWidth() and getHeight().

Collapse
 
webbanditten profile image
Patrick Nærvig Wøhlk