E finalmente inizio a divertirmi un po' anch'io
Qui vi segnalo i problemi nel codice riportati dai miei potenti mezzi investigativi e discuteremo eventuali soluzioni. La soluzione non dev'essere implementata necessariamente da chi ha scritto il codice.
it.diamonds.java
SoundException.java
getALErrorString()
Complessita' Ciclomatica di 7 (al limite).
Codice:
private static String getALErrorString(int err)
{
switch(err)
{
case AL10.AL_INVALID_NAME:
return BASEALERRORMESSAGE + "AL_INVALID_NAME";
case AL10.AL_INVALID_ENUM:
return BASEALERRORMESSAGE + "AL_INVALID_ENUM";
case AL10.AL_INVALID_VALUE:
return BASEALERRORMESSAGE + "AL_INVALID_VALUE";
case AL10.AL_INVALID_OPERATION:
return BASEALERRORMESSAGE + "AL_INVALID_OPERATION";
case AL10.AL_OUT_OF_MEMORY:
return BASEALERRORMESSAGE + "AL_OUT_OF_MEMORY";
case AL10.AL_NO_ERROR:
default:
throw new IllegalArgumentException();
}
}
Consiglio di implementare una mappa e usare il codice d'errore come chiave per recuperare la stringa.
it.diamonds.java
Sound.java
initSource()
47 righe di codice.
Codice:
private void initSource(String fileName) throws SoundException,
SoundNotFoundException
{
int error;
if(!AL.isCreated())
throw new SoundException("Sound System not initialized");
buffer.position(0).limit(1);
AL10.alGenBuffers(buffer);
if((error = AL10.alGetError()) != AL_NO_ERROR)
{
throw new SoundException(error);
}
FileInputStream inputFile = null;
try
{
inputFile = new FileInputStream(soundDir + fileName
+ soundExtension);
}
catch(IOException e)
{
throw new SoundNotFoundException();
}
WaveData waveFile = WaveData.create(inputFile);
if(waveFile == null)
{
throw new SoundNotFoundException();
}
AL10.alBufferData(buffer.get(0), waveFile.format, waveFile.data,
waveFile.samplerate);
waveFile.dispose();
source.position(0).limit(1);
AL10.alGenSources(source);
if((error = AL10.alGetError()) != AL_NO_ERROR)
{
throw new SoundException(error);
}
AL10.alSourcei(source.get(0), AL_BUFFER, buffer.get(0));
AL10.alSourcef(source.get(0), AL_PITCH, 1.0f);
AL10.alSourcef(source.get(0), AL_GAIN, 1.0f);
AL10.alSource3f(source.get(0), AL_POSITION, 0.0f, 0.0f, 0.0f);
AL10.alSource3f(source.get(0), AL_VELOCITY, 0.0f, 0.0f, 0.0f);
if((error = AL10.alGetError()) != AL_NO_ERROR)
{
throw new SoundException(error);
}
wasLoaded = true;
}
Consiglio di applicare "Extract Method" per renderla piu' leggibile.
it.diamonds.engine
Texture.java e Display.java contengono 12 metodi. Se il numero cresce andranno suddivise.
Texture.java
loadTextureFromFile()
37 righe di codice.
Codice:
public void loadTextureFromFile(String fileName) throws TextureNotFoundException
{
try
{
IL.create();
ILU.create();
IntBuffer image = ByteBuffer.allocateDirect(4).order(
ByteOrder.nativeOrder()).asIntBuffer();
ilGenImages(image);
ilBindImage(image.get(0));
loaded=ilLoadImage(imageDir + fileName);
if (!isLoaded())
{
throw new TextureNotFoundException("Unable to find the texture "+imageDir + fileName);
}
if(ilGetInteger(IL_IMAGE_FORMAT) == IL_RGB)
{
ilConvertImage(IL_RGB, IL_BYTE);
format = GL_RGB;
}
else
{
if(ilGetInteger(IL_IMAGE_FORMAT) == IL_RGBA)
{
ilConvertImage(IL_RGBA, IL_BYTE);
format = GL_RGBA;
}
}
iluFlipImage();
height = ilGetInteger(IL_IMAGE_HEIGHT);
width = ilGetInteger(IL_IMAGE_WIDTH);
data = ilGetData();
}
catch(LWJGLException e)
{
System.err.println("Texture loading error due to +");
e.printStackTrace();
System.exit(0);
}
}