Click here to Skip to main content
15,900,536 members
Articles / Style

Coding Style

Rate me:
Please Sign up or sign in to vote.
3.80/5 (4 votes)
19 Jan 2015CPOL2 min read 7.3K   3
Coding style

Style has an important role when it comes to writing clean, readable code. Conforming to conventions and best practices is a good starting point. In addition to that there is still plenty of room for choice. Those depend greatly on the level of the developers and personal preferences.

There is a very specific danger with conventions and best practices. Most of them are distilled experience of the industry and are there for good reasons. Some of them may be questionable but are rarely - if ever - questioned. They get incorporated into the coding style and without conscious effort to pinpoint them become invisible. I think all developers should investigate their code from time to time and inspect even the most trivial elements for such issues.

A couple of things that I think are worthy to look at:

Getter/setter methods
Since IDEs can generate them so easily, these are very often abused. Some developers have the habit of generating them right away, throwing away the most important element of object oriented programming - encapsulation - without further thought. Mutators are created on objects that could be immutable. The get...() prefix is often meaningless, and actually harms readability:

person.getName();
person.getAddress();
person.getMobileNumber();

person.name();
person.address();
person.mobileNumber();

Constant naming conventions
The convention is to use uppercase names which can be quite unreadable - especially if there are many long-named constants in a class. Unless it's part of the api, the fact that a field is constant is an implementation detail.

Final keyword on local variables
Some static code analyis tools show warnings when local variables are not marked as final - if they've been assigned only once. While it adds value in many cases, if applied on all local variables without reason, the clutter it introduces can make the code harder to read. One can argue their constant use is good in long and complex methods, but in my opinion the problem is having such methods in the first place.

Exception suffix
A really controversial one, but I think it's worth to at least toy with the idea. Almost all exceptions have the ...Exception suffix in their names. This is actually not really necessary and it does add some noise. Note that in whatever context we encounter an exception, the syntax makes it already obvious:

 

  • Definition of an exception - Something extends Exception
  • Throwing an exception - throw new Something
  • Declaration of an exception thrown - throws Something
  • Catching an exception - catch(Something)

Dropping the suffix can work on exceptions that are named as events, not just plain nouns (which is btw a useful practice on it's own right). Compare FileNotFound vs FileNotFoundException. It can be interesting to see it's effect in a codebase. Some libraries do this, eg javassist, bouncycastle.

Anyways - no absolute truth or silver bullets here. Just some food for thought.

 

This article was originally posted at http://codejargon.blogspot.com/2014/02/coding-style.html

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Norway Norway
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 3 Pin
Member 1017828122-Jan-15 1:56
Member 1017828122-Jan-15 1:56 
GeneralRe: My vote of 3 Pin
mrcellux22-Jan-15 4:42
mrcellux22-Jan-15 4:42 
These are not really views, but ideas, exercises as you say.

This practice is "baked" into us and it's hard to think objectively about it. While I don't think it's fully useless, it's nice to always think about the noise-usefulness ratio. It does share some similarities with practices like hungarian annotations or naming each and every abstract class *Base as a rule.

If you haven't tried it before, try to refactor a small codebase with event-style exception names without the suffix and see how it reads in practice. It is at the least interesting to see and few devs I know have done it. Some libraries do this, like Javassist or BouncyCastle.

That being said, I dare not to drop the suffix on public APIs, or even implementation code that's maintained by more than a small number of devs that all agree on deviating from the standard. The "exceptations" are too strong on the subject. Smile | :)

modified 22-Jan-15 10:49am.

GeneralExceptions Pin
PeejayAdams21-Jan-15 2:26
PeejayAdams21-Jan-15 2:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.