Jump to content

Recommended Posts

Posted

Hello, I had a talk with Elfocrash over in Skype some hours ago regarding good programming practices in java. This brought up a book I had read a year ago regarding that subject. It's called Clean Code, - A Handbook of Agile Software Craftsmansmith written by Robert C. Martin and I believe it's one of the most amazing books I've read so far. Since not everyone has the patience and free time to read 300 pages I feel the obligation to sum the key points of it up and post them in the form of tips in here for those that want to do an extra step to better coding.

 

I'll post very small , summarized portions of the book the same order as they are written in there starting from very simple things like naming and ending up to complex things like concurrency. Based on my mood i'll post from one to five/six tips per day each on a different post. None of them will be huge, it will only take about a minute to read them or even less.

 

Tip #1: Use Intention revealing names. Your variables, constances, methods, should reveal decent information about their role in the code.

Difficulty : Very easy

 

------------------------------------------------------------------------------------------------------------

 

Bad example:

 

int i =7; //This is the days in a week

 

Good example:

 

int daysInWeek = 7;

 

There's no need for comenting when you can place this info in the variable while maintaining its size relatively small.

 

------------------------------------------------------------------------------------------------------------

 

Bad example:

 

private DimensionalRift _dr; //The Dimensional Rift

 

Good example;

 

private DimensionalRift dimensionalRift;

 

It's not clear what _dr is when you read it later in the code. And the underscore _ is not even needed, it provides no information.

 

------------------------------------------------------------------------------------------------------------

 

 

Tip #2: The Law of Demeter: A method f of a class C should only call methods that fullfill one of the following conditions:

1)Belong in C

2)Are methods of argument objects of f

3)Are methods of objects created by f

4)Are methods of objects referenced by C

Difficulty : Medium

 

------------------------------------------------------------------------------------------------------------

 

Bad example:

 

public void calculateEarnings()

{

  resultObject = EarningsManager.getInstance().calculate(); //Bad practice EarningsManager does not fullfill any of the conditions.

  String result = resultObject.getResult().getOutcome().getStringValue(); //Trainwrech, resultObject calls nested methods that belong to other objects.

}

 

Good example:

 

public void calculateEarnings(EarningsManager manager)

{

  result = manager.calculate();

  handleResult(result);

 

}

 

public void calculateEarnings()

{

  result = manager.calculate(); //Now manager is a referenced instance inside our class.

  handleResult(result);

 

}

 

In this second example, we pass the manager as argument or reference it inside C class for the method to use it.

http://en.wikipedia.org/wiki/Law_of_Demeter

------------------------------------------------------------------------------------------------------------

Posted

Very nice idea m8.

Maybe should u put all the portions that will u type, in the main post? Coz if ppl comment &/|| spam, it will be "hard" to find every tip.

 

About the first one, the problem comes when the "coding" evolves to "coding to not let leachers 'decrypt' so easy", huh? :P

Posted

Bad example:

 

private DimensionalRift _dr; //The Dimensional Rift

 

Good example;

 

private DimensionalRift dimensionalRift;

 

It's not clear what _dr is when you read it later in the code. And the underscore _ is not even needed, it provides no information.

 

About this one , I prefer on using example _dimensionalRift. Just helps me  to seperate some things in my projects .

 

Would be better to update your main reply than re-reply each time , it will be difficult to read .

Posted

About this one , I prefer on using example _dimensionalRift. Just helps me  to seperate some things in my projects .

 

Would be better to update your main reply than re-reply each time , it will be difficult to read .

 

Just let him do it the way he wants to.

If the topic gets way too spammed, then when the "guide" is finished, a mod can clean it up, combine the posts and then re-open it.

 

Great idea, by the way :]

Posted

True I can update the first post and just bump for reminder.

 

@TheOldOne

 

What's different between _dimensionalRift and dimenisonalRift ? If you need to have two variable with different meaning you can name them different. Cause when someone else reads your code, he can't know what you mean with _. If for example you wanted one rift to be temporary you could write tempDimensionalRift. That makes more sense to others reading your code cause temp is a widelly accepted term for something that is temporary than "_" is.

 

Generally this is called variable encoding. People in the past were using different types of encoding ( postfix, prefix, Hungarian Notation ) because the compilers of the older languages could not do type checking. So they had for example to write m_dimensionalRift, username_String to remind themselves that this is a member of a class etc. But nowadays compilers do type checking so this _ thing is just an artifact of the past.

 

The reason most people are using it, is because they saw it in some code written by oldschool people who are used to write code the old way. We don't live in the 1990's anymore.

Posted

Tip #3: Writting testable code, means avoiding static calls, singletons and the new operator inside method calls.

Difficulty : Medium

 

------------------------------------------------------------------------------------------------------------

 

Bad Example:

 

public boolean myMethod()

{

    // Injecting MyTestObject here for testing needs code editing. Factory is prefered.

    MyObjectInterface object = new MyObject();

 

    //Database can't be replaced with dummy database for testing. Also this code violates the Law of Demeter

    DatabaseManager.getInstance().persist(object);

 

    //Static call can't be replaced with dummy for testing;

    return Geodata.evaluate(object);

}

 

Good example:

 

private MyObjectFactoryInterface factory;

private DatabaseServiceInterface databaseService;

private GeodataInterface geodata;

 

public boolean myMethod()

{

    //The factory implementation can define what type of object is returned. Production or testing object etc.

    MyObjectInterface object = factory.getNewObject();

 

    //Database service can now be either production object or just a dummy in-ram container.

    databaseService.persist(object);

 

    //Geodata can now be an empty object that implements the corresponding interface.

    return geodata.evaluate(object);

}

 

------------------------------------------------------------------------------------------------------------

 

 

  • 2 weeks later...
Posted

"Agile Software Craftsmansmith"

^ this, the one who manages to handle extreme programming or uses the ruck, will find easy to develop with L2J, and the neural networks theory would be easy as fuck.

Thanks for the bibliography, I'm already looking for the book c:

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Posts

    • You should buy it then I’ll make a discount  
    • Hi everyone,   In 2014, I completely stepped away from developing L2 servers and doing L2J-related work. Since then, I’ve only opened this server about once a year and helped a few servers and individuals for free. I haven’t taken on any paid L2J work since then.   LINEAGE2.GOLD is a project that has reached about Season 6. The first season launched at the end of 2020 and was a fully rebuilt Gold-style server on the Classic client (protocol 110). It featured many custom systems and enhancements. After several seasons, I decided to abandon the Mobius-based project and move to Lucera, as my goal was to get as close as possible to Interlude PTS behavior while still staying on the L2J platform.   The current project was once again completely rebuilt, this time on the Essence client (protocol 306), and is based on Lucera. Because of that, acquiring a license from Deazer is required.   My Lucera extender includes, but is not limited to: Formulas.java Basic anti-bot detection, which proved quite effective, we caught most Adrenaline users using relatively simple server-side logic, logged them, and took staff action. Simple admin account lookup commands based on IP, HWID, and similar identifiers. In-game Captcha via https://lineage2.gold/code, protected by Cloudflare, including admin commands for blacklisting based on aggression levels and whitelisting. Additional admin tools such as Auto-Play status checks, Enchanted Hero Weapon live sync, force add/remove clans from castle sieges, item listeners for live item monitoring, and more. A fully rewritten Auto-Play system with support for ExAutoPlaySetting, while still using the Auto-Play UI wheel, featuring: Debuff Efficiency Party Leader Assist Respectful Hunting Healer AI Target Mode Range Mode Summoner buff support Dwarf mechanics Reworked EffectDispelEffects to restore buffs after Cancellation. Raid Bomb item support. Reworked CronZoneSwitcher. Prime Time Raid Respawn Service. Community Board features such as Top rankings and RB/Epic status. Custom systems for Noblesse, Subclasses, support-class rewards, and much more.   Depending on the deal, the project can include: The lineage2.gold domain The website built on the Laravel PHP framework The server’s Discord Client Interface source Server files and extender source The server database (excluding private data such as emails and passwords)   I’m primarily looking for a serious team to continue the project, as it would be a shame to see this work abandoned. This is not cheap. You can DM me with offers. If you’re wondering why I’m doing this: I’ve felt a clear lack of appreciation from the L2 community, and I’m not interested in doing charity work for people who don’t deserve it. I’m simply not someone who tolerates BS. Server Info: https://lineage2.gold/info Server for test: https://lineage2.gold/download Over 110 videos YouTube playlist: https://www.youtube.com/watch?v=HO7BZaxUv2U&list=PLD9WZ0Nj-zstZaYeWxAxTKbX7ia2M_DUu&index=113
  • Topics

×
×
  • Create New...

Important Information

This community uses essential cookies to function properly. Non-essential cookies and third-party services are used only with your consent. Read our Privacy Policy and We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue..

AdBlock Extension Detected!

Our website is made possible by displaying online advertisements to our members.

Please disable AdBlock browser extension first, to be able to use our community.

I've Disabled AdBlock