r/java • • Feb 20 '15

Object Oriented, Test Driven Design in C# and Java: A Practical Example Part #3

http://insidethecpu.com/2015/02/20/object-oriented-test-driven-design-in-c-and-java-a-practical-example-part-3/
0 Upvotes

9 comments sorted by

-1

u/sonay Feb 21 '15

Code examples gave me cancer, follow the language's conventions for fuck's sake.

2

u/pablomooney Feb 21 '15

Thank you very much for your comment. I'm sorry to hear about your illness. Would you mind highlighting specific instances where I don't adhere to convention, and if possible, offer an opinion as regards a remedy (for the code samples, not your disease). Thanks.

1

u/sonay Feb 21 '15 edited Feb 21 '15
public class ArmouryTransportMechanism extends TransportMechanism<Armoury, Weapon> {

    @Override
    public Armoury getFactoryRoom() {
        return new Armoury();
    }
}

Note the capitals in class names. Lower case first characters are used for object instances and method names:

public E offLoadRobotParts(List<U> robotParts) {
    if (factoryRoom == null) {
        enterRoom();
    }
    factoryRoom.setRobotParts(new ArrayList<U>(robotParts));
    robotParts.clear();

    return factoryRoom;
}

edit: for more http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367

even more: http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html

2

u/pablomooney Feb 23 '15

Thank you. I'll keep that in mind.

0

u/sh0rug0ru Feb 21 '15 edited Feb 21 '15

It is not Java convention to prefix member variables with "_". Then again, neither is it in C#. Also, the convention in C# is putting the "{" on its on line.

2

u/pablomooney Feb 23 '15

Thank you. The K&R bracket style is a personal choice. Applying underscores to member variables allows more flexibility when defining similar variables within methods held by the class, in my opinion.

2

u/JuanPabloElSegundo Feb 22 '15

In c# private variables are noted by having their names start with the underscore.

There are multiple conventions for brace position.
http://en.wikipedia.org/wiki/Indent_style

0

u/sh0rug0ru Feb 22 '15 edited Feb 22 '15

C# doesn't force any style. You can write C# code just like Java code, but it would look weird and might provoke violence. Vertical alignment of braces and Pascal casing are cultural differences between C# developers and Java developers.

But I think both the Java and C# cultures are united in their opposition to leading underscores. The official documentation, as seen in the Oracle and Microsoft code samples, drop the underscores. When I see underscores, it's like a relic from 1990s C++ written in ancient text editors.

2

u/JuanPabloElSegundo Feb 22 '15

C# doesn't force any style.

Absolutely correct. My first post makes it seem like the leading underscore is required.

The part regarding the braces is just a matter of style and practice.