Planning for reusable code

Hello everyone,

It’s very late and I’ve also had quite a few rums so please excuse me if I ramble or don’t make sense!

I’ve always had a very unplanned approach to programming and often come into situations where I realise that with more planning I could have saved myself some time.

I very rarely have a project that is truly cross platform but am going to be starting one in the New Year that will be for Windows, Linux, OSX and Web.

Has anyone got any methodology, shortcuts, tips or great advice for code-reuse, pitfalls, etc that might be of use?

If I make no sense I will delete this post in the morning when I view it in shame :slight_smile:

Think as small as possible. Consider each component as its own mini program. Each window, each container, each thread… everything.

Your window controls should be private so NOTHING except that window can interact with them. Then you use methods, computed properties, and rarely events as a means for outside code to provide access to what you need. For example, maybe you have a label in your window that shows a file path. You could add a computed property TargetFile As FolderItem that both stores the FolderItem in a private normal property, and updates the label with the path.

What this does is allow the code outside the window to care about only what is important: the file. What the window does with that file is unimportant to the outside code. It doesn’t care that the path is displayed in a label on the window. That’s not its job. Its job is to give the window what it needs, and possibly get some data back from the window when finished. The calling code worries about the “why” of the problem, the window itself deals with the “how” of the problem.

This means the window has its own inputs and outputs, just like a simple program. You can change anything you want inside the window, as long as those inputs and outputs are satisfied, and your code won’t break.

This is of course a high level overview of the concept, but start there. As functionality is compartmentalized in this way, it makes it easier to reuse.

3 Likes

Read “Clean Code” by Robert Martin and “Head First Design Patterns”.

Encapsulate everything a much as possible. Make every control in every window and container control private. Use interfaces to treat different things the same.

3 Likes

Here are my tips:

  1. Put your code into External Modules so they can be run by other apps and platforms
  2. Create an External Module for each area of work (eg Databases, Strings, PDF, etc)
  3. Keep one External Module aside for Methods related to this one app alone
  4. For Web only code (eg access to Cookies) create a Web-only External Module
  5. Use #If TargetDesktop … #ElseIf TargetWeb … #ElseIf Target Console … #EndIf to stop cross-environment errors
  6. When passing in variables to the Method, pass UI items as Objects. In the Methods turn them back into items
  7. Plan for all environments: Desktop, Web and Mobile
  8. You will improve your shared code over time, meaning all your apps improve
  9. Your code will get smaller with API 2.0
  10. Place all UI on Containers that share the same name on Desktop and Web. Pass the Container names to your Methods and Classes (rather than Window names). This allows you to have the same Container in multiple applications.
  11. Share all other external resources (pictures, databases, classes, fonts, plist, etc)

Web 2.0 and API 2.0 have made this MUCH easier!

1 Like

This Desktop and Web app have an identical interface within Containers and identical code within a Class per Container:

2 Likes

Hello , my advice: always think on paper before coding. If you have already code, dont forget to document it.

If you dont have a tool, how about PlantUml. Input is a simple flatfile, which is also easy to checkin and diff.

BR Rainer

Some great ideas here folks. Many thanks.

1 Like

Happy coding / BR Rainer

1 Like