Jump to content


Photo

Rate My Code - Forum Game


  • Please log in to reply
12 replies to this topic

#1 G. Sperotto

G. Sperotto
  • Members
  • 471 posts

Posted 16 April 2012 - 05:14 PM

Hi there :)

Have you ever wondered whether your coding style would be considered organized, creative, or just a pain in the stomach if another programmer where to ever rate it? Have you ever scripted something and ended up not believing that you actually had the brains to do it?

Well, here's the chance to have your code rated for real!

How it works: Post your shiny work of art (up to 50 lines long - You can post just part of a script) and let the next user to post judge it on these 5 criteria:

1 - Creativity.
2 - Readability.
3 - Organization.
4 - Efficiency.
5 - Use of Deep AHK programming knowledge.

Rating should be on a 0-10 scale. 0 for worst, 10 for best and 5 for your everyday "average code".

Example:

I have coded this small function to have input numbers correctly evaluated by AHK regardless of chosen separators for the thousand houses and floating points (whether dots or commas). It should change any input number to 0000.00 format (for correct AHK evaluation). It's specially aimed to be used on those applications that have to be ready for foreign country users that could input data on either the american format (1,000.00) or a foreign (1.000,00) format.

AllowAHKToWorkThisNumber(ByRef Value, Houses = 2) 
{
   stringreplace Value, Value, %A_Space%,, All
   stringgetpos commapos, Value, `,
   stringgetpos dotpos, Value, .
   if (dotpos > commapos and dotpos > 0 and commapos > 0)
   {
      stringreplace Value, Value, `,,, All
   }
   if Value is not number
   {
      stringreplace Value, Value, .,, All
      stringreplace Value, Value, `,,., All
   }
   Value := Round(Value, Houses)
   return Value
}

You can rate it and post your judgement alongside a code to be evaluated :D

#2 fragman

fragman
  • Members
  • 1591 posts

Posted 16 April 2012 - 09:01 PM

I have coded this small function to have input numbers correctly evaluated by AHK regardless of chosen separators for the thousand houses and floating points (whether dots or commas). It should change any input number to 0000.00 format (for correct AHK evaluation). It's specially aimed to be used on those applications that have to be ready for foreign country users that could input data on either the american format (1,000.00) or a foreign (1.000,00) format.

AllowAHKToWorkThisNumber(ByRef Value, Houses = 2) 
{
   stringreplace Value, Value, %A_Space%,, All
   stringgetpos commapos, Value, `,
   stringgetpos dotpos, Value, .
   if (dotpos > commapos and dotpos > 0 and commapos > 0)
   {
      stringreplace Value, Value, `,,, All
   }
   if Value is not number
   {
      stringreplace Value, Value, .,, All
      stringreplace Value, Value, `,,., All
   }
   Value := Round(Value, Houses)
   return Value
}

[*:xtefduay]Creativity: 5 - It's a pretty generic task and carried out more or less straight-forward.
[*:xtefduay]Readability: 4 - Case of the commands could need some work, some or spelled small, some Capital. CamelCase is best for commands consisting of more than one word and one should stick to small or Capital word beginnings IMO.
[*:xtefduay]Organization: 5 - As I said, it's pretty straightforward, not much about it. A comment explaining the method used here would be nice if you want to grasp it without thinking about it, but it's not absolutely necessary here.
[*:xtefduay]Efficiency: 8 - Not tested, but I expect it to run fast enough. In general this should be timed if it is critical.
[*:xtefduay]Use of Deep AHK programming knowledge: 5 - More than a beginner would write. However, you might also want to consider regular expressions. In that case you might want to test the speed though if it really is performance critical

