Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Methods which wrap a single method

I think I'm going mad, someone please reassure me.

public class MyFile
{   
    public static byte[] ReadBinaryFile(string fileName)
    {
        return File.ReadAllBytes(fileName);
    }

    public static void WriteBinaryFile(string fileName, byte[] fileContents)
    {
        File.WriteAllBytes(fileName, fileContents);
    }
}

People keep on adding code like the above in to our code base, surely this is wrong and horrid and I am doing the world a favour by deleting it and replacing all (or both in this case...) references to it with the internal code.

Is there any real justification for this kind of thing? Could I be missing the bigger picture? We are quite YAGNI-centric in our team and this seems to fly in the face of that. I could understand if this was the beginnings of something more, however this code has lay dormant for many many months until I tripped over it today. The more I search the more I find.

like image 420
gingerbreadboy Avatar asked Feb 24 '10 14:02

gingerbreadboy


People also ask

How do you wrap a method in Java?

wrap(int[] array, int offset, int length) The wrap() method wraps an int array into a buffer. The new buffer will be backed by the given int array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new buffer's capacity will be array.

What is a static method in Java?

A static method in Java is a method that is part of a class rather than an instance of that class. Every instance of a class has access to the method. Static methods have access to class variables (static variables) without using the class's object (instance). Only static data may be accessed by a static method.

What does it mean to wrap a class?

A Wrapper class is a class whose object wraps or contains primitive data types. When we create an object to a wrapper class, it contains a field and in this field, we can store primitive data types. In other words, we can wrap a primitive value into a wrapper class object. Need of Wrapper Classes.

Why do we need wrapper classes in Java?

Why are wrapper classes needed in Java? Wrapper classes are fundamental in Java because they help a Java program be completely object-oriented. The primitive data types in java are not objects, by default. They need to be converted into objects using wrapper classes.


1 Answers

As written, the class/methods are garbage. However, I can see a situation in which a similar pattern might be used legitimately:

public interface IFileStorage
{
    byte[] ReadBinaryFile(string fileName);
    void WriteBinaryFile(string fileName, byte[] fileContents);
}

public class LocalFileStorage : IFileStorage { ... }

public class IsolatedFileStorage : IFileStorage { ... }

public class DatabaseFileStorage : IFileStorage { ... }

In other words, if you wanted to support different kinds of storage, then you might actually wrap very simple methods in order to implement a generic abstraction.

As written, though, the class doesn't implement any interface, and the methods are static, so it's pretty much useless. If you're trying to support the above pattern, then refactor; otherwise, get rid of it.

like image 172
Aaronaught Avatar answered Oct 06 '22 05:10

Aaronaught