IDE Window: Extract Procedure/es: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: IDE Window: Extract Procedure/es
| Old | New | Line | |
|---|---|---|---|
| 1 | + | Resumen: "Extraer Procedimiento" toma las sentencias pascal seleccionadas y crea con ellas un nuevo procedimiento o método, situándolas en el cuerpo del mismo. Esta herramienta es útil para dividir procedimientos muy grandes, o para crear fácilmente un nuevo procedimiento a partir de algo de código. | |
| 2 | + | ||
| 3 | + | Ejemplo básico: | |
| 4 | + | ||
| 5 | + | ``` pascal | |
| 6 | + | procedure HacerAlgo; | |
| 7 | + | begin | |
| 8 | + | UsarHacerOtraCosa; | |
| 9 | + | end; | |
| 10 | + | ``` | |
| 11 | + | ||
| 12 | + | Selecciona la línea "UsarHacerOtraCosa;" y utiliza *Extraer Procedure* de la opción *Refactorizar* del menú contextual, o desde el menú principal con *Editar \> Extraer Procedimiento ...*; con cualquiera de ellas aparece un [diálogo](http://wiki.lazarus.freepascal.org/images/7/78/Lazarus_IDE_Extraer_Procedimiento.png) en el que se puede seleccionar el nombre y el tipo del procedimiento que se creará. En nuestro caso el resultado será este: | |
| 13 | + | ||
| 14 | + | ``` pascal | |
| 15 | + | procedure NuevoProcedimiento; | |
| 16 | + | begin | |
| 17 | + | UsarHacerOtraCosa; | |
| 18 | + | end; | |
| 19 | + | ||
| 20 | + | procedure HacerAlgo; | |
| 21 | + | begin | |
| 22 | + | NuevoProcedimiento; | |
| 23 | + | end; | |
| 24 | + | ``` | |
| 25 | + | ||
| 26 | + | Como se puede ver "NuevoProcedimiento" se crea con las sentencias seleccionadas dentro del su cuerpo y que la selección ha sido reemplazada por una llamada a "NuevoProcedimiento". | |
| 27 | + | ||
| 28 | + | #### Variables locales y parametros | |
| 29 | + | ||
| 30 | + | "Extraer Procedimiento" busca las variables utilizadas y automáticamente crea la lista de parámetros y las variables locales. Por ejemplo: | |
| 31 | + | ||
| 32 | + | ``` pascal | |
| 33 | + | procedure TForm1.HacerAlgo(var Una, Dos: integer); | |
| 34 | + | var | |
| 35 | + | i: Integer; // Comentario | |
| 36 | + | begin | |
| 37 | + | Una := Una + Dos; | |
| 38 | + | for i := Una to 5 do | |
| 39 | + | begin | |
| 40 | + | | | |
| 41 | + | end; | |
| 42 | + | end; | |
| 43 | + | ``` | |
| 44 | + | ||
| 45 | + | Selecciona el bucle *for* y crea, como antes, un procedimiento "NuevoProcedimiento". La variable local *i* se utiliza únicamente en la selección, por ello se mueve al nuevo procedimiento. La variable *Una* se utiliza también en el resto del código y por ello es transformada en un parámetro. | |
| 46 | + | ||
| 47 | + | Resultado: | |
| 48 | + | ||
| 49 | + | ``` pascal | |
| 50 | + | procedure NuevoProcedimiento(const Una: integer); | |
| 51 | + | var | |
| 52 | + | i: Integer; // Comentario | |
| 53 | + | begin | |
| 54 | + | for i := Una to 5 do begin | |
| 55 | + | | | |
| 56 | + | end; | |
| 57 | + | end; | |
| 58 | + | ||
| 59 | + | procedure TForm1.HacerAlgo(var Una, Dos: integer); | |
| 60 | + | begin | |
| 61 | + | Una := Una + Dos; | |
| 62 | + | NuevoProcedimiento(Una); | |
| 63 | + | end; | |
| 64 | + | ``` | |
| 65 | + | ||
| 66 | + | Como se puede ver la variable *i* ha sido movida a *NuevoProcedimiento* (Nota: incluyendo el comentario) lo mismo que *Una*. | |
| 67 | + | ||
| 68 | + | **Limitaciones**: Pascal es un potente lenguaje, pero no esperes que funcione correctamente con cualquier código. Limitaciones actuales/Cosas por hacer (*ToDos*): | |
| 69 | + | ||
| 70 | + | - ¿Comprobar los límites de un if? (check if selection bounds on statement bounds) | |
| 71 | + | - Sentencias "with" | |
| 72 | + | ||
| 73 | + | --- | |
| 74 | + | *From the Free Pascal wiki, page "IDE Window: Extract Procedure/es", as it stood on 2020-02-17 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 75 | + | Category: Castellano | |
| 76 | + | Category: Español |