I'll post a really ugly piece of code. It's much longer than your limit, so just read what you want from it. The nasty code is in the Convert() function and the functions that are called from it. It's used to convert C# source files generated by the Visual Studio GUI designer into AHK code that works with my CGUI library. The code is really ugly but it does its job. It really needs a rewrite though and I'm not proud of it.
gui := new CSharpGuiConverter()
#include <CGUI>
#include <Regex>
Class CSharpGuiConverter Extends CGUI
{
   label1         := this.AddControl("Text", "label1", "x12 y15 w53 h13", "Input File:")
   txtInput       := this.AddControl("Edit", "txtInput", "x71 y14 w274 h20", "")
   btnInput    := this.AddControl("Button", "btnInput", "x351 y12 w36 h23", "...")
   label2         := this.AddControl("Text", "label2", "x12 y41 w61 h13", "Output File:")
   txtOutput      := this.AddControl("Edit", "txtOutput", "x71 y40 w274 h20", "")
   btnOutput   := this.AddControl("Button", "btnOutput", "x351 y38 w36 h23", "...")
   btnConvert  := this.AddControl("Button", "btnConvert", "x15 y66 w120 h23", "Convert")
   btnRun      := this.AddControl("Button", "btnRun", "x141 y66 w120 h23", "Run Converted File")
   btnEdit        := this.AddControl("Button", "btnEdit", "x267 y66 w120 h23", "Edit Converted File")
   __New()
   {
      IniRead, in, %A_ScriptName%.ini,Settings, In, %A_Space%
      IniRead, out, %A_ScriptName%.ini,Settings, Out, %A_Space%
      
      this.txtInput.Text := in
      this.txtOutput.Text := out
      
      this.btnConvert.Enabled := FileExist(in) && strlen(out) > 1    
      this.btnRun.Enabled := 0         
      this.btnEdit.Enabled := 0
      
      this.height := 116
      this.Title := "C# GUI Converter"
      this.Width := 401
      this.DestroyOnClose := true
      this.Show()    
   }
   btnInput_Click()
   {
      ;~ global CFileDialog
      FileDialog := new CFileDialog("Open")
      FileDialog.Filter := "*.cs"
      Text := this.txtInput.Text
      SplitPath, Text, Filename, FileDir
      if(FileDir)
         FileDialog.InitialDirectory := FileDir
      if(Filename)
         FileDialog.Filename := Filename
      if(FileDialog.Show())
         this.txtInput.Text := FileDialog.Filename
   }
   btnOutput_Click()
   {
      ;~ global CFileDialog
      FileDialog := new CFileDialog("Save")
      FileDialog.Filter := "*.ahk"
      Text := this.txtInput.Text
      SplitPath, Text, Filename, FileDir
      if(FileDir)
         FileDialog.InitialDirectory := FileDir
      if(Filename)
         FileDialog.Filename := Filename
      if(FileDialog.Show())
         this.txtOutput.Text := FileDialog.Filename
   }
   btnConvert_Click()
   {
      if(InStr(FileExist(this.txtInput.Text), "D") && InStr(FileExist(this.txtOutput.Text), "D"))
      {
         Loop, % this.txtInput.Text "\*.cs", 0, 0
         {
            if(InStr(A_LoopFileName, ".Designer.cs"))
            {
               ;~ MsgBox % "convert " A_LoopFileLongPath " -> " this.txtOutput.Text "\" SubStr(A_LoopFileName, 1, StrLen(A_LoopFileName) - 11) ".ahk"
               this.Convert(A_LoopFileLongPath, this.txtOutput.Text "\" SubStr(A_LoopFileName, 1, StrLen(A_LoopFileName) - 12) ".ahk")
            }
         }
      }
      else
      {
         this.Convert(this.txtInput.Text, this.txtOutput.Text)
         this.ConvertedFile := this.txtOutput.Text
         this.btnRun.Enable()
         this.btnEdit.Enable()
      }
   }
   btnRun_Click()
   {
      run % this.ConvertedFile
   }
   btnEdit_Click()
   {
      run % "*Edit " this.ConvertedFile
   }
   txtInput_TextChanged()
   {
      if(FileExist(this.txtInput.Text) && this.txtOutput.Text)
         this.btnConvert.Enable()
      else
         this.btnConvert.Disable()
   }
   txtOutput_TextChanged()
   {
      if(FileExist(this.txtInput.Text) && this.txtOutput.Text)
         this.btnConvert.Enable()
      else
         this.btnConvert.Disable()
   }
   PreClose()
   {
      IniWrite, % this.txtInput.Text, %A_ScriptName%.ini, Settings, In
      IniWrite, % this.txtOutput.Text, %A_ScriptName%.ini, Settings, Out
      ExitApp
   }
   ReadTabs(ByRef InputFile, Controls, TabPages)
   {
      ;Collect TabPage variables and create TabPages array for each tab control
      Loop, Parse, InputFile, `n, %A_Space%%A_Tab%
      {
         if(InStr(A_LoopField, "private System.Windows.Forms."))
         {
            CSharptype := Regex.MatchSimple(A_LoopField, "type", "\.Forms\.(?P<type>.*?) (?P<name>.*?)\;")
            name := Regex.MatchSimple(A_LoopField, "name", "\.Forms\.(?P<type>.*?) (?P<name>.*?)\;")
            if(CSharptype && name)
            {
               if(CSharpType = "TabControl")
                  Controls[Name].TabPages := {}
               else if(CSharpType = "TabPage")
                  TabPages[Name] := {}
            }
         }
      }
      ;Scan for tab pages and associate controls with tab control tabs
      ;Find tabpage->tab control relationship
      Loop, Parse, InputFile, `n, %A_Space%%A_Tab%
      {
         if(InStr(A_LoopField, ".Controls.Add("))
         {
            TabControl := Regex.MatchSimple(A_LoopField, "Name", "this\.(?P<Name>.*?)\.Controls\.Add\(")
            if(Controls[TabControl].Type = "Tab")
            {
               TabPage := {Name : Regex.MatchSimple(A_LoopField, "Control", "\(this\.(?P<Control>.*?)\);")}  ;this.tabPage1.Controls.Add(this.label2);
               Controls[TabControl].TabPages.Insert(TabPage)
            }
         }
         ;~ this.tabPage2.Controls.Add(this.txtTags);
      }
      ;Find tabpage text
      Loop, Parse, InputFile, `n, %A_Space%%A_Tab%
      {
         if(InStr(A_LoopField, ".Text = """)) ;this.tabPage1.Text = "General";
         {
            TabPage := Regex.MatchSimple(A_LoopField, "Name", "this\.(?P<Name>.*?)\.Text = ")
            for name, control in Controls
               for index, tabpage2 in control.TabPages
               if(TabPage2.Name = TabPage)
               {
                  TabPage2.Text := Regex.MatchSimple(A_LoopField, "Text", " = ""(?P<Text>.*?)"";")
                  break 2
               }
         }
      }
      
      ;Find control->tabpage relationship
      Loop, Parse, InputFile, `n, %A_Space%%A_Tab%
      {
         if(InStr(A_LoopField, ".Controls.Add(")) ;this.tabPage1.Controls.Add(this.label2);
         {
            TabPage := Regex.MatchSimple(A_LoopField, "Name", "this\.(?P<Name>.*?)\.Controls\.Add\(")
            if(IsObject(TabPages[TabPage]))
            {
               SubControl := Regex.MatchSimple(A_LoopField, "Control", "\(this\.(?P<Control>.*?)\);")  
               for Name, Control in Controls
                  if(IsObject(Control.TabPages))
                     for index, TabPage2 in Control.TabPages
                        if(TabPage2.Name = TabPage)
                        {
                           Controls[SubControl].TabControl := Control                           
                           Controls[SubControl].Tab := index
                           break 2
                        }              
            }
         }
         ;~ this.tabPage2.Controls.Add(this.txtTags);
      }
   }
   ReadGroupBoxes(ByRef InputFile, Controls)
   {
      for name, control in Controls
         if(control.Type = "GroupBox")
            control.Controls := {}
      Loop, Parse, InputFile, `n, %A_Space%%A_Tab%
      {
         if(InStr(A_LoopField, ".Controls.Add("))
         {
            GroupBoxControl := Regex.MatchSimple(A_LoopField, "Name", "this\.(?P<Name>.*?)\.Controls\.Add\(")
            if(Controls[GroupBoxControl].Type = "GroupBox")
            {
               SubControl := Regex.MatchSimple(A_LoopField, "Control", "\(this\.(?P<Control>.*?)\);") ;this.tabPage1.Controls.Add(this.label2);
               Controls[GroupBoxControl].Controls.Insert(SubControl)
               Controls[SubControl].GroupBox := 1
               Controls[SubControl].AccessString := "this." GroupBoxControl ".Controls." Controls[SubControl].Name
            }
         }
      }
   }
   Convert(InPath, OutPath)
   {
      ;~ global Regex
      FileRead, InputFile, % "*t " InPath
      start := InStr(InputFile, "partial class ") + StrLen("partial class ")
      Class := SubStr(InputFile, start, InStr(InputFile, "`n", 0, start) - start)
      Controls := [] ;array storing control definitions
      Window := {Events : {}} ;Object storing window properties
      StartString := "private System.Windows.Forms."
      EndString := ";"
      ;Get a list of controls
      Loop, Parse, InputFile, `n, %A_Space%%A_Tab%
      {
         line := A_LoopField
         if(InStr(line, "private System.Windows.Forms."))
         {
            CSharptype := Regex.MatchSimple(line, "type", "\.Forms\.(?P<type>.*?) (?P<name>.*?)\;")
            name := Regex.MatchSimple(line, "name", "\.Forms\.(?P<type>.*?) (?P<name>.*?)\;")
            if(CSharptype && name)
            {
               SupportedControls := { TextBox : "Edit", Label : "Text", Button : "Button", CheckBox : "CheckBox", PictureBox : "Picture", ListView : "ListView", ComboBox : "ComboBox", ListBox : "ListBox", TreeView : "TreeView", GroupBox : "GroupBox", RadioButton : "Radio", TabControl : "Tab", LinkLabel : "SysLink", StatusStrip : "StatusBar", NumericUpDown : "Edit"}
               type := SupportedControls[CSharptype]
               if(type)
               {
                  Control := {Type : Type, Name : name, Events : {}}
                  if(CSharpType = "NumericUpDown")
                  {
                     Control.UpDown := true
                     Control.Min := 0
                     Control.Max := 100
                  }
                  Controls[Control.Name] := Control
               }
            }
         }
      }
      ;Parse all control and gui properties
      Loop, Parse, InputFile, `n, %A_Space%%A_Tab%
      {
         line := A_LoopField
         if(InStr(line, "// ") && !InStr(line, "///") && strlen(line) > 4) ;Start of control section is marked by //
         {
            found := false
            for Name, Control in Controls
            {
               if(line = "// " Control.Name) ;Start of new control section
               {
                  CurrentControl := Control
                  found := true
                  break
               }
            }
            if(!found)
               CurrentControl := ""
            if(!found && strLen(line) > 5 && !InStr(line, "///"))
            {
               if(!found && InStr(line, "// " Class))
               {
                  CurrentControl := "Window"
               }
            }
         }
         if(CurrentControl = "Window")
         {
            if(InStr(line, " =")) ;window property assignments
            {
               if(InStr(line, "this.ClientSize"))
               {
                  Width := Regex.MatchSimple(line, "width", "\.Size\((?P<width>\d+),.*?(?P<height>\d+)")
                  Height := Regex.MatchSimple(line, "height", "\.Size\((?P<width>\d+),.*?(?P<height>\d+)")
                  if(width)
                     Window.Width := width
                  if(height)
                     Window.height := height
               }
               else if(InStr(line, "this.MaximizeBox"))
                  Window.MaximizeBox := InStr(line, "true")
               else if(InStr(line, "this.MinimizeBox"))
                  Window.MinimizeBox := InStr(line, "true")
               else if(InStr(line, "this.TopMost"))
                  Window.AlwaysOnTop := InStr(line, "true")
               else if(InStr(line, "this.Enabled"))
                  Window.Enabled := InStr(line, "true")
               else if(InStr(line, "this.Autosize"))
                  Window.AutoSize := InStr(line, "true")
               else if(InStr(line, "FormBorderStyle."))
               {
                  if(InStr(line, "ToolWindow;"))
                     Window.ToolWindow := 1
                  if(InStr(line, "Sizable"))
                     Window.Resize := 1
               }
               else if(InStr(line, "this.Text"))
                  Window.Title := Regex.MatchSimple(line, "text", """(?P<text>.*)""")
            }
            else if(InStr(line, "EventHandler(")) ;GUIs have different event handler classes
            {
               if(InStr(line, "this.DragDrop"))
                  Window.Events.Insert("DropFiles()")
               else if(InStr(line, "this.FormClosing"))
                  Window.Events.Insert("PreClose()")
               else if(InStr(line, "this.FormClosed"))
                  Window.Events.Insert("PostDestroy()")
            }
         }
         else if(IsObject(CurrentControl)) ;Process control property assignments
         {
            Handled := false
            if(InStr(line, " =")) ;control property assignments
            {
               Handled := true
               if(InStr(line, "this." CurrentControl.Name ".Size")) ;Some basic ones first
               {
                  Width := Regex.MatchSimple(line, "width", "\.Size\((?P<width>\d+),.*?(?P<height>\d+)")
                  Height := Regex.MatchSimple(line, "height", "\.Size\((?P<width>\d+),.*?(?P<height>\d+)")
                  if(width)
                     CurrentControl.Width := width
                  if(height && CurrentControl.Type != "ComboBox") ;ComboBox/DropDownList use it to limit their drop down list
                     CurrentControl.height := height
               }
               else if(InStr(line, "this." CurrentControl.Name ".Location"))
               {
                  x := Regex.MatchSimple(line, "x", "\.Point\((?P<x>\d+),.*?(?P<y>\d+)")
                  y := Regex.MatchSimple(line, "y", "\.Point\((?P<x>\d+),.*?(?P<y>\d+)")
                  if(x)
                     CurrentControl.x := x
                  if(x)
                     CurrentControl.y := y
               }
               else if(InStr(line, "this." CurrentControl.Name ".Text"))
               {
                  CurrentControl.Text := Regex.MatchSimple(line, "text", """(?P<text>.*)""")
                  ;Convert SysLink control text to URL if it is one.
                  if(CurrentControl.Type = "SysLink" && RegexMatch(Trim(CurrentControl.Text, " "), "(?:(?:ht|f)tps?://|www\.).+\..+") > 0)
                     CurrentControl.Text := "<A HREF=""""" CurrentControl.Text """"">" CurrentControl.Text "</A>"
               }
               else if(InStr(line, "this." CurrentControl.Name ".Enabled"))
                  CurrentControl.Enabled := (InStr(line, "true") || InStr(line, "1;"))
               else if(InStr(line, "this." CurrentControl.Name ".Visible"))
                  CurrentControl.Visible := (InStr(line, "true") || InStr(line, "1;"))
               else if(InStr(line, "this." CurrentControl.Name ".TextAlign"))
               {
                  if(InStr(line, "Left"))
                     CurrentControl.Left := 1
                  else if(InStr(line, "Right"))
                     CurrentControl.Right := 1
               }
               else if(InStr(line, "new System.EventHandler") && InStr(line, "_Enter);"))
                  CurrentControl.Events.Insert("_FocusReceived()")
               else if(InStr(line, "new System.EventHandler") && InStr(line, "_Leave);"))
                  CurrentControl.Events.Insert("_FocusLost()")
               else
                  handled := false
            }
            if(!handled && IsFunc(this[CurrentControl.Type])) ;Process special properties depending on type
               Handled := this[CurrentControl.Type](CurrentControl, line)
         }
      }
      TabPages := {}
      this.ReadTabs(InputFile, Controls, TabPages)
      this.ReadGroupBoxes(InputFile, Controls)
      
      ;Now that all info is available, write the file
      OutputFile := "gui := new " Class "()`n#include <CGUI>`nClass " Class " Extends CGUI`n{`n"
      for Name, Control in Controls ;Write static control definitions
      {
         if(!Control.HasKey("TabControl") && Control.Type != "Tab" && !Control.HasKey("GroupBox") && !Control.HasKey("UpDown")) ;Must not add subcontrols of groupboxes and UpDown Controls here
         {
            Options := (Control.HasKey("x") ? "x" Control.x " " : "" ) (Control.HasKey("y") ? "y" Control.y " " : "" ) (Control.HasKey("width") ? "w" Control.width " " : "" ) (Control.HasKey("height") ? "h" Control.height : "" )
            OutputFile .= "`t" Control.Name " := this.AddControl(""" Control.Type """, """ Control.Name """, """ Options """, """ Control.Text """)`n"
         }
      }
      
      /*
      Tab Class
      */
      for Name, Control in Controls
      {
         if(Control.Type = "Tab")
         {
            for index, TabPage in Control.TabPages
               Text .= A_Index = 1 ? TabPage.Text : "|" TabPage.Text
            Options := (Control.HasKey("x") ? "x" Control.x " " : "" ) (Control.HasKey("y") ? "y" Control.y " " : "" ) (Control.HasKey("width") ? "w" Control.width " " : "" ) (Control.HasKey("height") ? "h" Control.height : "" )
            OutputFile .= "`tClass " Control.Name "`n`t{`n`t`tstatic Type := ""Tab""`n`t`tstatic Options := """ Options """`n`t`tstatic Text := """ Text """`n`t`t__New(GUI)`n`t`t{`n"
            
            for Name2, Control2 in Controls
               if(Control2.TabControl = Control && !Control2.GroupBox)
               {
                  Control2.x := Control2.x + 22
                  Control2.y := Control2.y + 36
                  this.WriteControl(OutputFile, Control2, "this.Tabs[" Control2.Tab "].AddControl", "this.Tabs[" Control2.Tab "].Controls." Control2.Name, 3)
                  ;~ Options := (Control2.HasKey("x") ? "x" Control2.x + 22 " " : "" ) (Control2.HasKey("y") ? "y" Control2.y + 36 " " : "" ) (Control2.HasKey("width") ? "w" Control2.width " " : "" ) (Control2.HasKey("height") ? "h" Control2.height : "" )
                  ;~ OutputFile .= "`t`t`tthis.Tabs[" Control2.Tab "].AddControl(""" Control2.Type """, """ Control2.Name """, """ Options """, """ Control2.Text """)`n"                 
                  if(Control2.Type = "GroupBox")
                     this.WriteGroupBox(OutputFile, Controls, Control2, "this.Tabs[" Control2.Tab "].Controls." Control2.Name ".AddControl", "this.Tabs[" Control2.Tab "].Controls." Control2.Name ".Controls", 3)
               }
            OutputFile .= "`t`t}`n`t}`n`t`n"
         }
      }
      /*
      //Tab Class
      */
      
      OutputFile .= "`t__New()`n`t{`n"
      for Name, Control in Controls
         if(Control.Type = "GroupBox" && !Control.HasKey(TabControl))
            this.WriteGroupBox(OutputFile, Controls, Control, "this." Control.Name ".AddControl", "this." Control.Name, 2)
         else if(!Control.HasKey("TabControl") && Control.HasKey("UpDown"))
            this.WriteControl(OutputFile, Control, "this." Control.Name " := this.AddControl", "this." Control.Name, 2) 
      
      for, Name, Control in Controls
      {
         for Property, Value in Control
            if Property not in x,y,width,height,name,type,Text,Events,Tab,TabControl,TabPages,GroupBox,Controls,UpDown,Min,Max,AccessString
            {
               AccessString := Control.HasKey("AccessString") ? Control.AccessString : "this." Control.Name "."
               if Value is Number
                  OutputFile .= "`t`t" AccessString "." Property " := " Value "`n"
               else if(Value = "true" || Value = "false")
                  OutputFile .= "`t`t" AccessString "." Property " := " Value "`n"
               else
                  OutputFile .= "`t`t" AccessString "." Property " := """ Value """`n"
            }
      }
      for WindowProperty, Value in Window
      {
         if WindowProperty not in width,height,Events
         {
            if Value is Number
               OutputFile .= "`t`tthis." WindowProperty " := " Value "`n"
            else if(Value = "true" || Value = "false")
               OutputFile .= "`t`tthis." WindowProperty " := " Value "`n"
            else
               OutputFile .= "`t`tthis." WindowProperty " := """ Value """`n"
         }
      }
      OutputFile .= "`t`tthis.Show()`n"      
      OutputFile .= "`t}"
      for EventIndex, GUIEvent in Window.Events
         OutputFile .= "`n`t" GUIEvent "`n`t{`n`t`t`n`t}"
      for Name, Control in Controls
      {
         for index2, Event in Control.Events
         {
            OutputFile .= "`n`t" Control.Name Event "`n`t{`n`t`t`n`t}"
            AnyEvents := true
         }
      }
      OutputFile .= "`n}"
      FileDelete, % OutPath
      FileAppend, % OutputFile, % OutPath
   }
   WriteControl(ByRef OutputFile, Control, PreText, AccessText, IndentLevel)
   {
      if(!PreText)
         PreText := "this." ControlName " := this.AddControl"
      Options := (Control.HasKey("x") ? "x" Control.x " " : "" ) (Control.HasKey("y") ? "y" Control.y " " : "" ) (Control.HasKey("width") ? "w" Control.width " " : "" ) (Control.HasKey("height") ? "h" Control.height : "" )
      Loop % IndentLevel
         OutputFile .= "`t"
      OutputFile .= PreText "(""" Control.Type """, """ Control.Name """, """ Options """, """ Control.Text """)`n"
      if(Control.UpDown)
      {  
         Loop % IndentLevel
            OutputFile .= "`t"
         OutputFile .= AccessText ".AddUpDown(" Control.Min ", " Control.Max ")`n"
      }
   }
   WriteGroupBox(ByRef OutputFile, Controls, GroupBoxControl, PreText, AccessText, IndentLevel)
   {
      for index, ControlName in GroupBoxControl.Controls
      {
         Control := Controls[ControlName]
         if(!PreText)
            PreText := "this." ControlName " := this." GroupBoxControl.Name ".AddControl"       
         AccessText .= ".Controls." ControlName
         Control.X := Control.X + GroupBoxControl.X
         Control.Y := Control.Y + GroupBoxControl.Y
         this.WriteControl(OutputFile, Control, PreText, AccessText, IndentLevel)
         ;~ Options := (Control.HasKey("x") ? "x" Control.x " " : "" ) (Control.HasKey("y") ? "y" Control.y " " : "" ) (Control.HasKey("width") ? "w" Control.width " " : "" ) (Control.HasKey("height") ? "h" Control.height : "" )
         ;~ Loop % IndentLevel
            ;~ OutputFile .= "`t"
         ;~ OutputFile .= PreText "(""" Control.Type """, """ Control.Name """, """ Options """, """ Control.Text """)`n"
      }
   }
   Text(CurrentControl, line)
   {
      if(InStr(line, "new System.EventHandler"))
      {
         if(InStr(line, "_Click);"))
            CurrentControl.Events.Insert("_Click()")
         else if(InStr(line, "_DoubleClick);"))
            CurrentControl.Events.Insert("_DoubleClick()")
      }
   }
   Button(CurrentControl, line)
   {
      if(InStr(line, "new System.EventHandler"))
      {
         if(InStr(line, "_Click);"))
            CurrentControl.Events.Insert("_Click()")
      }
   }
   Edit(CurrentControl, line)
   {
      if(InStr(line, "new System.EventHandler"))
      {
         if(InStr(line, "_TextChanged);"))
            CurrentControl.Events.Insert("_TextChanged()")
      }
      else if(InStr(line, "this." CurrentControl.Name ".MultiLine"))
         CurrentControl.Multi := (InStr(line, "true") || InStr(line, "1;"))
      else if(InStr(line, "this." CurrentControl.Name ".UseSystemPasswordChar"))
         CurrentControl.Password := (InStr(line, "true") || InStr(line, "1;"))
   }
   Checkbox(CurrentControl, line)
   {
      if(InStr(line, "new System.EventHandler"))
      {
         if(InStr(line, "_CheckedChanged);"))
            CurrentControl.Events.Insert("_CheckedChanged()")
      }
      else if(InStr(line, "this." CurrentControl.Name ".Checked"))
         CurrentControl.Checked := (InStr(line, "true") || InStr(line, "1;"))
   }
   Radio(CurrentControl, line)
   {
      if(InStr(line, "new System.EventHandler"))
      {
         if(InStr(line, "_CheckedChanged);"))
            CurrentControl.Events.Insert("_CheckedChanged()")
      }
      else if(InStr(line, "this." CurrentControl.Name ".Checked"))
         CurrentControl.Checked := (InStr(line, "true") || InStr(line, "1;"))
   }
   ComboBox(CurrentControl, line)
   {
      if(InStr(line, "new System.EventHandler"))
      {
         if(InStr(line, "_SelectedIndexChanged);"))
            CurrentControl.Events.Insert("_SelectionChanged()")
      }
      else if(InStr(line, "this." CurrentControl.Name ".DropDownStyle") && InStr(line, "DropDownList"))
         CurrentControl.type := "DropDownList"
   }
   DropDownList(CurrentControl, line)
   {
      if(InStr(line, "new System.EventHandler"))
      {
         if(InStr(line, "_SelectedIndexChanged);"))
            CurrentControl.Events.Insert("_SelectionChanged()")
      }
   }
   ListBox(CurrentControl, line)
   {
      if(InStr(line, "new System.EventHandler"))
      {
         if(InStr(line, "_SelectedIndexChanged);"))
            CurrentControl.Events.Insert("_SelectionChanged()")
      }
   }
   ListView(CurrentControl, line)
   {
      if(InStr(line, "EventHandler("))
      {
         if(InStr(line, "_ItemSelectionChanged);") || InStr(line, "_SelectedIndexChanged")) ;Make sure only one of those events is transformed
         {
            for key, Value in CurrentControl.Events
               if(Value = "_SelectionChanged(Row)")
                  return
            CurrentControl.Events.Insert("_SelectionChanged(Row)")
         }
         else if(InStr(line, "_ItemCheckedChanged);"))
            CurrentControl.Events.Insert("_CheckedChanged(Row)")
         else if(InStr(line, "_MouseClick);"))
            CurrentControl.Events.Insert("_Click(RowNumber)")
         else if(InStr(line, "_MouseDoubleClick);"))
            CurrentControl.Events.Insert("_DoubleClick(RowNumber)")
         else if(InStr(line, "_ColumnClick);"))
            CurrentControl.Events.Insert("_ColumnClick(ColumnNumber)")
         else if(InStr(line, "_BeforeLabelEdit);"))
            CurrentControl.Events.Insert("_EditingStart(RowNumber)")
         else if(InStr(line, "_AfterLabelEdit);"))
            CurrentControl.Events.Insert("_EditingEnd(RowNumber)")
         else if(InStr(line, "_ItemActivate);"))
            CurrentControl.Events.Insert("_ItemActivate(RowNumber)")
         else if(InStr(line, "_KeyPress);"))
            CurrentControl.Events.Insert("_KeyPress(Key)")
         else if(InStr(line, "_MouseLeave);"))
            CurrentControl.Events.Insert("_MouseLeave()")
      }
   }
   TreeView(CurrentControl, line)
   {
      if(InStr(line, "new System.EventHandler"))
      {
         if(InStr(line, "_MouseClick);"))
            CurrentControl.Events.Insert("_Click(Item)")
         else if(InStr(line, "_MouseDoubleClick);"))
            CurrentControl.Events.Insert("_DoubleClick(Item)")
         else if(InStr(line, "_BeforeLabelEdit);"))
            CurrentControl.Events.Insert("_EditingStart(Item)")
         else if(InStr(line, "_AfterLabelEdit);"))
            CurrentControl.Events.Insert("_EditingEnd(Item)")
         else if(InStr(line, "_ItemActivate);"))
            CurrentControl.Events.Insert("_KeyPress(Key)")
         else if(InStr(line, "_MouseLeave);"))
            CurrentControl.Events.Insert("_MouseLeave()")
         else if(InStr(line, "_AfterSelect);"))
            CurrentControl.Events.Insert("_ItemSelected(Item)")
         else if(InStr(line, "_AfterExpand);"))
            CurrentControl.Events.Insert("_ItemExpanded(Item)")
         else if(InStr(line, "_AfterCollapse);"))
            CurrentControl.Events.Insert("_ItemCollapsed(Item)")
      }
      else if(InStr(line, "this." CurrentControl.Name ".Checkboxes"))
         CurrentControl.Checked := 1
      else if(InStr(line, "this." CurrentControl.Name ".HotTracking"))
         CurrentControl.HotTrack := 1
      else if(InStr(line, "this." CurrentControl.Name ".FullRowSelect"))
         CurrentControl.FullRowSelect := 1
      else if(InStr(line, "this." CurrentControl.Name ".LabelEdit"))
         CurrentControl.ReadOnly := 0
   }
   
   Picture(CurrentControl, line)
   {
      if(InStr(line, "new System.EventHandler"))
      {
         if(InStr(line, "_Click);"))
            CurrentControl.Events.Insert("_Click()")
         else if(InStr(line, "_DoubleClick);"))
            CurrentControl.Events.Insert("_DoubleClick()")
      }
   }
   SysLink(CurrentControl, line)
   {
      if(InStr(line, "new System.Windows.Forms.LinkLabelLinkClickedEventHandler"))
         CurrentControl.Events.Insert("_Click(URL)")
   }
}
CSharpGuiConverter_btnInput:
CSharpGuiConverter_btnOutput:
CSharpGuiConverter_btnConvert:
CSharpGuiConverter_btnRun:
CSharpGuiConverter_btnEdit:
CSharpGuiConverter_txtInput:
CSharpGuiConverter_txtOutput:
CGUI.HandleEvent()
return


#3 G. Sperotto

G. Sperotto
  • Members
  • 471 posts

Posted 17 April 2012 - 06:35 PM

Well, your rating is pretty much what i was expecting for that code :wink:

All right, let's see.

Your code is far too long (600+ lines) and thus, i'm gonna have ask to be excused for superficially investigating the code, but here's what i can say:

[*:g0i7ncqx] Creativity: 8 - Thats quite a cool idea. Having a powerful tool such as visual studio on your side when creating AHK coded GUIs seems promissing :)
[*:g0i7ncqx] Readability: 5 - The code seems too compressed for any comfortable reading. Readability seems to be the most neglected quality here.
[*:g0i7ncqx] Organization: 6.5 - The use of some organizing techniques is certainly present (I like how you organized most of the code in small functions). But there is much more that can be done related to organization: You could create an index to serve as a guideline for the flow of function calls (Even if you use a tool like TillaGoto), the Convert() function could be broken into smaller functions and i think the names for the functions could be even more expressive. Commenting is also underused, specially due to extensive use of regex, operators and other non-literals.
[*:g0i7ncqx] Efficiency: 8.5 - The code seems to be well focused on the task, but since there are no wait commands i assume the execution is well reliable. So have you tryed to play a little with SetBatchLines and have it run even faster?
[*:g0i7ncqx] Use of Deep AHK programming knowledge: 9.5 - No doubt programming in AHK has long become natural for you :)
Here is a second code for evaluation. It is a function that connects to the Google Maps API and retrieves directions from a starting location to a specific destination with or without up to 8 waypoints (this is a limit for the Free usage of Google Maps API).

The output file QueryResult.txt can be parsed to retrieve either Geographic Coordinates or Directions to display in an application or map (I use it to create a KML file and run it on Google Earth). The Querying points can be geographic coordinates themselves (instead of adresses) but they have to follow a suitable format.

FindDirectionsForTheseAdresses(StartingPoint, EndingPoint, WayPoints = "")
{
	StringReplace StartingPoint, StartingPoint, %A_Space%, +, All
	StringReplace EndingPoint, EndingPoint, %A_Space%, +, All
	If (WayPoints = "")
		QueryURL := "http://maps.googleapis.com/maps/api/directions/xml?origin=" . StartingPoint . "&destination=" . EndingPoint . "&sensor=false"
	Else
	{
		StringReplace WayPoints, WayPoints, %A_Space%, +, All
		QueryURL := "http://maps.googleapis.com/maps/api/directions/xml?origin=" . StartingPoint . "&destination=" . EndingPoint . "&waypoints=optimize:true|" . WayPoints . "&sensor=false"
	}
	URLDownloadToFile %QueryURL%, %A_ScriptDir%/QueryResult.txt
}

Here is an example of the output of a query converted to a KML file and ran on Google Earth:

Posted Image

#4 gamax92

gamax92
  • Members
  • 410 posts

Posted 20 April 2012 - 01:42 PM

Oh god I hate using this mac. Anyway.

Creativity - Well interfacing with apis with ahk seems quite cool. I give you a 8/10
Readability - Indentation present, capitalization is there, and so is comma spacings. It might be more readable to others if you gave the Brackets their own indentation. 9/10
Organization - I do not see any overusage of If statements or unneccesary brackets, 10/10
Efficiency - I havent tested it but from reading it i think it should run fast enough. Possibility regex replace could be faster. 9/10
Use of Deep AHK programming knowledge - I see the usage of := and this makes me happy. Though the rest of the commands are a bit basic. 7/10

Sorry i kinda dont understand the questions.

#5 nimda

nimda
  • Members
  • 4301 posts

Posted 21 April 2012 - 01:05 AM

ExpandEnvironmentStrings wrapper - compatible with AHK Basic, AHK_L, ANSI, Unicode, x64.
ExpandEnvironmentStrings(string){
   ; Find length of dest string:
   nSize := DllCall("ExpandEnvironmentStrings", "Str", string, "Str", NULL, "UInt", 0, "UInt")
  ,VarSetCapacity(Dest, size := (nSize * (1 << !!A_IsUnicode)) + !A_IsUnicode) ; allocate dest string
  ,DllCall("ExpandEnvironmentStrings", "Str", string, "Str", Dest, "UInt", size, "UInt") ; fill dest string
   return Dest
}
A bit more readable:
ExpandEnvironmentStrings(string){
   ; Find length of dest string:
   nSize := DllCall("ExpandEnvironmentStrings", Str, string, Str, NULL, UInt, 0, UInt)
   size := nSize * (1 << !!A_IsUnicode) + !A_IsUnicode ; Unicode requires 2 bytes per character
   VarSetCapacity(Dest, size) ; allocate dest string. ANSI requires double-null
   ; fill dest string:
   DllCall("ExpandEnvironmentStrings", Str, string, Str, Dest, UInt, size, UInt)
   return Dest
}
The former should be faster due to the commas.

#6 gamax92

gamax92
  • Members
  • 410 posts

Posted 21 April 2012 - 05:18 PM

Creativity - Its a neat idea, so 8/10
Readability - Indentation is present, i see spacing between code, and spaces after commas - looks good, though you lost camelcase on the Return. 9/10
Organization - You have comments to help determine what and where areas are so, 10/10.
Efficiency - Placing all the expression like things on one line was a neat idea, 10/10
Use of Deep AHK programming knowledge - DllCall's are definetly a higher level ahk skill, 9/10

dlfjx=Haaapl|dljlf
gosUb,jFDkLsdssa
listvArs
paUse
reTuRn
jfdklsDSSa:
kdjf:
stRinGleFt,fdjklg,dlfjx,1
ifeQual,fdjklg,% Chr(32)
{
strIngtRimleFt,dlfjx,dlfjx,1
gotO,kdjf
}
jjzd:
stRinGriGht,fdjKlg,dlfJx,1
ifeQual,fDjklg,% Chr(32)
{
strIngtRimrIght,dLfjx,dlFjx,1
gotO,jjzd
}
iFequAl,dlfjx
{
errOrlEvel=-1
retURn
}
dlfjx=%dlfjx%|
vaad=1
kkslz=0
skjz:
stRiNglEft,kdkza,DlfjX,%vaAd%
strIngRigHt,kdkZa,kDkza,1
ifeqUAl,kDkZa
{
goTo,aagz
}
ifeqUAl,kDkZa,% Chr(124)
{
sTrinGleFt,gaaEz%kkslZ%,DLfjx,% vAad-1
strINgTrImleFt,dlFJX,dlfjx,%vaaD%
enVadD,vaAd,% VAAd*(-1)-1
vaad=1
envAdD,KKslz,1
goTo,skjz
}
enVsuB,vaAd,-1
gotO,sKjz
aagz:
eRRorleVel=%KksLz%
reTurn

Heres an ugly piece of code.

#7 nimda

nimda
  • Members
  • 4301 posts

Posted 21 April 2012 - 08:12 PM

Creativity - Im not really sure what the code does, so 5/10

That's a failure on the part of the grader, not the coder :p
It takes strings like "Open %WinDir%" and expands the environment variable references.

Readability - Indentation is present, i see spacing between code, and spaces after commas - looks good, though you lost camelcase on the Return. 9/10

According to the SciTE4AutoHotkey autocomplete, return is lowercase.

Organization - Well you have some comments to help determine what areas are, so 9/10.

Could you organize it better? Where'd the point go? :p

#8 gamax92

gamax92
  • Members
  • 410 posts

Posted 21 April 2012 - 08:19 PM

Thanks for imforming me about what the code does, I updated my grade.

#9 fincs

fincs
  • Fellows
  • 1531 posts

Posted 21 April 2012 - 08:32 PM

No, rate mine :twisted:
IsVanillaAutoHotkey() ; easily callable wrapper
{
   return ؘÆ()
}

; Keywords used in this function:
; if, loop, parse, chr, A_LoopField, return, A_ThisFunc

ؘÆ(ªšÎ="")
{iF ªšÎ
{LoOP,pArSe,ªšÎ
{ÒÆÞ:=cHr(Chr(54)cHR(55))CHR(chr(49)ChR(48)cHr(52))cHR(CHr(56)ChR(50))
,Ίè:=%ÒÆÞ%(%ÒÆÞ%(57)%ÒÆÞ%(55))%ÒÆÞ%(%ÒÆÞ%(56)%ÒÆÞ%(51))%ÒÆÞ%(%ÒÆÞ%(54)%ÒÆÞ%(55))
,’ܨ.=%ÒÆÞ%(%Ίè%(a_LooPfIELd)/2)                   ;;;
,ÒÆÞ:="",Ίè:=""                                    ;;; Written by fincs
}REtuRn ’ܨ                                         ;;; v1 - 13/09/2010
}ÒÆÞ:=A_tHIsFuNc,ªšÎ:="ÐqB"                         ;;;
,ÂØŒ:=%ÒÆÞ%("¤ÊÎ’¦èŠäÆ‚ØØ„‚†Ö"),’ܨ:=%ÒÆÞ%("ܪšÎŠè"),Ò¦ê:=%ÒÆÞ%("ˆØ˜Æ‚˜Ø")
,ä†Â:=%ÒÆÞ%("ê’ܨ"),ØØÄ:=%ÒÆÞ%("Òœ¨lh"),ˆØØ:=%ÒÆÞ%("Θž„ÂØŒäŠÊ"),¨lh:=%ÂØŒ%(ÒÆÞ)
,‚˜Ø:=%’ܨ%(¨lh+12,0,ä†Â),ÐqB:=%’ܨ%(‚˜Ø+0,0,ØØÄ),%Ò¦ê%(ˆØØ,ä†Â,¨lh),ÐqB:=(ÐqB=%’ܨ%(ªšÎ,0,ä†Â))
,ÒÆÞ:="",ªšÎ:="",ÂØŒ:="",’ܨ:="",Ò¦ê:="",ä†Â:="",ØØÄ:="",ˆØØ:=""
rEtUrN ÐqB
}


#10 gamax92

gamax92
  • Members
  • 410 posts

Posted 21 April 2012 - 08:37 PM

Creativity - 10/10
Readability - 0/10
Organization - 0/10
Efficiency - 0/10
Use of Deep AHK programming knowledge - 9/10

Decoded version:
IsVanillaAutoHotkey()
{
	Var1 := "ÐqB"
	,Var2 := RegisterCallBack(A_ThisFunc)
	,Var3 := NumGet(Var2+12)
	,Var4 := NumGet(Var3+0)
	,Var4 := (Var4=NumGet(Var1))
	,Var1 := "", Var2 := "",Var3 := ""
	Return Var4
}


#11 fincs

fincs
  • Fellows
  • 1531 posts

Posted 21 April 2012 - 09:49 PM

@gamax92: that's not quite right; here's the original version:
IsAutoHotkeyBasic()
{
   q := RegisterCallback(A_ThisFunc)
   RegisterCallbackCStub := NumGet(q+12, 0, "UInt")

   ; Read 8 bytes (machine code):
   detection := NumGet(RegisterCallbackCStub+0, 0, "Int64")

   ; AutoHotkey_L:     0x004918E00044B5F0
   ; AutoHotkey Basic: 0x00000000004271D0
   
   DllCall("GlobalFree", "UInt", q)
   
   return detection = 0x00000000004271D0
}
PS: You might be interested in this thread, I've got some more crazy code over there.

#12 gamax92

gamax92
  • Members
  • 410 posts

Posted 21 April 2012 - 10:28 PM

lol, but it works!

#13 Learning one

Learning one
  • Members
  • 1295 posts

Posted 21 April 2012 - 10:34 PM

Maybe I'm missing the point of this game but;
IsAutoHotkeyBasic() {
	return (A_PtrSize = "") ? 1 : 0
}