With / Autocomplete lazarus / 9-Editor ---------------------------------------------------------------------- asdf · 2010-11-17 01:30 with button3 do begin . Autocomplete pops up overhere                                Will it be available in the near future version? ---------------------------------------------------------------------- theo · 2010-11-17 06:59 . does not make sense here. You can press Ctrl+Space to see the list. Warning: using the "with" statement is dangerous imho. ---------------------------------------------------------------------- ivan17 · 2010-11-17 14:19 Quote from: theo on November 17, 2010, 07:59:26 amWarning: using the "with" statement is dangerous imho. so are fork and knife.  eating with your hands is safer. ---------------------------------------------------------------------- theo · 2010-11-17 14:32 Quote from: ivan17 on November 17, 2010, 03:19:02 pmso are fork and knife.  eating with your hands is safer. I've never had any problems with table knives in my life, but I've seen lots of problems with with-statements. ;-) ---------------------------------------------------------------------- Leledumbo · 2010-11-17 15:16 It's a matter of preference, I use it when necessary but not all the time. ---------------------------------------------------------------------- asdf · 2010-11-17 15:33 I will destroy all "with" in every of my project as your kind warning. Thank you so much. Could you please provide some examples or describe the damages coming from "with"? ---------------------------------------------------------------------- theo · 2010-11-17 15:53 Quote from: asdf on November 17, 2010, 04:33:51 pmCould you please provide some examples or describe the damages coming from "with"? http://stackoverflow.com/questions/514482/is-delphi-with-keyword-a- bad-practice ---------------------------------------------------------------------- captian jaster · 2010-11-17 16:15 Quote from: asdf on November 17, 2010, 02:30:28 amwith button3 do begin . Autocomplete pops up overhere                                Will it be available in the near future version?                Sometimes developers(For some reason) use "With" even if its in one variable statement... Just put begin after the Do and end will automatically come up if there is no errors in your code... Quote from: asdf on November 17, 2010, 04:33:51 pmI will destroy all "with" in every of my project as your kind warning. Thank you so much. Could you please provide some examples or describe the damages coming from "with"? I rarely use the with code myself but in small records its not so bad. ---------------------------------------------------------------------- ivan17 · 2010-11-17 17:33 Quote from: asdf on November 17, 2010, 04:33:51 pmI will destroy all "with" in every of my project as your kind warning. Thank you so much. also destroy all for loops and use while with a counter too. Quote from: asdf on November 17, 2010, 04:33:51 pmCould you please provide some examples or describe the damages coming from "with"? with Form2 do Tag := Tag div (Tag - Tag); causes division by zero. very dangerous usage of with keyword. ok, idiot-mode off.  with-do construct is one of the nicest things object pascal provides. it allows you to type more readable code when setting multiple properties of an object: Code: [Select]    with Form2 do begin         Top := Screen.WorkAreaHeight - Height;  // omitted Form2  TWICE         Left := Screen.WorkAreaWidth - Width;   //  these two lines put your form into bottom right corner         ...     end; it allows you to typecast an object at the same time: Code: [Select]    with Sender as TListBox do begin         if ItemIndex = -1 then Exit;  // nothing selected?         //use methods and properties of your listbox here     end; it allows you to use a temporary anonymous object: Code: [Select]    with TRegistry.Create do try         RootKey := HKEY_CURRENT_USER;         OpenKey('\Software\Whatever\Whatever', true);         myoption1 := ReadBoolean('myoption1');         myoption2 := ReadBoolean('myoption2');         CloseKey;     finally         Free;     end; and once all that works for you, it allows you to write one with statement instead of several nested statements (note the enabled property of the listbox, not its strings): Code: [Select]    with ListBoxMain, Items do begin         Delete(2);  // deletes the third string         for i := 0 to Count-1 do whatever;  //nicer loop bounds         k := IndexOf(...);  // searches         if somecondition then             Enabled := false;   // disables the listbox     end; idiot-mode back on (lower setting): if you don't care to learn the rules of scope precedence then give up on programming altogether and switch to knitting because simple things could and will produce semantic errors (also operator precedence and flow control rules - all that varies between different languages). once you learn them, it is up to your personal preference whether you'll use the construct (and wonder how those poor c++ bastards live without it) or not. ---------------------------------------------------------------------- asdf · 2010-11-17 17:46 In the past, I always used it in VB6 but for the properties of controls only. ---------------------------------------------------------------------- Leledumbo · 2010-11-18 13:58 Quoteit allows you to use a temporary anonymous objectCertainly my favorite Use it properly and you'll get it's benefit. ---------------------------------------------------------------------- thierrybo · 2010-11-20 11:34 It's a pity we can not use . with pascal as in .net before statements. It's would be so easy for the compiler and the reader to know which statements will use the 'with' statement: Code: [Select]with TMyForm.Create do   .Width := Width + 2; //which width in this with is width? Now I know ---------------------------------------------------------------------- ivan17 · 2010-11-21 07:54 Quote from: thierrybo on November 20, 2010, 12:34:36 pmIt's a pity we can not use . with pascal as in .net before statements. It's would be so easy for the compiler and the reader to know which statements will use the 'with' statement: no, it would be ugly and confusing. and that is not the pascal way. Code: [Select]with TButton.Create(Self) do begin     ...     ...     Width := Self.Width div 2; //button width is now half the form's width     ... end; [/quote] ---------------------------------------------------------------------- captian jaster · 2010-12-22 21:30 Quote from: ivan17 on November 17, 2010, 06:33:26 pmQuote from: asdf on November 17, 2010, 04:33:51 pmI will destroy all "with" in every of my project as your kind warning. Thank you so much. also destroy all for loops and use while with a counter too. I don't understand... Whats wrong with For? Also.. Where did you learn so much about objects ---------------------------------------------------------------------- JuhaManninen · 2010-12-23 00:50 Yes, the proper usage is the key here. Theo gave a good stackoverflow link about the topic, did you guys read it? "with" can make a program difficult to read, difficult to debug and difficult to maintain. It can cause hard-to-track nasty bugs. The code inside "with" block can brake accidentally by a change somewhere else in the code-base. I tried to find the problems in THtmlPort (in CRC, original is David Baldwin's THtmlView). It has methods with even 3 nested levels of "with". To understand the code, the "with" statements must be carefully replaced with local variables. When just removing a "with" the code may still compile but its meaning changes completely. Uhhh... Debugging is almost impossible, too, because the debugger doesn't understand the short name, it has to be manually prefixed in the watch list. So, if you want to have readable and maintainable code, use "with" with care. Use only one active "with" object at time (no nesting). Also, if you need to use "self" to prevent "with" from messing the variables then don't use "with. Use a local variable with a short name instead. It makes the code more readable but keeps it short. This is from the stackoverflow link, one of the given answers. I couldn't write it better myself: --- The biggest danger of with, outside of pathological conditions like "with A, B, C, D" is that your code can silently change meaning with no notice to you. Consider this example: Code: [Select]with TFoo.Create try   Bar := Baz;   DoSomething(); finally   Free; end; You write this code knowing that Bar is a property of TFoo, and Baz is a property of the type containing the method which has this code. Now, two years later, some well-meaning developer comes in adds a Baz property to TFoo. Your code has silently changed meaning. The compiler won't complain, but the code is now broken. --- ---------------------------------------------------------------------- Leledumbo · 2010-12-23 02:42 One more thing, only use that when you know there would be no identifier clash (includes shadowing). Perhaps you've ever had this: Code: [Select]// DBQuery: TZQuery; function NameExists(const TableName,Name: String): Boolean; begin   with DBQuery do begin     SQL.Text := 'SELECT * FROM ' + TableName + ' WHERE name = :nama LIMIT 1';     Params.ParamByName('nama').AsString := Name;     Open;     Result := not EOF;   end; end; Now I wonder... why this procedure always returns true? OK, TZQuery also has a property called Name, so it shadows the Name parameter due to "with" semantics which makes the object's properties have higher visibility. ---------------------------------------------------------------------- JuhaManninen · 2010-12-23 09:32 ... and one more: Sometimes the extensive use of "with" is a sign of poorly structured code. Then it should be refactored, moving blocks of code to another class. Like: Code: [Select]procedure TMainStuff.Dosomething; begin   ...   with OtherStuffHelper do   begin     OtherStuffMethod(OtherStuffVar);     AnotherMethod(MainStuffVar);     ...   end;   ... end; It should be refactored to something like: Code: [Select]procedure TMainStuff.Dosomething; begin   ...   OtherStuffHelper.RefactoredOutMethod(MainStuffVar);   ... end; [.. maybe in another file: ..] procedure TOtherStuffHelper.RefactoredOutMethod(AParam: string); begin   OtherStuffMethod(OtherStuffVar);   AnotherMethod(AParam);   ... end; Juha