Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new type: Patterns.IO.FileSize #71

Open
jbatte47 opened this issue Jul 26, 2013 · 12 comments
Open

new type: Patterns.IO.FileSize #71

jbatte47 opened this issue Jul 26, 2013 · 12 comments
Assignees
Milestone

Comments

@jbatte47
Copy link
Member

AC: the following code has been added to Patterns along with features supplying coverage of each helper method

/// <summary>
///     Provides quick access to Windows-compliant file size measurements.
/// </summary>
public static class FileSize
{
   private const long _kb = 1024;
   private const long _mb = _kb*_kb;
   private const long _gb = _mb*_kb;
   private const long _tb = _gb*_kb;
   private const long _pb = _tb*_kb;

   /// <summary>
   ///  Gets the specified number of kilobytes.
   /// </summary>
   /// <param name = "kilobytes">The kilobytes.</param>
   /// <returns></returns>
   public static long FromKilobytes(int kilobytes)
   {
      return _kb*kilobytes;
   }

   /// <summary>
   ///  Gets the specified number of megabytes.
   /// </summary>
   /// <param name = "megabytes">The megabytes.</param>
   /// <returns></returns>
   public static long FromMegabytes(int megabytes)
   {
      return _mb*megabytes;
   }

   /// <summary>
   ///  Gets the specified number of gigabytes.
   /// </summary>
   /// <param name = "gigabytes">The gigabytes.</param>
   /// <returns></returns>
   public static long FromGigabytes(int gigabytes)
   {
      return _gb*gigabytes;
   }

   /// <summary>
   ///  Gets the specified number of terabytes.
   /// </summary>
   /// <param name = "terabytes">The terabytes.</param>
   /// <returns></returns>
   public static long FromTerabytes(int terabytes)
   {
      return _tb*terabytes;
   }

   /// <summary>
   ///  Gets the specified number of petabytes.
   /// </summary>
   /// <param name = "petabytes">The petabytes.</param>
   /// <returns></returns>
   public static long FromPetabytes(int petabytes)
   {
      return _pb*petabytes;
   }
}
@ghost ghost assigned jbatte47 Jul 26, 2013
@jbatte47
Copy link
Member Author

Each of the constants listed above should be made available as public constant fields. Let the consumer know what they're getting with each value:

long kb = FileSize.OneKilobyte;

@jbatte47
Copy link
Member Author

And now that I read it, it looks ridiculous, unless the type itself is ridiculous, and I'm just now picking up on it. If we expose the base FileSize values as public constants, then how is FileSize.FromMegabytes(5) any easier than just going with FileSize.OneMegabyte * 5? It's exactly as complex as consuming the method, and the method can just not exist. I'm gonna need some of The Tribe to chime in on this one because I'm a little torn about which way to go. One of the ways to go that I'm considering is to just drop this altogether since it's seeming less and less helpful all the time.

@blaw2422
Copy link
Contributor

I agree with the last thought. 1024 x N right?

Brandon Law
On Jan 9, 2014 6:10 PM, "John Batte" notifications@github.com wrote:

And now that I read it, it looks ridiculous, unless the type itself is
ridiculous, and I'm just now picking up on it. If we expose the base
FileSize values as public constants, then how is FileSize.FromMegabytes(5)any easier than just going with FileSize.OneMegabyte

  • 5? It's exactly as complex as consuming the method, and the method can
    just not exist. I'm gonna need some of The Tribe to chime in on this one
    because I'm a little torn about which way to go. One of the ways to go that
    I'm considering is to just drop this altogether since it's seeming less and
    less helpful all the time.


Reply to this email directly or view it on GitHubhttps://github.com//issues/71#issuecomment-31991214
.

@Wesam18
Copy link

Wesam18 commented Jan 10, 2014

I with dropping this altogether. The whole concept can be a part of metrics conversion class.

@chrislwade
Copy link
Contributor

Why not a bunch of extension methods so you could just do 5.megabytes() or 1.5.gigabytes().?

@jbatte47
Copy link
Member Author

^^^ I feel like Wesam's answer is almost spot-on, but a metric conversion class that concerned itself with anything other than file sizes would break SRP by doing so. I really love Chris's idea. It feels SO Ruby. I wonder why that is ;)

@jbatte47
Copy link
Member Author

Chris: if we did that, we'd have to offer extensions for all the numeric types. That's:

@chrislwade
Copy link
Contributor

I take it that none of those types inherit from Number or something similar? And realistically, how bad would it be to do so? Yes, it would be a ton of small methods but why is that a problem? It's not really about "conversion" of types as it is making things convenient for the programmer using the methods. 5.Megabytes() is much more natural coming from other languages when compared to 'FileSize.FromMegabytes(5)`.

@jbatte47
Copy link
Member Author

The common base type is ValueType, which is not descriptive enough and is
not a valid extension target. It's not bad to do this though. I like your
idea best so far. I just want to make sure we don't miss any of the numeric
types.
On Jan 9, 2014 7:04 PM, "Christopher Wade" notifications@github.com wrote:

I take it that none of those types inherit from Number or something
similar? And realistically, how bad would it be to do so? Yes, it would be
a ton of small methods but why is that a problem? It's not really about
"conversion" of types as it is making things convenient for the programmer
using the methods. 5.Megabytes() is much more natural coming from other
languages when compared to 'FileSize.FromMegabytes(5)`.


Reply to this email directly or view it on GitHubhttps://github.com//issues/71#issuecomment-31994242
.

@jbatte47
Copy link
Member Author

I'm taking this, and I'm gonna do both things: expose a FileSizes type with constants for one of each: KB, MB, GB, TB, PB. Also, a set of extensions returning long, double, or float values depending on the target type. E.g.:

3.5.Gigabytes().Should().BeGreaterThan(3500.Megabytes()); //which seems weird at first

@blaw2422
Copy link
Contributor

Stupid question. .. why do you need it? What were you doing that made
you want this? Just playing Devil's advocate. ..

Brandon Law
On Jan 9, 2014 9:08 PM, "John Batte" notifications@github.com wrote:

I'm taking this, and I'm gonna do both things: expose a FileSizes type
with constants for one of each: KB, MB, GB, TB, PB. Also, a set of
extensions returning long, double, or float values depending on the
target type. E.g.:

3.5.Gigabytes().Should().BeGreaterThan(3500.Megabytes()); //which seems weird at first


Reply to this email directly or view it on GitHubhttps://github.com//issues/71#issuecomment-31999289
.

@jbatte47
Copy link
Member Author

It's to fill a very small gap. It's similar to the gap that you'd feel if there were no TimeSpan.FromMinutes(5) to call. If you had to use new TimeSpan(0, 5, 0) every time, you'd want your convenience method back because it lets you express your wishes using the logical units you were already thinking in. This class's purpose was to allow the same experience to exist when assigning size limits or buffer lengths to things:

logOptions.Mode = LogModes.RollingFile;
logOptions.MaxFileSize = FileSize.FromMegabytes(5);

Only now, with the new syntax, it will look more like:

logOptions.MaxFileSize = 5.Megabytes();

Which I like a LOT better.

@jbatte47 jbatte47 removed this from the 3.11-beta.2 milestone Feb 5, 2014
@jbatte47 jbatte47 added this to the 4.2 milestone Aug 15, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants