Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411508 Posts in 69374 Topics- by 58429 Members - Latest Member: Alternalo

April 26, 2024, 09:33:57 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Conditional compilation in Java?
Pages: [1]
Print
Author Topic: Conditional compilation in Java?  (Read 1878 times)
Jamie W
Level 1
*



View Profile WWW
« on: August 19, 2014, 04:22:05 AM »

One for the Java experts here:

Is there any kind of conditional compilation in Java? Like there is #ifdef / #endif in C - anything like that in Java?

Cheers,
Jamie
Logged

kamac
Level 10
*****


Notoriously edits his posts


View Profile
« Reply #1 on: August 19, 2014, 05:32:56 AM »

You might aswell google such simple questions :S

http://stackoverflow.com/questions/14208524/if-in-java-like-in-c-preprocessors
Logged

Jamie W
Level 1
*



View Profile WWW
« Reply #2 on: August 19, 2014, 05:54:16 AM »


Thanks.

I don't think I worded my question very well, it certainly doesn't do justice to the complexity of the problem I'm faced with. The solutions presented in the link, don't seem all that elegant to me. The idea of commenting out code? Please! Wink
Logged

jgrams
Level 3
***



View Profile
« Reply #3 on: August 19, 2014, 07:12:05 AM »

Would you like to try re-wording it? Because they do answer the question which you asked. Several of them say very clearly that there is no preprocessor in Java.

One of them even says:
Quote
Depending on your build system, you may be able to use a third-party preprocessor (you can find lots of them by searching for "java preprocessor"). Some examples are

...

Logged
MorleyDev
Level 0
***

"It is not enough for it to just work"


View Profile WWW
« Reply #4 on: August 19, 2014, 11:23:48 AM »

There are external 3rd party tools, but I've never been a fan of them in any language (it just never has felt like a clean solution to me). The more fundamental question is what are you actually trying to do with this, as there may be a cleaner and more elegant solution.
Logged

Average Software
Level 10
*****

Fleeing all W'rkncacnter


View Profile WWW
« Reply #5 on: August 19, 2014, 05:38:02 PM »

There's no reason why you can't just use a C preprocessor on your Java files.
Logged



What would John Carmack do?
kolrabi
Level 0
*



View Profile
« Reply #6 on: August 20, 2014, 03:41:02 AM »

Just use normal ifs. If the compiler knows the condition can never be true, no bytecode will be generated.
Logged
Jamie W
Level 1
*



View Profile WWW
« Reply #7 on: August 21, 2014, 04:14:15 AM »

There are external 3rd party tools, but I've never been a fan of them in any language (it just never has felt like a clean solution to me). The more fundamental question is what are you actually trying to do with this, as there may be a cleaner and more elegant solution.

I should have been more clear; you know you're in trouble when you can't clearly define the problem (or question) ... which to be honest, is probably winder in scope that simple 'what is a substitute Java mechanic for #define).

Previously, I had a class, called Vars. With members for each conditional requirement for Java code to be compiled (or executed). For example, Vars.condition1 or Vars.condition2

This solution just seemed really messy and ugly. I've only been using Java for maybe a year (been using C and C++ much longer), so am quite unfamiliar with good programming practices available in Java.

Someone mentioned the use of the keyword 'final'. I'm guessing that's used to show a value won't be changed; like a const? Maybe?

So anyway, the conditional compilation issue is just one of the problems I'm having. But there's more....

I'm having to maintain an Android project with multiple targets, using different store IAP code, and also Google Play Services or Amazon Game Circle or whatever, for leaderboards. How best to handle that? At the moment, I'm thinking some kind of class heirachy malarky.

Apologies if my original question was a bit vague. Smiley
Logged

InfiniteStateMachine
Level 10
*****



View Profile
« Reply #8 on: August 21, 2014, 10:42:42 AM »

yeah java kind of lends itself to a lot of design pattern malarky.

Your vars solution is probably as close are you are gonna get. Off the top of my head I'd probably do that as well then for something like IAP just make a common interface and do something like the following :


IIAP thing;
if (vars.apple)
{
 thing = new AppleIAP()
}
if (vars.android)
{
 thing = new AndroidIAP()
}


etc etc..

It sucks, if it was c/c++ I'd just do something with the buildscript to inject defines for each platform but I'm not sure that's possible with java. I havent used ant/maven more than absolutely necessary.
Logged

MorleyDev
Level 0
***

"It is not enough for it to just work"


View Profile WWW
« Reply #9 on: August 23, 2014, 12:45:36 PM »

Well even on C++ I'd typically find it more desirable to have multiple implementations that implement a base abstract class, and then have some factory that creates the desired implementation. Conditional compilation is just to remove ones that won't compile on certain platforms.

So it could look closer to:
Code:
class IAPFactory {
    IAP create(AppType type) {
        switch(type) {
            case AppType.Apple: return new AppleIAP();
            case AppType.Android: return new AndroidIAP();
            case ...
        }
    }
}

Splitting such things into multiple discrete implementations of a common interface winds up making the code less dense overall as code density is a bigger source of difficult-to-follow code and therefore bugs than LOC, and so (insert usual ramblings about the advantages of SOLID principles here).
« Last Edit: August 23, 2014, 12:52:45 PM by MorleyDev » Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic