Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (526)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (593)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2] 3
  ignore  |  Print  
  Code-style: Cringe pics  (Read 3119 times)
0 Members and 1 Guest are viewing this topic.
Offline Danny02
« Reply #30 - Posted 2014-06-04 18:47:29 »

I give you some code I had to deal with lately (legacy Java 1.3 or 4 code):
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public Double addTwoDouble(Double a, Double b)
{
  if(a != null)
    if(b != null)
      return new Double(a.doubleValue() + b.doubleValue());
    else
      return new Double(0.0);
  else
    return null;
//and the other way around
}


yes I know why they did it, but it is so cringe worthy not the less.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 273
Exp: 5 years



« Reply #31 - Posted 2014-06-04 18:49:32 »

I imagine it's projects like those that gave you your avatar?
Offline Herjan
« Reply #32 - Posted 2014-06-04 18:59:08 »

I see this way too often:
1  
2  
3  
4  
5  
if(blood == true){ //or just (!)blood
   blood = false;
}else if(blood == false){ // but exaggerating is mandatory in this thread (just like the semicolons)
   blood = true;
}

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline opiop65

JGO Kernel


Medals: 159
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #33 - Posted 2014-06-04 19:02:20 »

That's annoying, but not as bad. Many "newbies" to programming don't know the little tricks like that. I do cringe when I forget and sometimes do that though. Gets erased right away and I do it the shortcut way!

Offline Drenius
« Reply #34 - Posted 2014-06-04 19:02:47 »

1  
blood = !blood;

Shorter ideas?
Offline Herjan
« Reply #35 - Posted 2014-06-04 19:08:11 »

1  
blood = !blood;

Shorter ideas?

Actually, yes, Riven had one:

To reduce the redundancy and verbosity:
blood ^= true;
(it might look obscure, but your brain will soon recognize this as 'toggle')

Offline The Lion King
« Reply #36 - Posted 2014-06-04 19:17:19 »

I came across this piece of incredibly awesome looking code:

Its in some kind of c++ type language, but still fits the topic, so...
1  
2  
MyClass* myClass = new MyClass();
if(myClass) myClass.doSomething();


This makes sense, this is probably in more industrial or maybe code for an embedded device. If the new call does not work because you run out of memory it will return null. Well Malloc does at least i'm assuming new does too

-edit

I was wrong a new without enough space throws a new bad_alloc, but some compilers will just return null

"You have to want it more than you want to breath, then you will be successful"
Offline trollwarrior1
« Reply #37 - Posted 2014-06-04 19:21:11 »

Let me correct myself. Here is how the code might look in java.

1  
2  
3  
4  
5  
String paramStr = "Param received in method";

HashMap<String, String> map = new HashMap<String, String>();
if(map != null) map.put("Key", "Value");
if(paramStr != null) map.put("Key", paramStr);
Offline The Lion King
« Reply #38 - Posted 2014-06-04 19:27:40 »

Don't wan't to get too off topic, but in Java when you don't have enough memory for a new call it throws an OutOfMemoryException. In C++ it throws a bad_alloc. In C a malloc will return null and some C++ compilers, VC 06 being a good example, will also return null if there isn't enough memory on a new call.

In C:

1  
   someting* stuff = malloc(TooMuchMem);


will return null, so to check that the memory allocation was successful it is common to start the next line of code with if(stuff).

Also this is a C thing, doesn't have a reasonable equivalent in Java

-add/edit/reply

So you just added the parmStr thing after i posted this. That is different. In java this doesn't apply as i noted earlier. Your first post I replied to was an example in C++

Ill try to explain this better:

if(null) is the same as saying if(false).
a new call in some C++ compilers and a malloc in C returns null IF there isn't enough memory.
Therefore,
by doing
Pointer* ptr = malloc(sizeof(ptr));
or
Pointer* ptr = new Pointer();  //in SOME older compilers
if(ptr) ....

we are just making sure that the allocation of memory for pointer was successful, AKA that we didn't run out of memory.


"You have to want it more than you want to breath, then you will be successful"
Offline trollwarrior1
« Reply #39 - Posted 2014-06-04 19:32:42 »

Did you even read the code I wrote? It checks first line of the object is not null, and afterwards it doesn't care if its null. Its same as saying if(true) doSomething();
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline matheus23

JGO Kernel


Medals: 113
Projects: 3


You think about my Avatar right now!


« Reply #40 - Posted 2014-06-04 19:44:33 »

BTW, why is that not a compiler error ? why can you just open a block and close it ? with no keyword before it it makes no sense, why would this be allowed syntax :/

I used it back then. I thought it made things more readable:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
glBegin(GL_QUADS); 
{
    glVertex2f(...);
    glColor3f(...);
    glVertex2f(...);
    glColor3f(...);
    glVertex2f(...);
    glColor3f(...);
    glVertex2f(...);
    glColor3f(...);
}
glEnd();

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 273
Exp: 5 years



« Reply #41 - Posted 2014-06-04 19:53:41 »

BTW, why is that not a compiler error ? why can you just open a block and close it ? with no keyword before it it makes no sense, why would this be allowed syntax :/

One of the few "practical" reasons I've seen: http://stackoverflow.com/a/11971070/3517265

Using them so that you can copy-paste blocks of code, i.e. in tests. Also in switch statements, so you don't have to hoist variables out of the cases.
Offline Drenius
« Reply #42 - Posted 2014-06-04 19:58:41 »

These blocks do at least help to isolate variables defined inside them as usual afaik.

EDIT: Which is actually what the link is about.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 273
Exp: 5 years



« Reply #43 - Posted 2014-06-04 20:02:20 »

It would be interesting to see the effects of these blocks around variables on the assembly generated by the JIT, for those bit twiddling, performance mongering hackers.
I expect that most of the time they have no effect whatsoever, quite possible they're eliminated entirely by javac. Guess I'll go check.
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 833
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #44 - Posted 2014-06-04 20:12:32 »

Scopes don't exist in bytecode. (yes, i know about stackframes)

Javac will reuse localvar slots earlier when explictly limiting scope.

HotSpot couldn't care less about javac reusing localvars, it builds a completely unrecognizable new version from scratch, and again, and again, while inlining and optimizing.

TL;DR: no effect whatsoever on anything.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ags1

JGO Wizard


Medals: 72
Projects: 3
Exp: 5 years


Make code not war!


« Reply #45 - Posted 2014-06-04 20:38:37 »

Callsites like:

public void foo(String url, String path, String type, Boolean option1, Boolean option2, Boolean option3, Integer max)

And then in sample code:


//Just do this :-)
foo(null, null, "Thing", null, null, true, null);


wtf???

Offline BurntPizza

« JGO Bitwise Duke »


Medals: 273
Exp: 5 years



« Reply #46 - Posted 2014-06-04 20:45:58 »

TL;DR: no effect whatsoever on anything.

That's basically what I expected.
Offline Roquen
« Reply #47 - Posted 2014-06-05 04:29:21 »

Note: javac does nothing.  Nor do any of the mainstream java AOT compilers.  That's why a good decompiler can pretty much exactly reproduce your source from bytecodes.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 273
Exp: 5 years



« Reply #48 - Posted 2014-06-05 04:30:50 »

It does do some things, but yeah, the AST is unaffected.
Offline HeroesGraveDev

JGO Kernel


Medals: 300
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #49 - Posted 2014-06-05 05:03:31 »

The JVM does optimisation at/just before runtime, which would make sense since it knows the right details about the system.

Offline BurntPizza

« JGO Bitwise Duke »


Medals: 273
Exp: 5 years



« Reply #50 - Posted 2014-06-05 05:07:26 »

It does it during runtime too, sometimes multiple times as it finds out more information and can produce better codepaths. I believe you can set the threshold at which the JIT will consider code "hot" too, which is nifty sometimes.
There's a lot of good info on this wiki: https://wikis.oracle.com/display/HotSpotInternals/PerformanceTechniques
Online NegativeZero

JGO Knight


Medals: 36
Projects: 2


Zero but not.


« Reply #51 - Posted 2014-06-05 05:18:30 »

Watching Ray's livestream, I've noticed he does this:
1  
}else{
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 273
Exp: 5 years



« Reply #52 - Posted 2014-06-05 05:19:38 »

Well I mean he is the resident crazyman.
Offline Roquen
« Reply #53 - Posted 2014-06-05 13:07:42 »

The JVM does optimisation at/just before runtime, which would make sense since it knows the right details about the system.
At "compile" time.  The details of when a compile occurs is VM, version and options dependent.  Normally the "we've done nothing at AOT bytecode" is interpreted a rather large number of time before a compile will occur.  All this kinda stuff is unspecified (and for good reason).

Quote
multiple times as it finds out more information and can produce better codepaths
Yes the number of time a piece of code will be compiled is likewise VM, version and option dependent.  I'm pretty sure that ATM the only time hotspot will recompile is if an assumption is proven wrong (something else is tickling the back of my brain but it's not coming to the surface).

So it's not a good idea to turn down (too far) the number of times a method is interpreted before compiling since you'll be cutting short the collection of data for "profile guided" optimizations.

Quote
I believe you can set the threshold at which the JIT will consider code "hot" too, which is nifty sometimes.
http://www.java-gaming.org/topics/hotspot-options/32055/view.html
Offline atombrot
« Reply #54 - Posted 2014-06-05 15:11:55 »

I had to maintain a js project once that had a very special way of doing things. Can't post actual code from the project, but it looked something like this:

1  
2  
3  
4  
5  
6  
7  
8  
for(var arrIndex = 0; arrIndex < arr.length; arrIndex++){
   for(var arrFooIndex = 0; arrFooIndex < arr[arrIndex].length; arrFooIndex++){
      for(var arrFooBarIndex = 0; arrFooBarIndex < arr[arrIndex][arrFooIndex].length; arrFooBarIndex++){
         arr[arrIndex][arrFooIndex][arrFooBarIndex]['Property'] = 'yay';
         //Lets loop some more ;)
      }
   }
}


Now imagine this going down up to 10 levels of nesting and add a bit of business logic into each layer, often referencing the objects of other layers. The project was very well documented and structured, but things like this required that you had to be extra careful with every little change. It didn't help that there was very much redundancy, so often you had to change multiple loop trees, if you needed to change something.

Another thing that annoys me very much (it might be a bit off topic for code style) are date parsers in javascript. Somehow people resist using already established solutions and try to roll their own. And this is really hard to get right, especially if you you take different locales into account. In one project I had to help out with a problem on the frontend with datepickers, I found three different implementations (let's call them hacks) of a dateParse method, each one used in different places of the application and one of them actually returned the correct dates sometimes... Fun thing is that this application ran on a platform, that had a js api and provided full support for localized dates and some other nice features. But hey, why not roll your own?

I once read a nice quote, sadly I can't remember where I read it, but it fits nicely for this post:

Quote
Write your code like if the person maintaining your source is a total psycho and knows your home address.
Offline Rayvolution

« JGO Spiffy Duke »


Medals: 222
Projects: 2
Exp: 1 year


Resident Crazyman


« Reply #55 - Posted 2014-06-05 18:52:28 »

Watching Ray's livestream, I've noticed he does this:
1  
}else{


haha, I don't even know where I picked that up. for whatever reason it just felt natural. I think it's a sideeffect to me setting up my if/else statements by typing this one liner real quick then filling it in and spacing it out as I code a method:
1  
if(){}else{}


Actually, one thing I do that I know makes some people cringe is how I "compress" my getters and setters at the end of my class files into one line. But I've always liked it this way;

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
   public int getTileId(int tileX, int tileY, int layer){return mapArray[tileX][tileY][layer];}
   public String getTileProperty(int tileID, String property, String def){return mtl.getTileProperty(tileID, property, def);}
   public Image getTileImage(int x, int y, int l){return mtl.getTiles().get(mapArray[x][y][l]);}
   public Image getCollisionImage(int x, int y, int l){return mtl.getTilesCollision().get(mapArray[x][y][l]);}
   public int getBrushSize(){return brushSize;}
   public void setbrushSizeUp(){if (brushSize < 20)brushSize += 1;}
   public void setbrushSizeDown(){if (brushSize > 1)brushSize -= 1;}
   public int getLayer(){return layer;}
   public void setLayer(int l){layer = l;}
   public void setGridMode(){gridView = !gridView;}
   public boolean getGridMode() {return gridView;}  
   public void lockAccentMode(){accentModeLock = !accentModeLock;}
   public void accentModeOn(){accentMode = true;}
   public void accentModeOff(){accentMode = false;}


this ones probably the worst of the bunch:
1  
2  
   public void setbrushSizeUp(){if (brushSize < 20)brushSize += 1;}
   public void setbrushSizeDown(){if (brushSize > 1)brushSize -= 1;}

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Survival Sim/Builder/Roguelike!
LIVE-STREAMING DEVELOPMENT: http://www.twitch.tv/SG_Rayvolution
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 273
Exp: 5 years



« Reply #56 - Posted 2014-06-05 19:05:02 »

That's it, we need an intervention.
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 833
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #57 - Posted 2014-06-05 19:11:33 »

I don't see the need for the on/off setters, unless reflection is used to tie functionality to UI. Having said that, eww!

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline kpars

JGO Kernel


Medals: 107
Projects: 4
Exp: 4 years


Extreme Typist.


« Reply #58 - Posted 2014-06-05 19:19:08 »

Not necessarily code-style, but this is a thing I do which some may find really cringy.


(Yes, I know, I have a few warnings, I'll fix those later Tongue)

I don't have time to organise my code into fancy little packages. I'm sloppy when it comes to that. There are more important things to organise, like my mind. Roll Eyes

I also format my code like this:
1  
2  
3  
4  
private void foo(double bar)
{
    System.out.println(bar);
}

I come from the dark land of C.

- Jev

Offline Roquen
« Reply #59 - Posted 2014-06-05 19:23:36 »

I just wrote an exception that passes back a method to call at a catch site.
Pages: 1 [2] 3
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

toopeicgaming1999 (72 views)
2014-11-26 15:22:04

toopeicgaming1999 (62 views)
2014-11-26 15:20:36

toopeicgaming1999 (15 views)
2014-11-26 15:20:08

SHC (29 views)
2014-11-25 12:00:59

SHC (27 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (27 views)
2014-11-24 19:59:16

trollwarrior1 (40 views)
2014-11-22 12:13:56

xFryIx (78 views)
2014-11-13 12:34:49

digdugdiggy (56 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!