Ugly code of the day :

Ugly code of the day :

class function TFile.DoReadAllBytes(const Path: string): TBytes;
var
  LFileStream: TFileStream;
begin
  LFileStream := nil;
  try
    LFileStream := OpenRead(Path);
    SetLength(Result, LFileStream.Size);
    LFileStream.ReadBuffer(Pointer(Result)^, Length(Result));
  finally
    LFileStream.Free;
  end;
end;

Comments

  1. I have a 25Gb large file I'd like to try that on...

    ReplyDelete
  2. Nick Hodges was already clogging up my feed =P

    ReplyDelete
  3. Frédéric Hannes That was a pre-empting attempt ;-)

    ReplyDelete
  4. Eric Grange oh my goodness... Lars Fosdal  LOL*2!

    ReplyDelete
  5. Interesting. I am also using a prefix of "L" for local variables (starting with lower case, upper case now...) since years. Seems to be a more and more used coding style?!

    ReplyDelete
  6. No L for me, but a leading a in aName for arguments.
    Indexes: ix, jx, kx, or any ?x ...

    ReplyDelete
  7. Graeme Geldenhuys but we didn't work at the same company? ;-)

    ReplyDelete
  8. I only prefix class fields with F. In the past I've aways used lowercase for short variable names, but I recently moved to using caps and longer variable names, like "Idx" instead of "i". Resulting in my average array loop looking like:

    Len := Length(Arr);
    for Idx := 0 to Len - 1 do

    ReplyDelete
  9. Do you use properties even for simple field access?
    I tend to wrap them in a property
      MyField:Type read FMyField write FMyField;
    and really wish I could explicitly disable direct references to FMyField elsewhere in the same class.

    ReplyDelete
  10. Lars Fosdal
    I do. Sometimes I even do implement private properties :D It's always easier to change if you do need to add a property setter later.

    ReplyDelete
  11. Why not implement private properties if it results to clean code ?

    e.g.
    type
      TFoo = class
    ....
      private {or strict private }
        function GetBar: TBar;
        procedure SetBar (const Value: TBar);
        property Bar: TBar read GetBar write SetBar;
     end;

    where GetBar is 
      Result := someDataSource.DataSet.FieldByName('Bar').As....
    and SetBar is 
      someDataSource.DataSet.FieldByName('Bar').As.... := Value;

    in case of first stage of legacy code refactoring it really helps

    ReplyDelete
  12. The coding style that works well for us

    1. Give local variables as short names as possible. A typical variable name is 2 symbols length. For this function it will be fs instead of LFileStream.

      Why: The code is much more readable and easier to debug. Just try it, it works! A function has a narrow scope, and variables are most often self documented by the type and by the code. The trick is also to name similar variables in different functions similar way. E.g. we use e for Edge, p for point, p for previous, c for current, n for next, so cp: T3DPoint means CurrentPoint, ne: TEdge means NextEdge;

    2. Give fields the "f" prefix, nothing new here.

      Why: This way you distinguish fields from local variables. No need for the L.

    3. Always add self. before internal method calls and internal properties access. 

      Why: Properties may have side effects, so by prefixing them with self. you emphasize this.
        By prefixing own methods with self. you distinquish them from global functions. Methods usually operate on object fields, adding self. just makes it clear for a reader.

    ReplyDelete
  13. Apart from counters I never use short names, unless it is object references, and then it's usually org, cpy, nxt, prv, first or last. Reading Code Complete cured my short name delusion.

    ReplyDelete
  14. Using self - occasionally, but do you fully qualify library functions too?

    ReplyDelete
  15. Lars Fosdal I qualify global functions sometimes, when I want the Ctrl+[space] showed me the variants. For the methods, if a method operates on object fields using self is mandatory, if it doesn't, than self is optional depending on whether it improves readability or not.

    ReplyDelete
  16. Lars Fosdal I never expose class fields, so even for simple ones I use properties as public members, but I usually don't use properties in private or protected.

    ReplyDelete
  17. Something that followed me from Python back to Delphi is, explicit is better than implicit. I try to always use self, but have not completely followed through with unit names.

    ReplyDelete

Post a Comment