• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

java to c++ conversion of Configuration.DEBUG

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings,
I am trying to translate following java code into c++ code.

I do not understand what Configuration.DEBUG is doing. What is the c++ equivalent for it? Will omitting it affect " if then " performance significantly?
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'ts kind of hard to say, since we don't know what kind of object "Configuraiton" is. My GUESS is that it has read a configuration file, and holds all the various things from it. One of which is a boolean flag called "DEBUG" that you set depending on what you want to see logged. so without changing your CODE base, you can turn pieces of it on or off by editing a file that gets read in.

I'm not entirely sure how you'd do it in C++, but i'm sure there are examples around you could find.
 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It's a precondition check to ensure that argument "n" is always in the range -2047 to 2047.
An example of design by contract, where each method ensures that the arguments it receives from calling methods are valid.

Configuration.DEBUG is just an application defined variable which is likely set to true only during development phases, to uncover any buggy caller who passes in |n|>=2048.
When those bugs are found and corrected, the flag is set to false and pushed into production.

It's a good programming practice, and in my opinion, you should retain it even in the C++ version. These are called "assertions" btw - search for that term if you're not familiar with them.
One common technique to implement assertions in C++ would be something like

NDEBUG macro is doing the same thing as java's Configuration.DEBUG. If it's not defined, the assert condition is evaluated. If it's defined, the condition is not evaluated.

One thing you need to check is whether Configuration.DEBUG value is hardcoded somewhere in code at compile time, or read at runtime from some properties file or other configuration file.
If it's set at runtime, then I suppose the C++ version too should be designed to read the flag at runtime and use it, instead of relying on NDEBUG.

 
Karthik Shiraly
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, sorry about butting into the thread, Fred. There were no replies when I started writing one.
 
Marshal
Posts: 80145
418
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you have done nothing wrong.
 
Adam Szewczyk
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. You helped me solve the problem.
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karthik Shiraly wrote:Ah, sorry about butting into the thread, Fred. There were no replies when I started writing one.


yeah - no apologies necessary. You gave a much more thorough answer than mine - and that is always welcome.
 
Rancher
Posts: 280
VI Editor C++ Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adam Szewczyk wrote:Greetings,
I am trying to translate following java code into c++ code.
(...)
I do not understand what Configuration.DEBUG is doing. What is the c++ equivalent for it? Will omitting it affect " if then " performance significantly?



While your specific question regarding 'what Configuration.DEBUG is doing' was addressed and answered, just a note that unlike Java that requires an IEEE 754 based floating point representation, C++ does not require so. Also, C++ does not have functions such as double-to-long functions and one typically would have to use a cast. The behavior of the cast is also left undefined by the language (since the size of a double and the size of a long could be different).

For most PC based compilers, such matters are not an issue though and your 'translation' from Java to C++ would appear to work just fine.
 
No one can make you feel inferior without your consent - Eleanor Roosevelt. tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic