Fractal Softworks Forum

Please login or register.

Login with username, password and session length
Advanced search  

News:

Starsector 0.97a is out! (02/02/24); New blog post: Simulator Enhancements (03/13/24)

Author Topic: Coding Java for Noobs  (Read 4836 times)

WarStalkeR

  • Captain
  • ****
  • Posts: 343
  • Per Aspera Ad Astra!
    • View Profile
Coding Java for Noobs
« on: May 12, 2012, 02:02:37 AM »

Coding Java for Noobs, or rather how to make your scripts optimized, understandable and useful for yourself and for others.
I've started this topic because sometimes when I look though scripts my eyes starting to bleed and my soul starts to scream.

Spoiler
Let's begin, from some basic stupid examples:
This guy will create new array like this:
array[1] = 0;
array[2] = 0;
array[3] = 0;
array[4] = 0;
array[5] = 0;


or even worst:
a1 = 0;
a2 = 0;
a3 = 0;
a4 = 0;
a5 = 0;

And this guy will write it like this:
int[] array = {0,0,0,0,0};

See the difference?
[close]

Java Language is easy to learn, easy to start and easy to mess up. My point is try to pack as much code as you can in functions:

Good example is Paul's pardon system:
    if (pvi < 0) {
      boolean haspardon = playercargo.removeItems(CargoAPI.CargoItemType.RESOURCES, "ipardon", 1);
      if (haspardon) {
      player.setRelationship("independent", 0);
      Global.getSectorAPI().addMessage("The Gun Runners explained things to the Independents, they will see you as neutral now.");
      cargo.addItems(CargoAPI.CargoItemType.RESOURCES, "ipardon", 1);
    }}
    if (pvt < 0) {
      boolean haspardon = playercargo.removeItems(CargoAPI.CargoItemType.RESOURCES, "tpardon", 1);
      if (haspardon) {
      player.setRelationship("tritachyon", 0);
      Global.getSectorAPI().addMessage("The Gun Runners hacked Tri-Tachyon, you've been erased from their kill list.");
      cargo.addItems(CargoAPI.CargoItemType.RESOURCES, "tpardon", 1);
    }}


And my way of using this pardon system:
   PardonCheck(player, playercargo, cargo, "idf", "idf_pardon", "The Negotiators probed their connections with IDF, you now can try to contact them.");
   PardonCheck(player, playercargo, cargo, "hierarchy", "hir_pardon", "The Negotiators bribed main command of Hierarchy in this sector, they will ignore you now.");

   private void PardonCheck(FactionAPI player, CargoAPI playercargo, CargoAPI stationcargo, String side, String pardonitem, String message) {
      if (player.getRelationship(side) < 0) {
         boolean haspardon = playercargo.removeItems(CargoAPI.CargoItemType.RESOURCES, pardonitem, 1);
         if (haspardon) {
            player.setRelationship(side, 0);
            Global.getSectorAPI().addMessage(message);
            stationcargo.addItems(CargoAPI.CargoItemType.RESOURCES, pardonitem, 1);
         }
      }
   }


If you won't follow this simple rule of coding, sooner or later you will got lost in your own code.
All questions regarding java are welcome :)
« Last Edit: May 12, 2012, 03:45:49 AM by WarStalkeR »
Logged

"Happiness for everybody, freely, and let no one to leave unhappy!" (c) Strugatsky Brothers
Independent Defense Force is here! And they already in Sector Xplo.

Upgradecap

  • Admiral
  • *****
  • Posts: 5422
  • CEO of the TimCORP
    • View Profile
Re: Coding Java for Noobs
« Reply #1 on: May 12, 2012, 02:07:13 AM »

I don't get your pardon system.  You call the same faction twice? (Side)
Logged

WarStalkeR

  • Captain
  • ****
  • Posts: 343
  • Per Aspera Ad Astra!
    • View Profile
Re: Coding Java for Noobs
« Reply #2 on: May 12, 2012, 03:44:56 AM »

I don't get your pardon system.  You call the same faction twice? (Side)
Nope, side is faction to which you want change relationship and player it is you.
Logged

"Happiness for everybody, freely, and let no one to leave unhappy!" (c) Strugatsky Brothers
Independent Defense Force is here! And they already in Sector Xplo.

Uomoz

  • Admiral
  • *****
  • Posts: 2663
  • 'womo'dz
    • View Profile
Re: Coding Java for Noobs
« Reply #3 on: May 12, 2012, 04:12:22 AM »

This looks interesting! I'll surely learn much from this thread. Thanks WarStalkeR!
Logged

Okim

  • Admiral
  • *****
  • Posts: 2161
    • View Profile
    • Okim`s Modelling stuff
Re: Coding Java for Noobs
« Reply #4 on: May 12, 2012, 04:17:19 AM »

LOL. I`m not that good with Java, but i can clearly see the simpleness and yet effectiveness of your method :)

Keep it going. Some nice examples of various code posted here would be great.

basket

  • Ensign
  • *
  • Posts: 2
    • View Profile
Re: Coding Java for Noobs
« Reply #5 on: May 12, 2012, 07:30:31 AM »

I wouldn't overdo it on extracting code into functions, specially if you call the function very often and copy a lot of bytes.
Logged

Emailformygames

  • Commander
  • ***
  • Posts: 103
    • View Profile
Re: Coding Java for Noobs
« Reply #6 on: May 12, 2012, 10:46:31 AM »

Why is Java so much fun?  :)
Logged

WarStalkeR

  • Captain
  • ****
  • Posts: 343
  • Per Aspera Ad Astra!
    • View Profile
Re: Coding Java for Noobs
« Reply #7 on: May 12, 2012, 01:40:34 PM »

Why is Java so much fun?  :)
Ask Alex, he definitely has answer for this kind of question ;D
Logged

"Happiness for everybody, freely, and let no one to leave unhappy!" (c) Strugatsky Brothers
Independent Defense Force is here! And they already in Sector Xplo.

Dr.Noid

  • Lieutenant
  • **
  • Posts: 79
    • View Profile
Re: Coding Java for Noobs
« Reply #8 on: May 13, 2012, 01:27:19 AM »

I wouldn't overdo it on extracting code into functions, specially if you call the function very often and copy a lot of bytes.
Calling a function doesn't copy anything, all Objects are passed as a reference.
And if a function is called very often the jit compiler will just inline it automatically, or use other optimisations that are appropriate.
Logged

Thaago

  • Global Moderator
  • Admiral
  • *****
  • Posts: 7174
  • Harpoon Affectionado
    • View Profile
Re: Coding Java for Noobs
« Reply #9 on: May 13, 2012, 06:15:14 PM »

Yup, wrapping common code in functions is the way to go. Much easier to read and much easier to catch errors. There are cases when it goes too far: when the function takes 10 arguments in order to handle all the different cases, it probably wants to be split up (if possible).

As a native python/fotran programmer I find the syntax of java/C very tiring to read, but thats just my own personal gripe...
Logged