Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am currently a junior engineer and keen on learning best practices and expanding my experience. My question concerns any static programming language such as Java, C#, C++ etc.

When I am writing code, I like making it easy to read. In my opinion, reading code should be like reading a book. It should be fluent and elegant. Then, even if I do not need to use them, I like prepending this, superbase keywords whenever I can. Ditto for current class name when I want to use constants or static variables/methods.

Consequently, another developer can quickly have a long shot. He knows this method or var is in the current class or in mother class without needed to move to declaration. You may say to me it is really easy to do that with recent IDE. However, I know a bunch of guys who are still working with Vim, Emacs or other editors without these fancy features.

When explaining it, I like comparing my practices to a drama book. We have all read at least one of them, such as Macbeth from Shakespeare. Who has never felt frustrated to go back at the beginning of the book to know who is that character and what part he has in the plot? You did that so many times, right?

My question can be associated to literate programming practices. However, I assumed Donald Knuth's recommandations are more about commenting and adding macros to your code.

So I would like to know your opinions and feedbacks about these practices. Am I doing too much? Should I keep it simpler? Am I breaking OOP rules?

Here is a code sample:

class Bar {
    protected int _motherVar;

    public aBarMethod() { }
}

class Foo extends Bar {
    private static List<String> _strings;

    private int _myPrivateVar;

    public void aMethod() {
        super.aBarMethod();

        this._myPrivateVar++;
        super._motherVar -= 2;
    }

    public static String anotherMethod() {
        String outcome = "";

        for (String s : Foo._strings) {
            outcome += s;
        }

        return outcome;
    }
}
share|improve this question
2  
There are individual style guides for each programming language. You can find them by doing an internet search for "[language] coding standards." Here's a good one for c#: se.inf.ethz.ch/old/teaching/ss2007/251-0290-00/project/… –  Robert Harvey Feb 28 at 18:36
7  
In my opinion, using "this" and an underline is overkill. But using one is o.k., and arguably good. Most Java programs use neither, and that is generally accepted style. Thats why I think using vim is a horrible idea. (Puts on flameproof racing suit) –  user949300 Feb 28 at 18:59
3  
The signal to noise ratio of Java code is bad enough, no sense in making it worse. –  whatsisname Feb 28 at 19:09
5  
Honestly, class and method definitions should be relatively small. If you have to resort to lexicographic mnemonics to keep track of your identifiers, your code needs to be refactored into smaller portions. (And @user949300, I am focusing an emotional flame in your direction!) –  Kent Anderson Feb 28 at 19:40

1 Answer 1

I am not sure about Java, but C# has a whole set of framework design guidelines, that includes naming conventions for public and protected members. Protected fields are not recommended. Use protected properties instead.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.